C# Server Code:

Code: TcpListener tcpListener = new TcpListener(sampleTcpPort); while (true) { tcpListener.Start(); //Program blocks on Accept() until a client connects. Socket soTcp = tcpListener.AcceptSocket(); Console.WriteLine("SampleClient is connected through TCP."); Byte[] received = new Byte[1024]; int bytesReceived = soTcp.Receive(received, received.Length, 0); String dataReceived = System.Text.Encoding.ASCII.GetString(received); Console.WriteLine(dataReceived); String returningString = "The Server got your message through TCP: " + dataReceived; Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes(returningString.ToCharArray()); //Returning a confirmation string back to the client. soTcp.Send(returningByte, returningByte.Length, 0); tcpListener.Stop(); }

Android Client Code:

Code: class testClass { private String serverIpAddress = "127.0.0.1"; public String results=""; public testClass() { Thread cThread = new Thread(new ClientThread()); cThread.start(); } public class ClientThread implements Runnable { public void run() { try { InetAddress serverAddr = InetAddress.getByName(serverIpAddress); Log.d("ClientActivity", "C: Connecting..."); while (true) { results=""; try { Socket socket = new Socket("localhost", 4567); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); out.write("Test"); out.flush(); String inMsg = ""; boolean b=false; while (!b) { inMsg = in.readLine(); if(inMsg!="") b=true; } socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) {Log.e("ClientActivity", "S: Error", e);} } } catch (Exception e) { Log.e("ClientActivity", "C: Error", e);} } } }

Hi everybody,I'm trying to write code to communicate between c# server and Android client by using TCP socket, I'm using below code but it dosn't work