Search code examples
javaandroidtcpclienttcpserver

socket is not connected Android to pc tcp


I'm at a loss here. I have an Android client trying to connect a server on my pc. It is an actual device not emulated. On sending data I am getting SocketExceptions. I am connected to the same network as the pc and the port is forwarded. I'm not sure where the issue is now. heres the stacktrace

07-05 11:20:06.876 21128-21587/com.apklegion.pcnotetest W/System.err: 
java.net.SocketException: Socket is not connected
07-05 11:20:06.877 21128-21587/com.apklegion.pcnotetest W/System.err:     at 
java.net.Socket.getOutputStream(Socket.java:921)
07-05 11:20:06.878 21128-21587/com.apklegion.pcnotetest W/System.err:     at 
com.apklegion.pcnotetest.MainActivity$sendData.doInBackground(MainActivity.
java:80)        at  com.apklegion.pcnotetest.MainActivity$sendData.doInBackground
(MainActivity.java:61)
    at android.os.AsyncTask$2.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at     java.util.concurrent.ThreadPoolExecutor.runWorker
 (ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:607)
07-05 11:20:06.879 21128-21587/com.apklegion.pcnotetest W/System.err:     at 
java.lang.Thread.run(Thread.java:761)

here is the code for the app

  private static String ip = "10.0.0.3";
private static int port = 5555;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
         el = (EditText)findViewById(R.id.text);
         button = (Button)findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {




                 message = el.getText().toString();
              SendData sd = new sendData();
             sd.execute(message);

                 Toast.makeText(getApplicationContext(),"Data 
Sent",Toast.LENGTH_LONG).show();

Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
             }
         });

}

class SendData extends AsyncTask<String, Void, Void> {


 @Override
 protected Void doInBackground(String... voids) {

     String message = voids[0];
    try {
        Log.i(TAG, "Attempting to send...");
        s = new Socket();
        s.connect(new InetSocketAddress(ip,port));
        Log.i(TAG,"Socket connected...");
    }
    catch (IOException e) {
  e.printStackTrace();

    }


     try {
     pr = new PrintWriter(s.getOutputStream());
         Log.i(TAG,"Get Outstream...");
     error here-->     pr.write(message);
         Log.i(TAG,"writing message...");
         pr.flush();

         Log.i(TAG,"flushing...");
         pr.close();

     } catch (IOException e) {

         e.printStackTrace();
     }




         Log.i(TAG,"closing...");
     try {
         s.close();
         Log.i(TAG,"socket closed...");
     } catch (IOException e) {

         e.printStackTrace();
     }

     return null;
 }



 }
 }

and last but not least the server code on the pc side

public class Testtcp2 {

   private static  ServerSocket ss;
private static Socket s;
private  static BufferedReader bufferedreader;
private static InputStreamReader isr;
private static String message;



public static void main(String[] args) {
    try{
        ss = new ServerSocket(5555);

          while(true){
              s = ss.accept();
              isr = new InputStreamReader(s.getInputStream());
              bufferedreader = new BufferedReader(isr);
              message = bufferedreader.readLine();

              System.out.println(message);


       }






     }  catch (IOException ex) { 
           Logger.getLogger(Testtcp2.class.getName()).log(Level.SEVERE, null, 
ex);
       } 

}
}

Solution

  • The provided snippet of code ignores connection failures. Please start from logging the exception which is currently ignored:

    try {
        // ...
        s = new Socket();
        s.connect(new InetSocketAddress(ip,port));
        // ...
    }
    catch (IOException e) {
        // Here lays an answer for your issue, just log it and see the cause.
        e.printStackTrace();   // As suggested by @gabe-sechan.
    }