Android TCP通信错误

本文关键字:错误 通信 TCP Android | 更新日期: 2023-09-27 18:28:03

我是Android编程的新手。

我在android中的代码如下

 public class AndroidClient extends Activity {
 EditText textOut;
 TextView textIn;
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_android_client);
 textOut = (EditText)findViewById(R.id.textout);
 Button buttonSend = (Button)findViewById(R.id.send);
 textIn = (TextView)findViewById(R.id.textin);
 buttonSend.setOnClickListener(buttonSendOnClickListener);
 }
  Button.OnClickListener buttonSendOnClickListener
  = new Button.OnClickListener(){
 @Override
 public void onClick(View arg0) {
 // TODO Auto-generated method stub
 new BackgroundDataTask().execute("");
}};
    public class BackgroundDataTask extends AsyncTask<String,String,String> {
    private Exception ex;
    @Override
    protected String doInBackground(String... urls) {
         try {
          socket = new Socket("10.20.50.68", 8888);
          dataOutputStream = new DataOutputStream(socket.getOutputStream());
          dataInputStream = new DataInputStream(socket.getInputStream());
          dataOutputStream.writeUTF(textOut.getText().toString());
          textIn.setText(dataInputStream.readUTF());
          socket = serverSocket.accept();
          textIn.setText(socket.getInetAddress().toString());
         } catch (UnknownHostException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
         finally{
          if (socket != null){
           try {
            socket.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
          if (dataOutputStream != null){
           try {
            dataOutputStream.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
          if (dataInputStream != null){
           try {
            dataInputStream.close();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
          }
         }
         return "";
    }
}

 }

我有一个C#代码的服务器代码监听端口并写入输出

然而,这只是我第一次输入文本并单击Send时才有效。代码在服务器端收到,我可以在控制台中看到输出,但之后就没有收到值。那么我如何保持这个套接字运行呢?我错过了什么?

感谢所有

Android TCP通信错误

尝试这个

创建一个类client.java

public class client{
Socket socket;
int Port;
InetAddress serverIp;
public boolean connection;
public client(String ip, int port) {
    // TODO Auto-generated constructor stub
    try {
        serverIp = InetAddress.getByName(ip);
        Port = port;
                    connect();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        connection = false;
    }
}
public void connect(){
    try {
            System.out.println("wait connection client "+serverIp+"  "+Port);
        socket = new Socket(serverIp, Port);
        connection = true;

    } catch (IOException e) {
        //e.printStackTrace();
        connection = false;
    }
}
    public boolean send(String message) throws IOException {

    byte[] data=message.getBytes();
    OutputStream out = socket.getOutputStream();
    DataOutputStream dos = new DataOutputStream(out);
    int len = data.length;
    dos.writeInt(len);
    if (len > 0) {
        dos.write(data, 0, len);
    }
    return true;
}
public String read()  {
    InputStream in = null;
    try {
        in = socket.getInputStream();
    } catch (IOException e) {
        connection=false;
        e.printStackTrace();
    }
    DataInputStream dis = new DataInputStream(in);
    try {
        int len = dis.readInt();
        byte[] data = new byte[len];
            if (len > 0) {
                dis.readFully(data);
            }
            String result = new String(data, 0, data.length);
            return result;
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        connection=false;
    }
    return "";  
}

并在创建的活动中创建client.java的对象

client con=new client("10.20.50.68", 8888);

使用CCD_ 1发送数据,使用String data=con.read();接收数据。。。如果希望异步侦听套接字中的数据,请使用线程