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