Why does my client Socket not connect to my ServerSocket?

别来无恙 提交于 2020-01-16 04:34:07

问题


In this extremely basic client/server socket program, why does my client socket never connect and throw java.net.ConnectException? I am running the MessageServer program on one computer, and the ClientServer program on another laptop on the same network. I have verified that the local-ip of the computer which is running the server program is 10.0.0.1 using the ipconfig command in the windows cmd on that computer.

Server:

package server;

import java.net.*;
import java.io.*;

public class MessageServer {

    public static void main(String args[]) {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(4302);
        }
        catch (Exception e) {
            System.out.println("well bad news...");
        }

        boolean noConnection = true;
        while(noConnection == true) { 
            try {
                Socket client = serverSocket.accept();
                System.out.println("socket connection accepted:" + client.getRemoteSocketAddress().toString());
                noConnection = false;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("fail");
            }

    }
    }
}

Client:

import java.net.*;
import java.io.*;

    public class MessageClient {
        public static void main(String args[]) {
            Socket clientSocket = null;
            String recieve;
            try {
                clientSocket = new Socket("10.0.0.1", 4302);
            } catch (Exception e) {
                System.out.println("deal with it...");
                e.printStackTrace();
            }


        }
    }

When I run these 2 programs, i get the following result on the client machine:

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 MessageClient.main(MessageClient.java:10)

No issues on the server machine.

EDIT: I tried disabling my internet security (Norton 360), didn't help. Also, I am running (on both computers) Windows 10, Java 8 Update 65, Eclipse IDE Mars 1

EDIT 2: In case it helps, I tried running both parts of the program on one computer, this works. All signs point to a windows firewall issue. I will try to get to this as soon as possible.


回答1:


It doesn't matter what is the local ip of your computer. You server is running on localhost and hence you need to connect to localhost.

Change this to

clientSocket = new Socket("10.0.0.1", 4302);

to

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

When you host your Server on a VPS then you can change the localhost to the IP of the VPS.

You can also use "127.0.0.1" in place of "localhost".

EDIT According to the exception you are getting now,

The exception occurs when server is not listening on the port you are trying to connect to.

Either you have not started your server or server is not accepting connections or the port numbers on client and server are different.

EDIT 2

Since you are running Client and Server on different PC's (assuming different networks) you will need to port forward your Router to accept connections.

More about port forwarding here

Additionally you can use this website for helping you with port forwarding. Port Forwarding




回答2:


Ok, so I finally figured this problem out. Just putting this answer out there for anyone who runs into the same problem. When creating the client socket you need to directly specify the local ipv4 of the machine its connecting too. For some reason, local host will not allow it to connect and says the connection is refused for LAN connections. So EX Code:

Socket client = new Socket("10.0.0.8", 5566); 

instead of:

Socket client = new Socket("localhost" 5566);

If you are looking for the ipv4, go on the server machine, open the CMD (windows), and type ipconfig. Then under Ethernet adapter Local Area Connection: use the address listed under IPv4 Adress.



来源:https://stackoverflow.com/questions/33468332/why-does-my-client-socket-not-connect-to-my-serversocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!