Java Socket 端口转发 [英] Java Socket port forwarding

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

问题描述

我使用 Sockets 用 Ja​​va 制作了一个网络游戏.它工作得很好,除了...只能通过我的局域网.:/我想要做的是设置它,以便我可以启动在我的家用计算机上运行的服务器,并将客户端代码发送到一个可执行的 jar 中,给其他人,让他们能够启动它和通过互联网连接.但就像...我不知道如何设置我的调制解调器 &路由器.:(我有一个无线透明调制解调器,其配置页面如下所示:

I made a networked game in Java using Sockets. It works great, except... only through my LAN. :/ What I want to be able to do is set it up so that I can start the server running on my home computer and send the client code, in an executable jar, to someone else, and have them be able to launch it and connect through the internet. But like... I have no idea how to set up my modem & router for that. :( I have a wireless Clear modem, for which the configuration page looks like this:

http://imageshack.us/photo/my-images/254/调制解调器.jpg

还有一个 Netgear 路由器,其页面如下所示:

And a Netgear router, whose page looks like this:

http://imageshack.us/photo/my-images/443/路由器q.jpg

现在,在客户端和服务器运行器类中,我将我 PC 的私有 IP 地址以及我选择的端口号传递给 ServerSocket 和 Socket 对象.我听说套接字可以是 UDP 或 TCP,这取决于您如何设置它们,或者……?不过,我不做任何类似的事情,我只是实例化它们并向它们传递 2 个值,然后它们就开始了......

Right now, in the client and server runner classes, I pass my PC's private IP address along with my chosen port number to the ServerSocket and Socket objects. I hear that Sockets are able to be either UDP or TCP, depending on how you set them up, or something...? I don't do anything like that though, I just instantiate them and pass them the 2 values, and off they go...

在服务器代码中:

ServerSocket sock = new ServerSocket();
sock.bind(new InetSocketAddress(IP, 9001));

客户端代码:

Socket sock = new Socket(IP, 9001);

因此,我还需要知道如何根据路由器/调制解调器设置的更改来相应地更改代码.

So also, I would need to know how to change the code accordingly with the changes to my router/modem settings.

"IP" 是一个字符串,代表我的计算机的私有 IP,192.168.1.10,只有当这是我传递给 Socket & 的字符串时,程序才能工作.服务器套接字.就像我说的,我有一个调制解调器,它连接到连接到我的计算机的路由器.那么,有人可以向我解释一下(详细说明,因为我是菜鸟)我到底需要做什么来配置它们,以及我必须对我的代码进行哪些更改?

"IP" is a String representing my computer's private IP, 192.168.1.10, and the program only works if that is the string I pass to the Socket & ServerSocket. Like I said, I have a modem which is connected to a router which is connected to my computer. So, can someone please explain to me (in detail, because I'm a noob) what exactly I would have to do to configure both of them, AND the changes I would have to make to my code?

推荐答案

听起来你在这里问了很多问题......我不是网络/套接字编程专家,但这里有一些想法.

It sounds like you're asking a number of questions here... I'm no network/socket programming specialist, but here's some ideas.

对于分发,您可能希望查看 Java网络启动.这将使您的客户轻松获取应用程序并自动更新.

For distribution, you may wish to look into Java Web Start. It'll make it easy for your clients to obtain the app as well as automating updates.

如果您使用这样的 Socket 构造函数,则实际实现将是系统默认值.您可以通过调用 此方法 具有合适的SocketImplFactory 实现.

If you're using the Socket constructor like that, the actual implementation will be a system default. You could override this by calling this method with a suitable SocketImplFactory implementation.

至于 IP 地址和端口...使用硬编码的端口值应该没问题.您需要决定一个端口或至少一些默认值,以便客户端连接.即便如此,从某些外部配置文件中读取值可能很有用.如果您决定分发服务器应用程序以让其他人运行服务器,这将使事情变得更容易.他们可能想使用不同的端口.

As for the IP address and port... Using a hard-coded value for the port should be okay. You'll need to decide on a port or at least some default anyway for clients to connect to. Even so, it might be useful to have the value read from some external configuration file. This'll make it easier should you ever decide to distribute the server app to let other people run servers. They might want to use a different port.

将服务器的 IP 地址(您的)硬编码在代码中绝对是禁忌.客户端应使用需要提供或在某些配置文件中设置的 IP 地址或主机名进行连接.如果您没有静态 IP 地址,则需要主机名.

Having the server's IP address (yours) hard-coded in code is definitely a no-no, though. Clients should connect using either an IP address or host name that they need to provide or set in some configuration file. A host name would be required if you don't have a static IP address.

除非这是您和一些朋友喜欢的东西,否则您总是可以通过在聊天会话中提供 IP 和端口或其他方式来启动游戏,否则您最好在一家公司找到一些外部托管解决方案为您处理 DNS 解析和网络设置.现在,如果您没有 Java EE 服务器或数据库等要求,您会发现这样的东西非常便宜.

Unless this is something for you and some friends to enjoy, where you can always just initiate a game by providing IP and port in a chat session or something, you'd be better off finding some external hosting solution at a company that takes care of the DNS resolution and network setup for you. These days you'll find stuff like that pretty cheap if you don't have requirements like a Java EE server or database.

想到了别的东西.TCP 可能适合您的用例,但如果这是一款需要最小延迟和快速输入同步的游戏(如射击游戏或格斗游戏),那么它不是最佳选择.在这种情况下,UDP 会更好.这将导致需要添加某种检测不同步或一种机制来弥补任何丢弃的数据包.

thought of something else. TCP might be okay for your use-case, but if this is a game that requires minimal lag and quick input synchronization (like a shooter or fighting game) then it's not the best choice. In that case UDP would be better. It would induce the requirement of adding some sort of detection for desync or a mechanism that makes up for any dropped packets.

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

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