0

我对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# 代码中的服务器代码监听端口并写入输出

但是,这仅在我输入文本并单击发送时第一次有效,在服务器端接收到代码,我可以在控制台中看到输出,但之后没有收到值。那么我怎样才能保留这个套接字跑步 ?我错过了什么?

谢谢你们

4

1 回答 1

0

尝试这个

创建一个类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 "";  
}

并在您的活动 onCreate 中创建一个 client.java 对象

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

并用于con.send("your_data")发送和String data=con.read();接收数据...如果您想异步监听套接字中的数据,请使用线程

于 2013-08-17T06:12:24.390 回答