0

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);
       } 

}
}
apk legion
  • 21
  • 1
  • 9

1 Answers1

0

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.
}
  • It's not a duplicate question.... It was null due to the fact the other parts of the PrintWriter were out of the try catch block I added for testing the same socket connection error is still present – apk legion Jul 06 '18 at 00:08
  • 1
    @apklegion If the underlying problem was an NPE, which *you* stated above, it's a duplicate. It all results from your poorly structured code. Code that depends on the success of code in a prior `try` block should be inside that `try` block. – user207421 Jul 06 '18 at 01:01
  • @apklegion there is one thing which can help you in the future - when you are using a method which throws exception, don't catch it immediately, just write your code as you need it: firstly connect, then read, then at the far end, when you are interested if the whole operation succeeded or not - then catch accumulated exceptions, just to know what to display to user or if you should take some action. If you are catching exception only to log it to the console and don't do any further actions - mostly this is a signal, you catch it to early. – Jarosław Wiśniewski Jul 06 '18 at 07:16
  • for future readers, I figured it out it was the firewall blocking in the java program from running simple fix. the code is good to go. – apk legion Jul 06 '18 at 15:10