Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
private void runTcpServer() {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(TCP_SERVER_PORT);
            //ss.setSoTimeout(10000);
            //accept connections
            Socket s = ss.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            //receive a message
            String incomingMsg = in.readLine() + System.getProperty("line.separator");
            Log.i("TcpServer", "received: " + incomingMsg);
            text.append("received: " + incomingMsg);
            //send a message
            String outgoingMsg = "goodbye from port " + TCP_SERVER_PORT + System.getProperty("line.separator");
            out.write(outgoingMsg);
            out.flush();
            Log.i("TcpServer", "sent: " + outgoingMsg);
            text.append("sent: " + outgoingMsg);
            //SystemClock.sleep(5000);
            s.close();
        } catch (InterruptedIOException e) {
            //if timeout occurs
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

i try to run the runTcp methods the program work until Socket s = ss.accept();
then the application crashed

How can i fix this?
Posted
Updated 12-Dec-14 3:38am
v2
Comments
Afzaal Ahmad Zeeshan 12-Dec-14 9:45am    
While the application crashed, it must have given an error A.K.A Exception. Please provide that exception.
Member 10525430 12-Dec-14 9:58am    
Android program is crash.it says Unfortunalty the program determined or something like that:)
Afzaal Ahmad Zeeshan 12-Dec-14 10:20am    
And what is something like that. Because it is required, otherwise we can't help you out in this problem, please add that.
Member 10525430 12-Dec-14 10:41am    
It is not problem on android studio,its problem from emulator.When i run the app the emulator says unable problem on this.This program are stopped.
Afzaal Ahmad Zeeshan 12-Dec-14 10:50am    
See the console of the Android Studio for the details on the Error.

1 solution

You perform a networking operation on the main thread which is not allowed. For more details:
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html[^]

http://android-er.blogspot.nl/2012/04/androidosnetworkonmainthreadexception.html[^]

Good luck!
 
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