0

I'm working on a Android TCP/IP (Wifi) server. The serversocket is setup correctly( i think), but when i call the accept function then the app crashes. In the Catlog there is a unhandled acception but i catch the exception, at least i think i do.

Below is my TCPIP class, the method where it goes wrong is called setupserver. I see the print out of before accept, but not after accept. Does anybody have idea? All sugestions are welcome! Please let me know i need to supply more information

    public void RunServer(int PortNumber){
    // Try to setup server with given port number
    try {
        ServerSocket = new ServerSocket(PortNumber);
        System.out.println("Server set up");
    } 
    catch (IOException e) {
        Log.e("TCPIPCommunicator", "failed to setup server", e);
        e.printStackTrace();
    }

    // Wait for connection from client
    try {
        System.out.println("Before accept");
        ClientSocket = ServerSocket.accept();
        System.out.println("Ater accept");

    } 
    catch (IOException e) {
        Log.e("TCPIPCommunicator", "failed to accept", e);
        e.printStackTrace();
    }   


    while(true){

        //Send data

        //Recieve data

    }
}

The Catlog shows the following:

10-31 16:37:55.653: I/System.out(14525): Server set up 10-31 16:37:55.653: I/System.out(14525): Before accept 10-31 16:37:55.653: D/AndroidRuntime(14525): Shutting down VM 10-31 16:37:55.653: W/dalvikvm(14525): threadid=1: thread exiting with uncaught exception (group=0x411df2a0) 10-31 16:37:55.653: E/AndroidRuntime(14525): FATAL EXCEPTION: main 10-31 16:37:55.653: E/AndroidRuntime(14525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.communicationmodule/com.example.communicationmodule.MainActivity}: android.os.NetworkOnMainThreadException

Roy08
  • 253
  • 2
  • 7
  • 21
  • Yet another duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Chris Stratton Oct 31 '13 at 17:00

2 Answers2

1

Definitely do not launch a Server in the activity constructor. Here is an example to open a socket nicely.

Jo_
  • 56
  • 3
  • Thank you for your answer, i put the server code in a thread and the exception is gone. – Roy08 Nov 01 '13 at 10:37
0

You should use an infinitive loop in a thread to accept connections all the time ClientSocket = ServerSocket.accept(); this's completely wrong. Here's a little example.

public void listenSocket(){
  try{
    server = new ServerSocket(4444);
  } catch (IOException e) {
    System.out.println("Could not listen on port 4444");
    System.exit(-1);
  }
  while(true){
    ClientWorker w;
    try{
//server.accept returns a client connection
      w = new ClientWorker(server.accept(), textArea);
      Thread t = new Thread(w);
      t.start();
    } catch (IOException e) {
      System.out.println("Accept failed: 4444");
      System.exit(-1);
    }
  }
}

Refer this

Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
  • This is not relevant to the current actual problem, the android.os.NetworkOnMainThreadException. As a design issue, your suggestion can have merit, but it not always what is desired. Particularly in a device that is not generally intended to be used as a server, it may not be desired to routinely accept incoming connections, but rather only to do so on particular occasions. – Chris Stratton Oct 31 '13 at 17:02