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

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

问题描述

我使用套接字在Java中创建了一个联网游戏。它工作得很好,除了......只能通过我的局域网。 :/我想要做的是设置它,以便我可以启动在家用计算机上运行的服务器,并将客户端代码,在可执行的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/mo​​dem.jpg

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

And a Netgear router, whose page looks like this:

http://imageshack.us/photo/my-images/443/routerq.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);

所以,我还需要知道如何通过更改路由器来相应地更改代码/ modem设置。

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&的字符串时才有效。 ServerSocket的。就像我说的,我有一个调制解调器连接到一台连接到我的电脑的路由器。所以,有人可以向我解释(详细说明,因为我是菜鸟)我究竟需要做些什么来配置它们,以及我必须对我的代码进行哪些更改?

"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 Web Start 。这将使您的客户轻松获得应用程序以及自动更新。

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.

如果您使用套接字这样的构造函数,实际的实现将是系统默认的。你可以通过调用此方法,具有合适的 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会更好。这将导致需要为desync添加某种检测或者补偿任何丢弃数据包的机制。

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天全站免登陆