套接字未连接 Android 到 pc tcp

socket is not connected Android to pc tcp

我在这里不知所措。我有一个 Android 客户端试图连接我电脑上的服务器。它是未模拟的实际设备。在发送数据时,我收到 SocketExceptions。我连接到与 pc 相同的网络并且端口被转发。我不确定现在问题出在哪里。这是堆栈跟踪

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.call(AsyncTask.java:304)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor.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)

这是应用程序的代码

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



 }
 }

最后但同样重要的是pc端的服务器代码

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

}
}

提供的代码片段忽略了连接失败。请从记录当前被忽略的异常开始:

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.
}