Java Socket / Serversocket WAN连接 [英] Java Socket/Serversocket WAN Connection

查看:160
本文介绍了Java Socket / Serversocket WAN连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的计算机中取出服务器,以便来自其计算机的客户端可以连接并与我的计算机通信。我在端口31350上建立了服务器,客户端正试图通过我的路由器的IP地址连接。但是当我在创建套接字的参数中有localhost或我的计算机名称时,它只能通过局域网工作。而不是当我使用我的IP地址,在不同的网络上运行客户端和服务器。这是代码。

Im trying to make a server out of my computer so that clients from their computers can connect and communicate with my computer. I made the server on port 31350 and the client is trying to connect through my router's ip address. But it only works through lan when I have "localhost" or the name of my computer in the parameters for the socket creation. and not when I use my ip address, running the client and server on different networks. Here is the code.

这是我的电脑正在运行的服务器。

Here is the server that my computer is running.

public static void main (String[] args) throws IOException
{
    ServerSocket server = new ServerSocket(31350);
    Socket client1 = server.accept();   
}

这是我的朋友在他的电脑上运行的客户端代码

Here is the client code that my friend is running on his computer

public static void main(String[] args) throws IOException, UnknownHostException
{
    Socket socket;

    //  #'s are what I got from whatismyip.org on the server computer)
    byte[] serverb = new byte[] {(byte)##, (byte)##, (byte)###, (byte)###};

    socket = new Socket(InetAddress.getByAddress(serverb),31350);
}

这就是我运行客户端时的说法

This is what it says when I run the client

线程main中的异常java.net.ConnectException:连接超时:在java.net.DualStackPlainSocketImpl.connect0(本机方法)连接
在java.net上
.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect( SocksSocketImpl.java:391)
在java.net.Socket.connect(Socket.java:579)
在java.net.Socket.connect(Socket.java:528)
在java .net.Socket。(Socket.java:425)
at java.net.Socket。(Socket.java:241)
at ClientTest.main(ClientTest .java:22)// 22是行socket = new Socket(InetAddress.getByAddress(serverb),31350);

Exception in thread "main" java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.(Socket.java:425) at java.net.Socket.(Socket.java:241) at ClientTest.main(ClientTest.java:22) // 22 is the line socket = new Socket(InetAddress.getByAddress(serverb),31350);

防火墙被禁用。我的路由器上的端口31350被转发到我在cmd中使用ipconfig获得的计算机的本地IP地址。但它仍然无法正常工作,我只是在尝试从客户端计算机创建套接字时获得IOException。服务器计算机上没有任何操作。怎么了?

firewalls are disabled. the port 31350 on my router is forwarded to my computer's local ip address that I got from using ipconfig in cmd. But it still doesnt work, I just get IOException when trying to create the socket from the client computer. Nothing happens on the server computer. What's wrong?

推荐答案

[更新]

正如您所料,连接超时表明它是某种网络问题。来自客户端的数据包未到达服务器计算机。确切的解决方案将取决于路由器的类型,但谷歌的术语是端口转发。以下是我随机发现的可能有用的文章: http://www.rhinosoft .com / KnowledgeBase / kbarticle.asp?RefNo = 1289

As you might expect, connection timed out indicates it's some kind of a network problem. The packets from your client are not arriving at the server machine. The exact solution will depend on the type of router, but the term to google for is "port forwarding". Here is an article I found at random that might help: http://www.rhinosoft.com/KnowledgeBase/kbarticle.asp?RefNo=1289

基本上,您对路由器进行编程,以便将端口31350上的任何连接请求转发到您的计算机在局域网上的本地IP地址。

Basically you program the router so that any connection request at port 31350 will be forwarded to your computer on the lan at it's local IP address.

祝你好运!

[原始评论]

这更多的是评论而不是答案(但我需要额外的空间)。您的try catch逻辑将使诊断问题变得更加困难。简化代码如下:

This is more of a comment than an answer (but I needed the extra room). Your try catch logic is going to make it much more difficult to diagnose the problem. Simplify the code as follows:

public static void main(String[] args) throws IOException, UnknownHostException
{
    Socket socket;

            //  #'s are what I got from whatismyip.org on the server computer)
    byte[] serverb = new byte[] {(byte)##, (byte)##, (byte)###, (byte)###};

    socket = new Socket(InetAddress.getByAddress(serverb),31350);
}

让原始IOException传播并更新你的问题以包含异常堆栈跟踪。原始异常包含有价值的信息 - 如果它说连接被拒绝则意味着一件事 - 也许你的端口号是不正确的。如果它说连接超时则意味着其他东西 - 要么你确实遇到防火墙问题,要么你的IP地址错误。

Just let the original IOException propagate and update your question to include the exception stack trace. The original exception contains valuable information - if it says connection refused it means one thing - perhaps your port number is incorrect. If it says connection timed out it means something else - either you really do have a firewall problem or perhaps your ip address is wrong.

你的代码正在捕获有用的异常,吞下它并抛出一个不那么有用的例外。

Your code is catching the useful exception, swallowing it and throwing a much less useful exception.

对你的服务器代码做同样的事情:

Do the same thing to your server code:

public static void main (String[] args) throws IOException
{
    ServerSocket server = new ServerSocket(31350);
    Socket client1 = client1 = server.accept();
}

堆栈跟踪将显示抛出异常的方法,因此您不需要像 InetAddress创建失败的冗余文本

The stack trace will show which method threw the exception so you don't need redundant text like InetAddress creation failed

这篇关于Java Socket / Serversocket WAN连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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