2013-08-17 59 views
0

我很新Android編程Android TCP通信錯誤

我的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#代碼 代碼監聽端口的服務器,並寫入輸出

然而,這僅適用於第一次,當我鍵入文本,然後單擊發送代碼在服務器端收到,我可以在控制檯中看到ouptput,但是之後沒有收到該值。因此,如何讓該套接字運行?我錯過了什麼?

謝謝所有

回答

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 con=new client("10.20.50.68", 8888); 

創建client.java的對象,使用con.send("your_data")發送和String data=con.read();接收數據。 .. 如果你想異步監聽套接字中的數據,請使用線程