rudiyuan0035
rudiyuan0035
采纳率50%
2017-02-22 05:41

android tcp接收数据问题

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • aptx14 aptx14 4年前

    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();
    }
    }
    }

    点赞 评论 复制链接分享

相关推荐