java.net.ConnectException。拒绝TCP连接[英] java.net.ConnectException: Connection refused TCP

问题描述

目标 - 我想将Java(PC)项目中的输入文本发送到显示此文本的Android应用程序.PC连接到WiFi 由Android Mobile创建的热点.

pc/client java项目代码:

public class EcsDemo {

    public static void main(String[] args) {

        System.out.println("Enter SSID to connect :");
        Scanner in = new Scanner(System.in);
        String ssid = in.nextLine();
        System.out.println("You entered ssid " + ssid);
        System.out.println("Connecting to ssid ..");
        DosCommand.runCmd(DosCommand.connectToProfile(ssid));
        // netsh wlan connect name=
        System.out.println("initializing tcp client ..");

            try {
                TCPClient.startTCpClient();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

public class TCPClient {
  public static void startTCpClient() throws UnknownHostException, IOException{
      String FromServer;
      String ToServer;

      Socket clientSocket = new Socket("localhost", 5000);

      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
              System.in));

      PrintWriter outToServer = new PrintWriter(
              clientSocket.getOutputStream(), true);

      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
              clientSocket.getInputStream()));

      while (true) {

          FromServer = inFromServer.readLine();

          if (FromServer.equals("q") || FromServer.equals("Q")) {
              clientSocket.close();
              break;
          } else {
              System.out.println("RECIEVED:" + FromServer);
              System.out.println("SEND(Type Q or q to Quit):");

              ToServer = inFromUser.readLine();

              if (ToServer.equals("Q") || ToServer.equals("q")) {
                  outToServer.println(ToServer);
                  clientSocket.close();
                  break;
              } else {
                  outToServer.println(ToServer);
              }
          }
      }
  }
  }

android应用程序/服务器代码:

public class MainActivity extends Activity {

    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Log.i(TAG, "starting server");

        new ServerAsyncTask().execute();
    }
}

public class ServerAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            TCPServer.startTCPServer();// initTCPserver();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}


public static void startTCPServer() throws IOException{
    String fromclient;
    String toclient;

    ServerSocket Server = new ServerSocket(5000);

    System.out.println("TCPServer Waiting for client on port 5000");
    Log.i("startTCPServer","TCPServer Waiting for client on port 5000");

    while (true) {
        Socket connected = Server.accept();
        System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
                + ":" + connected.getPort() + " IS CONNECTED ");
        Log.i("startTCPServer"," THE CLIENT" + " " + connected.getInetAddress()
                + ":" + connected.getPort() + " IS CONNECTED ");

        BufferedReader inFromUser = new BufferedReader(
                new InputStreamReader(System.in));

        BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(connected.getInputStream()));

        PrintWriter outToClient = new PrintWriter(
                connected.getOutputStream(), true);

        while (true) {

//            System.out.println("SEND(Type Q or q to Quit):");
//            toclient = inFromUser.readLine();
//
//            if (toclient.equals("q") || toclient.equals("Q")) {
//                outToClient.println(toclient);
//                connected.close();
//                break;
//            } else {
//                outToClient.println(toclient);
//            }


            fromclient = inFromClient.readLine();

            if (fromclient.equals("q") || fromclient.equals("Q")) {
                connected.close();
                break;
            } else {
                System.out.println("RECIEVED:" + fromclient);
            }

        }

    }
}
}

运行Android应用程序后,然后运行Java项目时,我会收到以下异常:

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.expressecs.javademo.TCPClient.startTCpClient(TCPClient.java:15)
    at com.expressecs.javademo.EcsDemo.main(EcsDemo.java:41)

我已提到以下链接:

java.net.connectexception:连接拒绝

谢谢!

推荐答案

在ip上没有任何侦听:您尝试连接到的端口.

服务器没有启动,或者正在侦听不同的端口,或者绑定到127.0.0.1而不是0.0.0.0或公共IP地址.

本文地址:https://www.itbaoku.cn/post/978505.html