Hi,
 I am working on an application in which trying to make connection
between two emulators through socket programing. But getting an
exception

Code for Server:
public class TCPserverActivity extends Activity
{
                    private TextView serverStatus;
                    // default ip
                    public static String SERVERIP = "192.168.1.13";
                    // designate a port
                    public static final int SERVERPORT = 4048;
                    private Handler handler = new Handler();
                    private ServerSocket serverSocket;
                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);
                        serverStatus = (TextView) 
findViewById(R.id.serverStatus);
                //        SERVERIP = getLocalIpAddress();
                        Thread fst = new Thread(new ServerThread());
                        fst.start();
                    }
                    public class ServerThread implements Runnable
                    {
                        public void run()
                        {
                            try
                            {
                                if (SERVERIP != null)
                                {
                                    handler.post(new Runnable()
                                    {
                                        public void run()
                                        {
                                     serverStatus.setText("Listening on IP: "
+ SERVERIP);
                                        }
                                    });
                                    serverSocket = new ServerSocket(SERVERPORT);
                                    while (true)
                                    {
                                        // listen for incoming clients
                                        Socket client = serverSocket.accept();
                                        handler.post(new Runnable()
                                        {
                                            public void run()
                                            {
                                                
serverStatus.setText("Connected.");
                                            }
                                        });

                                        try
                                        {
                                            BufferedReader in = new
BufferedReader(new InputStreamReader(client.getInputStream()));
                                            String line = null;
                                            while ((line = in.readLine()) != 
null)
                                            {
        
System.out.println("ServerActivity--->>"+ line);
                                                handler.post(new Runnable()
                                                {
                                                    public void run()
                                                    {
                                                        // do whatever you want 
to
the front end
                                                        // this is where you 
can be
creative
                                                    }
                                                });
                                            }
                                            break;
                                        }
                                        catch (Exception e)
                                        {
                                            handler.post(new Runnable()
                                            {
                                                public void run()
                                                {
                                                    serverStatus.setText("Oops.
Connection interrupted. Please reconnect your phones.");
                                                }
                                            });
                                            e.printStackTrace();
                                        }
                                    }
                                }
                                else
                                {
                                    handler.post(new Runnable()
                                    {
                                        public void run()
                                        {
                                            serverStatus.setText("Couldn't 
detect
internet connection.");
                                        }
                                    });
                                }
                            }
                            catch (Exception e)
                            {
                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {
                                        serverStatus.setText("Error");
                                    }
                                });
                                e.printStackTrace();
                            }
                        }
                    }
                    // gets the ip address of your phone's network
                    /*
                    private String getLocalIpAddress()
                    {
                        try {
                            for (Enumeration<NetworkInterface> en =
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                                NetworkInterface intf = en.nextElement();
                                for (Enumeration<InetAddress> enumIpAddr =
intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                                    InetAddress inetAddress =
enumIpAddr.nextElement();
                                    if (!inetAddress.isLoopbackAddress()) { 
return
inetAddress.getHostAddress().toString(); }
                                }
                            }
                        } catch (SocketException ex) {
                            Log.e("ServerActivity", ex.toString());
                        }
                        return null;
                    }
                  */
                    @Override
                    protected void onStop() {
                        super.onStop();
                        try {
                             // make sure you close the socket upon exiting
                             serverSocket.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                    }

                }


Code For Client :

public class ConnectionTCPActivity extends Activity
{

            private EditText serverIp;
            private Button connectPhones;
            private String serverIpAddress = "";
            private boolean connected = false;
            private Handler handler = new Handler();
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                serverIp = (EditText) findViewById(R.id.serverIp);
                connectPhones = (Button) findViewById(R.id.connectPhones);
                connectPhones.setOnClickListener(connectListener);

            }
            private android.view.View.OnClickListener connectListener = new
android.view.View.OnClickListener()
            {
                        public void onClick(View v)
                {
                    if (!connected)
                    {
                        serverIpAddress = serverIp.getText().toString();
                        if (!serverIpAddress.equals(""))
                        {
                            Thread cThread = new Thread(new ClientThread());
                            cThread.start();
                        }
                    }
                }

                        public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub

                        }
            };
            public class ClientThread implements Runnable
            {
                public void run()
                {
                    try
                    {
                        InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
                        System.out.println("ClientActivity C:
Connecting...");
                     //   Socket socket = new Socket(serverAddr,
ServerActivity.SERVERPORT);
                        Socket socket = new Socket(serverAddr, 4048);
                        connected = true;
                        while (connected)
                        {
                            try
                            {
                                System.out.println("ClientActivity C: Sending
command.");
                                PrintWriter out = new PrintWriter(new
BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
true);
                                    // where you issue the commands
                                    out.println("Hey Server!");
                                    System.out.println("ClientActivity C:
Sent.");
                            }
                            catch (Exception e)
                            {
                                System.out.println("ClientActivity S:
Error-----"+ e);
                            }
                        }
                        socket.close();
                        System.out.println("ClientActivity C: Closed.");
                    }
                    catch (Exception e)
                    {
                        System.out.println("C: Error-----"+e);
                        connected = false;
                    }
                }
            }
        }


Please help me
Thanks in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to