java.net.ConnectException:连接被拒绝的TCP

java.net.ConnectException:连接被拒绝的TCP,第1张

概述Objective–Iwanttosendtheenteredtextinthejava(PC)projecttotheandroidappwhichdisplaysthistext.ThePCisconnectedtowifihotspotcreatedbytheandroidmobile.PC/客户端Java项目代码:publicclassEcsDemo{publicstaticvoidm

Objective – I want to send the entered text in the java (PC) project to the androID app which displays this text.The PC is connected to wifi
hotspot created by the 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 @R_403_6889@=        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

谢谢!

解决方法:

您尝试连接的IP:端口没有监听.

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

总结

以上是内存溢出为你收集整理的java.net.ConnectException:连接被拒绝的TCP全部内容,希望文章能够帮你解决java.net.ConnectException:连接被拒绝的TCP所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存