java.net.ConnectException:连接拒绝TCP

时间:2015-05-16 18:10:32

标签: java android tcp

  

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

PC /客户端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: Connection refused

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在尝试连接的IP端口没有任何内容。

您的服务器未启动,或正在侦听其他端口,或者绑定到127.0.0.1而不是0.0.0.0或公共IP地址。