为什么我的客户端套接字无法连接到ServerSocket? [英] Why does my client Socket not connect to my ServerSocket?

查看:612
本文介绍了为什么我的客户端套接字无法连接到ServerSocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个极其基本的客户端/服务器套接字程序中,为什么我的客户端套接字从不连接并抛出java.net.ConnectException?我在一台计算机上运行MessageServer程序,并在同一网络上的另一台笔记本电脑上运行ClientServer程序.我已经使用Windows cmd上的ipconfig命令验证了运行服务器程序的计算机的本地IP为10.0.0.1.

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.

服务器:

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

    }
    }
}

客户:

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)

服务器上没有问题.

我尝试禁用Internet安全性(Norton 360),无济于事.另外,我正在两台计算机上运行Windows 10,Java 8 Update 65,Eclipse IDE Mars 1

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

如果有帮助,我尝试在一台计算机上运行程序的两个部分,这是可行的.所有迹象都指向Windows防火墙问题.我将尝试尽快解决这个问题.

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.

推荐答案

好,所以我终于弄清楚了这个问题.只需为遇到相同问题的任何人提供此答案.创建客户端套接字时,您还需要直接指定与其连接的计算机的本地ipv4.由于某些原因,本地主机将不允许其连接,并说该连接被LAN连接拒绝.所以EX代码:

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

代替:

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

如果要查找ipv4,请在服务器计算机上打开CMD(Windows),然后键入ipconfig.然后在Ethernet adapter Local Area Connection:下使用IPv4地址下列出的地址.

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.

这篇关于为什么我的客户端套接字无法连接到ServerSocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆