1

I am establishing serial Communication between windows pc and android aap

here are the steps I followed used portforwading method,

in command prompt executed:

adb forward tcp:6666 tcp:6767

java code lines

 try {
            server = new ServerSocket();
            server.setReuseAddress(true);
            server.bind(new InetSocketAddress(6767));
            server.setSoTimeout(TIMEOUT * 1000);

            // attempt to accept a connection
            client = server.accept();
            Globals.socketIn = new Scanner(client.getInputStream());
            Globals.socketOut = new PrintWriter(client.getOutputStream(),
                    true);

            // Globals.socketIn.
        } catch (SocketTimeoutException e) {
            // print out TIMEOUT
            e.printStackTrace();
            connectionStatus = "Connection has timed out! Please try again";
            mHandler.post(showConnectionStatus);
        } catch (IOException e) {
            Log.e(TAG, "" + e);
        } finally {
            // close the server socket
            try {
                if (server != null)
                    server.close();
            } catch (IOException ec) {
                Log.e(TAG, "Cannot close server socket" + ec);
            }
        }

but getting timeout Exception. let me know what is problem with it.

here is my logcat

 09-25 16:57:59.299: W/System.err(20521): java.net.SocketTimeoutException
 09-25 16:57:59.299: W/System.err(20521):   at                            java.net.PlainSocketImpl.accept(PlainSocketImpl.java:108)
 09-25 16:57:59.304: W/System.err(20521):   at java.net.ServerSocket.implAccept(ServerSocket.java:202)
 09-25 16:57:59.304: W/System.err(20521):   at java.net.ServerSocket.accept(ServerSocket.java:127)
 09-25 16:57:59.304: W/System.err(20521):   at com.example.usbpccomminication.SocketMain$1.run(SocketMain.java:90)
 09-25 16:57:59.304: W/System.err(20521):   at java.lang.Thread.run(Thread.java:856)
 09-25 16:57:59.309: W/System.err(20521): Caused by: libcore.io.ErrnoException: accept failed: EAGAIN (Try again)  
 09-25 16:57:59.309: W/System.err(20521):   at libcore.io.Posix.accept(Native Method)
 09-25 16:57:59.309: W/System.err(20521):   at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
 09-25 16:57:59.309: W/System.err(20521):   at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
 09-25 16:57:59.314: W/System.err(20521):   ... 4 more
Naveed Ali
  • 2,599
  • 1
  • 19
  • 36

1 Answers1

-1

One way to effectively handle it is to define a connection timeout and later handle it by using a try catch block.Hope this will help.

HttpUrlConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000); //set the timeout in milliseconds

//This is the average time interval
Maveňツ
  • 9,147
  • 14
  • 53
  • 94