首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android TCP通信错误

Android TCP通信错误
EN

Stack Overflow用户
提问于 2013-08-17 05:19:32
回答 1查看 135关注 0票数 0

我对Android编程非常陌生。

我在android中的代码如下

代码语言:javascript
复制
 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#代码中有一个服务器,代码侦听端口并写入输出

但是,这只是在我第一次输入文本并单击发送时,在服务器端接收到代码,我可以在控制台中看到ouptput,但在此之后没有接收到该值- .So -如何保持这个套接字运行?我遗漏了什么?

谢谢大家

EN

回答 1

Stack Overflow用户

发布于 2013-08-17 06:12:24

尝尝这个

创建一个类client.java

代码语言:javascript
复制
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中创建onCreate对象。

代码语言:javascript
复制
client con=new client("10.20.50.68", 8888);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18285409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档