套接字未将Android连接到pc tcp

时间:2018-07-05 15:32:48

标签: java android tcpclient tcpserver

我在这里不知所措。我有一个Android客户端试图连接PC上的服务器。这是未仿真的实际设备。在发送数据时,我得到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$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)

这是应用程序的代码

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

}
}

1 个答案:

答案 0 :(得分:0)

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

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