Java Socket 端口转发

发布于 2024-12-16 01:52:01 字数 1126 浏览 2 评论 0原文

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

http ://imageshack.us/photo/my-images/254/mo​​dem.jpg

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

http://imageshack.us/photo/my-images/443/routerq.jpg

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

在服务器代码中:

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

客户端代码:

Socket sock = new Socket(IP, 9001);

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

“IP”是代表我的计算机的私有 IP 192.168.1.10 的字符串,并且该程序仅在该字符串时才有效。我传递给套接字&服务器套接字。就像我说的,我有一个调制解调器,它连接到一个连接到我的计算机的路由器。那么,有人可以向我解释一下(详细说明,因为我是菜鸟)我到底需要做什么来配置它们,以及我必须对代码进行的更改?

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/modem.jpg

And a Netgear router, whose page looks like this:

http://imageshack.us/photo/my-images/443/routerq.jpg

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...

In the code for the server:

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

Code for the client:

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" 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-12-23 01:52:01

您必须手动进行端口转发,或者查看 UPnP 或 NAT-PMP 等解决方案来实现自动端口转发。不幸的是,NAT 配置起来很麻烦,而且大多数路由器都不能正确支持它。这也不是一项简单的任务,因为您必须支持一系列不同的实现和机制。

不过,设置路由器将一个端口转发到您的计算机应该不会太困难。请参阅 http://portforward.com/ 了解如何在几乎任何路由器上执行此操作的精彩列表。

You'll have to manually port forward, or look at a solution like UPnP or NAT-PMP for automatic port forwarding. Unfortunately, NAT is a pain in the ass to configure and most routers don't properly support it. It's also not a simple task, as you have to support a whole range of different implementations and mechanisms.

It shouldn't be too difficult to set up your router to forward one port to your machine, though. See http://portforward.com/ for an awesome list of how to do it on pretty much any router.

我的鱼塘能养鲲 2024-12-23 01:52:01

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

对于分发,您可能希望查看 Java Web Start< /a>.它将使您的客户轻松获取应用程序并自动更新。

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

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

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

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

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

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

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.

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.

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.

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.

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.

EDIT: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文