1

I try to create a Simple TCP Server on Android phone and waiting for client. I only want to implement the connection between TCPServer and Client , it doesn't need to transmit any data.

I have the another application for client , It use to connect to this TCPServer. The code of TCPServerthread is like the following.

private class TCPServerThread implements Runnable
    {
        @Override
        public void run() {
            // TODO Auto-generated method stub

            try {
                ServerSocket serverSocket = new ServerSocket(PORT);

                //while loop
                while (true) {
                    Log.i(TAG, "TCPServerThread...while loop");
                    try {
                        Socket socket = serverSocket.accept();
                        Log.i(TAG, "TCPServerThread...socket.getInetAddress() = " + socket.getInetAddress());
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                        Log.i(TAG, "First IOException");
                    }
                }
                //while loop end

            } catch (Exception e) {
                // TODO: handle exception
                //e.printStackTrace();
                Log.i(TAG, "Second IOException");
            }
        }
    }

But it seems stop at Socket socket = serverSocket.accept(); and doesn't show the log of TCPServerThread...socket.getInetAddress() = when the client try to connect to this Server.

DO I missing something for TCPServer ?

Is it mean the client doesn't connect to the Server when the code stop at Socket socket = serverSocket.accept(); ??

----------------------------EDIT----------------------------------------

Update the process.

The Server(Android Phone) open the WiFi-Hot-Spot, it also open the TCP-Server like the above code.

After Client connect to WiFi-Hot-Spot , the Client and the Server are in the same network.

The Client will get a IP address of gateway, and the Client try to connect to this IP address of gateway by TCP.

So the connection port and Server address seems correct for Client.

9
  • 2
    If it does not pass serverSocket.accept() then no client connects. Find out why. Look in the client for errors. Please post the Client Logcat/stacktrace. Tell the setup you use. Enulator? Device? And so on.
    – greenapps
    Jul 9, 2014 at 12:44
  • @greenapps So the code of Server is correct ? and the problem is at Client ?
    – Martin
    Jul 9, 2014 at 12:55
  • If all server code is correct i do not know. But for now it is obvious that your client does not connect. That can be because your setup is not according the used urls in the client. First: tell your set up as i already asked. Second: post the LogCat of the client. I already asked for that too.
    – greenapps
    Jul 9, 2014 at 12:58
  • @greenapps The code of client is not for Android , so I can not post the code. But I download the Socket Protocol App from Play store. The client can connect to Socket Protocol App.
    – Martin
    Jul 9, 2014 at 13:15
  • 1
    I create the Soft-AP mode and TCP Server on my phone , and the client connect to my phone. And then start TCP connect. The port is 50006.
    – Martin
    Jul 9, 2014 at 13:49

1 Answer 1

0

Your code is correct, but it seems that no one is connecting to your TCPserver. To avoid this blocking situation on

     Socket socket = serverSocket.accept();

you have to set the timeout option for your socket when you declare it

     serverSocket.setSoTimeout(mTime);

;)

1
  • But I have using a client try to connect to this Server. I also use the same client connect to another TCPServer APP , and it can connect
    – Martin
    Jul 9, 2014 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.