locked
TCP socket between C# server and Android Client RRS feed

  • Question

  • 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 :(

    Please please please help me...

    C# Server 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:

    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);}
    	    }
    	}



    • Edited by Belal Shakatra Monday, March 11, 2013 12:56 AM
    • Moved by Jason Dot Wang Tuesday, March 12, 2013 5:19 AM This thread is about Windows Communication Foundation, Serialization, and Networking
    Monday, March 11, 2013 12:53 AM

Answers

  • Hi Shakatra,

      Welcome to MSDN Forum Support.

     

    You are more likely to get more efficient responses to
    Windows Communication Foundation, Serialization, and Networking issues at  http://social.msdn.microsoft.com/Forums/en-US/wcf/threads where you can contact Windows Communication Foundation, Serialization, and Networking experts.

      Sincerely,

      Jason Wang


     


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 12, 2013 5:18 AM

All replies