通过 LAN 使用 TCP/IP 进行 Java 聊天 [英] Java Chat with TCP/IP over LAN

查看:38
本文介绍了通过 LAN 使用 TCP/IP 进行 Java 聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,用于在 Java 中使用 TCP/IP 进行聊天.到目前为止,它在同一台机器上运行服务器和客户端时完成了这项工作,但是我想让它通过 LAN 工作.

I have developed an application for chatting using TCP/IP in Java. So far it does the job when running the server and clients on the same machine, however I want to make it work over LAN.

我发现我将在我的路由器上使用端口转发,与我在客户端和客户端中使用的端口相同.服务器并将其转发到我机器的 IP(即服务器).我还发现我应该小心防火墙.

I have found out that I shall be using port forwarding on my router, for the same port that I am using in the clients & server and forward it to IP of my machine (that will be the server). Also I found out that I shall be careful of the firewalls.

在我的原始媒体集线器路由器上,我使用 (4444) 将端口转发到我机器的本地 IP (192.168.0.21),使用 TCP 协议.我还确保没有端口被阻止.

On my virgin media hub router I have port forwarded the port im using (4444), with protocol TCP to the local IP of my machine (192.168.0.21). I have also made sure that no port is blocked.

对于防火墙,我已确保未启用 Windows 防火墙并关闭了我的卡巴斯基防病毒防火墙.

For firewalls, I have made sure that the windows firewall is not enabled and switched off my kaspersky anti-virus firewall.

到目前为止,这还不允许我通过 LAN 与我的 vmware 机器进行通信.

So far this didn't allow me yet to have a communication over the LAN, with my vmware machine.

这里是socket和server socket的代码;

Here is the code for socket and server socket;

客户:

int portNumber = 4444;
InetAddress host = InetAddress.getLocalHost(); // I also did try changing the host to a String and making host = InetAddress.getLocalHost().getHostAddress();
Socket link = new Socket(host, portNumber);

服务器:

int portNumber = 4444;
ServerSocket serverSocket = new ServerSocket(portNumber);
link = serverSocket.accept();

知道我做错了什么或遗漏了什么吗?

Any idea what I'm doing wrong, or missing something?

推荐答案

使用该代码 (InetAddress host = InetAddress.getLocalHost();),您的客户端将始终联系 localhost,而这显然不是什么你想要(但解释为什么它在本地工作......

With that code (InetAddress host = InetAddress.getLocalHost();) your clientside will always contact localhost, and that's obviously not what you want (but an explanation why it works locally...

我可以在客户端上推荐官方 Oracle 教程-服务器通信?

May I suggest the official Oracle tutorial on client-server communication?

假设您的远程系统的 IP 地址是 192.168.0.100,您的代码将是

Assuming your remote system's IP address is 192.168.0.100, your code will be

int portNumber = 4444;
String host = "192.168.0.100"; // Socket also has a constructor that accepts
                               // as String where you can either input a hostname or
                               // an IP address
Socket link = new Socket(host, portNumber);

Socket Javadoc 这里

Socket Javadoc here

你的程序的主要缺陷是你使用了 InetAddress.getLocalHost() 根据 Javadoc

the major flaw in your program is that you use InetAddress.getLocalHost() which according to the Javadoc

返回本地主机的地址.这是通过从系统检索主机的名称,然后将该名称解析为 InetAddress 来实现的.

Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.

强调我的.所以是的,您的程序可以在本地计算机上运行,​​但在远程计算机上不能.我的答案的本质是不是我使用字符串,而是我使用本地主机...

Emphasis mine. So yes, your program will work with the local computer but not with remote computers. The essential of my answer is not that I use a String, but that I don't use localhost...

这篇关于通过 LAN 使用 TCP/IP 进行 Java 聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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