数据报包和套接字的未知错误 - Java Networking
嘿,我一直在研究数据报类,但我只是不明白为什么我的“服务器”不会从“客户端”接收数据包。
我已经用同时在我自己的 PC 上运行的服务器和客户端对其进行了测试,并且它工作得很好,但是如果我尝试将服务器移到另一台 PC,它......不会。
现在我知道我一定是在套接字/地址/端口方面做错了什么......我以前没有使用过网络,所以我了解不多。
这是服务器代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.UnknownHostException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.sql.Connection;
import java.util.ArrayList;
public class ServerThread extends Thread {
private boolean needsToRun;
private DatagramSocket socket;
public ServerThread() {
super();
needsToRun = true;
try {
socket = new DatagramSocket(4446);
}
catch (SocketException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
while(needsToRun) {
byte[] buf = new byte[265];
DatagramPacket packet = new DatagramPacket(buf,buf.length);
try {
socket.receive(packet);
}
catch(IOException e) {
e.printStackTrace();
}
String data = new String(packet.getData(),0,packet.getLength());
if(data != null)
System.out.println(data);
}
socket.close();
}
}
您会看到一些未使用的导入,但我只是剥离了代码以使其变得基本(其中大部分来自我实际上希望服务器在收到数据后对其进行处理,但我已经让那部分工作)。
这是客户端代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
// get a datagram socket
DatagramSocket socket = new DatagramSocket();
// send request
String testInfo = "Hi";
byte[] buf = testInfo.getBytes();
/*This could possible be a point at which the code won't work.
*to get the bytes of the IP address of the computer I'm trying to run the server
*on I just did
* byte[] address = InetAddress.getLocalHost().getAddress();
* on the computer I was running the server on. I'm 95% sure that the IP addresses
* match.
*/
byte[] inet = {-64,-88,1,5};
InetAddress address = InetAddress.getByAddress(inet);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4446);
try {
socket.send(packet);
}
catch(Exception e) {
e.printStackTrace();
}
socket.close();
}
}
我已经检查过代码的明显部分,例如端口和地址(请参阅代码内注释)。
所以我真正要寻找的是,如果您不明白为什么代码无法工作,那么您能告诉我一些端口可能出现问题的事情以及诸如此类的事情(端口可能被关闭等(除了我)我也相当确定端口是开放的))。
感谢您的任何建议。
Hey, I've been playing around with the Datagram classes and I just can't figure out why my "server" won't receive packets from the "client".
I've tested it with the server and client both running on my own PC at once and it works perfectly, but if I try to move the server over to another PC, it...doesnt.
Now I know I must be doing something wrong with sockets/addresses/ports bleh...I haven't worked with networking before so I don't know much.
Here's the server code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.net.UnknownHostException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.sql.Connection;
import java.util.ArrayList;
public class ServerThread extends Thread {
private boolean needsToRun;
private DatagramSocket socket;
public ServerThread() {
super();
needsToRun = true;
try {
socket = new DatagramSocket(4446);
}
catch (SocketException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
while(needsToRun) {
byte[] buf = new byte[265];
DatagramPacket packet = new DatagramPacket(buf,buf.length);
try {
socket.receive(packet);
}
catch(IOException e) {
e.printStackTrace();
}
String data = new String(packet.getData(),0,packet.getLength());
if(data != null)
System.out.println(data);
}
socket.close();
}
}
You'll see some unused imports, but I just stripped down the code to make it basic (most of it came from what I actually want the server to do with the data once it is received, but I've got that part working).
Here's the client code:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Main {
public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
// get a datagram socket
DatagramSocket socket = new DatagramSocket();
// send request
String testInfo = "Hi";
byte[] buf = testInfo.getBytes();
/*This could possible be a point at which the code won't work.
*to get the bytes of the IP address of the computer I'm trying to run the server
*on I just did
* byte[] address = InetAddress.getLocalHost().getAddress();
* on the computer I was running the server on. I'm 95% sure that the IP addresses
* match.
*/
byte[] inet = {-64,-88,1,5};
InetAddress address = InetAddress.getByAddress(inet);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4446);
try {
socket.send(packet);
}
catch(Exception e) {
e.printStackTrace();
}
socket.close();
}
}
Obvious parts of the code, I've checked, like the port and address (see in-code comment).
So what I'm really looking for is, if you don't see why the code wouldnt be working, then could you tell me some things that may go wrong with the ports and whatnot (port might be closed, etc. (except I'm also fairly certain the port is open)).
Thanks for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我预计这是某种网络或防火墙问题,而不是 Java 客户端和服务器应用程序的问题。 (他们在同一台机器上运行时可以通话的证据表明了这一点……)
检查这些事情:
检查客户端是否可以解析服务器的 IP 地址;例如,尝试对它执行 ping 操作,或者在已知服务器支持的其他端口上连接到它。
检查客户端和服务器计算机上的软件防火墙是否允许端口 4446 上的 UDP 流量。
如果有桥接器并且/或两台计算机之间的路由器,检查它是否没有阻止端口 4446 上的 UDP 流量。
I expect that this some kind of network or firewall issue, rather than an issue with your Java client and server applications. (The evidence that they can talk when run on the same machine points that way ...)
Check these things:
Check that the client can resolve the server's IP address; e.g. try pinging it or connecting to it on some other port that is known to be supported by the server.
Check that the software firewalls on client and server machines allow UDP traffic on port 4446.
If there is a bridge and/or router between the two machines, check that it is not blocking UDP traffic on port 4446.