Click here to Skip to main content
15,566,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends !

I am new In Android. I have developed an application related to Client -Socket Connection (TCP);

Server ------------ My PC (Desktop)

Client ------------ HTC Desire HD --- Android

Both Server & Client are in same Network, Ping is successfully between Them.

Server Is running well , But communication for Client -Server is not running well.

------------------------------- SERVER CODING --------------


Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;



public class TCPDesktopServer implements Runnable{

    public static final String SERVERIP = "127.0.0.1"; // My system's IP Address
    public static final int SERVERPORT = 8080;

    public void run() {
         try {
        	 System.out.println("S: Connecting...");
             ServerSocket serverSocket = new ServerSocket(SERVERPORT);
             while (true) {      	 
            	 Socket client = serverSocket.accept();
            	 System.out.println("S: Receiving...");

            	 try {

                      BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                      String str = in.readLine();
                      System.out.println("S: Received: '" + str + "'");
                    
            	 } catch(Exception e) {
                        System.out.println("S: Error");
                        e.printStackTrace();
                    
            	 } finally {
                    	client.close();
                        System.out.println("S: Done.");
                    }
             }       
         } catch (Exception e) {
             System.out.println("S: Error");
             e.printStackTrace();
         }
    }

    public static void main (String a[]) {

    	Thread desktopServerThread = new Thread(new TCPDesktopServer());
    	desktopServerThread.start();
    }
}

--------------------------------------- Client On Android Device-------




Java
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.util.Log;


public class TCPClient implements Runnable {
		
	
	public void run() {
         try {
        	 InetAddress serverAddr = InetAddress.getByName("127.0.0.1");
         	 Log.d("TCP", "C: Connecting...");
        	 Socket socket = new Socket(serverAddr, 8080);
        	 String message = "Hello from Client android emulator";

		     try {
		    	 Log.d("TCP", "C: Sending: '" + message + "'");
		    	 PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
		    	 out.println(message);
		    	 Log.d("TCP", "C: Sent.");
	             Log.d("TCP", "C: Done.");
		    	 
             } catch(Exception e) {
                 Log.e("TCP", "S: Error", e);

		     } finally {
		        socket.close();
		      }

         } catch (Exception e) {
              Log.e("TCP", "C: Error", e);
         }
    }
}


-------------------------
Posted
Updated 3-Oct-11 23:42pm
v3

Both your server and client are using the loopback address 127.0.0.1 which means they are communicating with themselves rather than each other. Your client should be calling the IP address of the server, and the server should be binding to address "any" to accept connections on any interface.
 
Share this answer
 
v2
Also, you have to grand permission for the App to access internet, by adding the code in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" xmlns:android="#unknown" />


http://android-er.blogspot.com/2011/01/simple-communication-using.html[^
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900