android tcp接收数据问题

我要实现一个功能就是在android客户端点击一个dialog的确定按钮之后,提交数据并接收服务端返回的数据,目前提交的数据服务器端是可以接收的到,但是服务器端返回的数据却接收不到。我的客户端代码如如图。图片说明图片说明图片说明接收的时候总是在in.readline位置就停止了,然后报异常说 socket close。请问要怎么解决啊。

1个回答

package com.example.sockettest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Client extends Activity {
EditText sendinfo,ip;
Button send;
TextView text;
Socket socket;
private String ServiceIP="192.168.1.15";
private int Port=8080;
private boolean isrecier=false;
ReceiveThread receiveThread=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
new Thread(startConcent).start();
send.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        new Thread(SendData).start();
    }
});

}

private void init(){
sendinfo=(EditText) findViewById(R.id.sendinfo);
send=(Button) findViewById(R.id.send);
text=(TextView) findViewById(R.id.text);
ip=(EditText) findViewById(R.id.edit_IP);
//ServiceIP=ip.getText().toString();
}

Runnable startConcent=new Runnable() {

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        System.out.println(ServiceIP);
        socket=new Socket(ServiceIP,Port);
        isrecier=true;
        receiveThread=new ReceiveThread(socket);
        receiveThread.start();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

};
Handler handler=new Handler(){
public void handleMessage(Message msg) {
String recdata=msg.obj.toString();
System.out.println(""+recdata);
//text.append(recdata);
text.setText(recdata.toString());
};
};
Runnable SendData=new Runnable() {
OutputStream outStream;
@Override
public void run() {
byte[]sendbuff=null;
try {
sendbuff=sendinfo.getText().toString().getBytes("utf-8");
outStream=socket.getOutputStream();
outStream.write(sendbuff);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

};
private class ReceiveThread extends Thread{
InputStream inStream=null;
byte[]buffer;
String data=null;
public ReceiveThread(Socket socket){
try {
inStream=socket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (isrecier) {
buffer=new byte[1024];
try {
inStream.read(buffer);
data=new String(buffer, "utf-8").trim();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg=new Message();
msg.obj=data;
System.out.println(""+data);
handler.sendMessage(msg);
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(receiveThread!=null){
isrecier=false;
receiveThread.interrupt();
}
}
}

rudiyuan0035
rudiyuan0035 sendbuff=sendinfo.getText().toString().getBytes("utf-8"); outStream=socket.getOutputStream(); outStream.write(sendbuff);
3 年多之前 回复
rudiyuan0035
rudiyuan0035 程序崩了,报了一个NullPointerException。![图片说明](http://img.ask.csdn.net/upload/201702/22/1487750155_534459.jpg)
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐