Hello, in the absence of experience and knowledge I can not understand the problem, I hope for help. I have a simple C # server socket written on my PC
try { IPAddress localAddress = IPAddress.Parse("10.152.8.26"); Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 5555); listenSocket.Bind(ipEndpoint); listenSocket.Listen(1); while (true) { Console.WriteLine("Server wait {0}", ipEndpoint); Socket handler = listenSocket.Accept(); Console.WriteLine("Connect with {0}", handler.RemoteEndPoint); Console.WriteLine("Send message.."); handler.Send(encoding.GetBytes("I'm wait")); Console.WriteLine("Close connect"); handler.Close(); } } catch (Exception e) { Console.WriteLine("Error: {0}", e.ToString()); } } }
And socket client on android
public class ClientActivity extends Activity { EditText textOut; TextView textIn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client); textOut = (EditText)findViewById(R.id.editText1); Button buttonSend = (Button)findViewById(R.id.button1); textIn = (TextView)findViewById(R.id.textView1); buttonSend.setOnClickListener(buttonSendOnClickListener); } Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener() { //@Override public void onClick(View arg0) { // TODO Auto-generated method stub Socket socket = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; try { socket = new Socket("10.152.8.26", 5555); dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutputStream.writeUTF(textOut.getText().toString()); String readUTF = dataInputStream.readUTF(); textIn.setText(readUTF); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { if (socket != null){ socket.close(); } if (dataOutputStream != null){ dataOutputStream.close(); } if (dataInputStream != null){ dataInputStream.close(); } } catch (Exception 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(); } } } } ;}
I am very bad at this, the whole code is nuggul. If from the android in the browser enter the address and port, the server sees the connection. And through the application on the phone, nothing happens. Help please advice, what's wrong?