locked
TCP socket between C# server and Android 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);}
    	    }
    	}


    Tuesday, March 12, 2013 2:27 PM

Answers

  • Your emulator emulates  hardware, so for the APP 127.0.0.1 is still the android machine.

    After a little search I found that the hosting machine of the emulator is reachable via IP address 10.0.2.2.

    But I think your also better of asking Android question in a android development forum or on stackoverflow.

    • Proposed as answer by cheong00 Wednesday, March 13, 2013 8:49 AM
    • Marked as answer by Belal Shakatra Wednesday, March 13, 2013 1:12 PM
    Wednesday, March 13, 2013 8:00 AM

All replies

  • Need clarification. You're saying you want to connect C# server and Android, right? Why does the "supposed to be Android code" want to connect to localhost?

    It won't work that way unless your "server" also run on Android.

    After changing that, if you still can't make it to connect, try verify to see if there's any routing / firewall related issues.

    Btw, Oracle does provide pretty good sample code you can reference to create a TCP client. You're encouraged to read.


    • Edited by cheong00 Wednesday, March 13, 2013 6:59 AM
    Wednesday, March 13, 2013 6:56 AM
  • I'm using "127.0.0.1" because I tested android app on emulator(that is meaning on the same machine).

    About firewall I turned off it since along time.

    Finally the link your suggested it doesn't open at my PC.

    So, could anyone help me .. please :(


    Belal

    Wednesday, March 13, 2013 7:22 AM
  • Your emulator emulates  hardware, so for the APP 127.0.0.1 is still the android machine.

    After a little search I found that the hosting machine of the emulator is reachable via IP address 10.0.2.2.

    But I think your also better of asking Android question in a android development forum or on stackoverflow.

    • Proposed as answer by cheong00 Wednesday, March 13, 2013 8:49 AM
    • Marked as answer by Belal Shakatra Wednesday, March 13, 2013 1:12 PM
    Wednesday, March 13, 2013 8:00 AM
  • The W3C validator has access to the page so I'm pretty confident that it's online.

    Many Java official documentation are hosted on www.oracle.com now, so a Java firm is unlikely to block it.

    So strange.

    Wednesday, March 13, 2013 8:49 AM