为什么 TCP/IP 服务器在 java 中的侦听端口不工作
Why TCP/IP server with listening port in java not working
我在 java 中编写了一个代码,用于将我的计算机与发射器连接,发射器设备,通信板已经实现并准备好通过 TCP/IP 连接到具有特定地址的任何服务器IP(例如 192.168.2.2)并侦听特定端口号(例如 4000)。
我遵循了如何在 Java 中创建服务器端应用程序的确切步骤,提供了一个监听端口,以便我可以连接到那个发射器。
我不明白为什么当我尝试调试代码时,它会阻塞 clientSocket = serverSocket.accept()
行并抛出超时异常。
有人可以帮我找出我的代码中可能存在错误的地方吗?
任何帮助将不胜感激。
谢谢
代码如下:
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declares server and client socket, as well as the input and the output stream
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out;
//BufferedReader in;
BufferedReader in;
try{
InetAddress addr = InetAddress.getByName("192.168.2.2");
//Opens a server socket on port 4000
serverSocket = new ServerSocket(4000) ;
//Sets the timeout
serverSocket.setSoTimeout(30000);
System.out.println("Server has connected");
//Create a connection to server
System.out.println("Server listening connection from client ....");
//Listens and waits for client's connection to the server
clientSocket = serverSocket.accept();
// Creates input and output streams to socket
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Reads response from socket
while((in.readLine())!= null ){
System.out.println ( in.readLine() );
}
System.out.println ( "Closing connection ....");
//Terminates connection
clientSocket.close();
serverSocket.close();
System.out.println("Connecton successfully closed");
}
catch(IOException e){
System.out.println(e);
}
}
}
您指定了 30 秒的超时时间,对吗? :
serverSocket.setSoTimeout(30000);
所以 30 秒后,无论是在调试器中停止还是在 运行 中停止,这都会超时并抛出异常。
Could someone help me find out where the error might be in my code?
您的代码中没有可能导致此问题的错误。很明显,您没有正确配置设备以连接到此服务器,或者设备未 运行,或者它没有连接,或者存在防火墙。调查一下。
但是:
InetAddress addr = InetAddress.getByName("192.168.2.2");
这是做什么用的?没用。
System.out.println("Server has connected");
这根本不是真的。服务器 尚未 连接。此时它所做的就是创建一个监听套接字。
while((in.readLine())!= null ){
您正在阅读一行并将其丢弃。
System.out.println ( in.readLine() );
在这里,您每 秒 打印一行,并丢弃所有奇数行。这个循环的正确写法是:
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
另请注意,此服务器只会为一个客户端提供服务,然后退出。从 accept()
到 clientSocket.close()
的所有内容都应该有一个循环,如果有多个设备,它应该为每个接受的套接字启动一个新线程来处理 I/O.
我在 java 中编写了一个代码,用于将我的计算机与发射器连接,发射器设备,通信板已经实现并准备好通过 TCP/IP 连接到具有特定地址的任何服务器IP(例如 192.168.2.2)并侦听特定端口号(例如 4000)。
我遵循了如何在 Java 中创建服务器端应用程序的确切步骤,提供了一个监听端口,以便我可以连接到那个发射器。
我不明白为什么当我尝试调试代码时,它会阻塞 clientSocket = serverSocket.accept()
行并抛出超时异常。
有人可以帮我找出我的代码中可能存在错误的地方吗? 任何帮助将不胜感激。 谢谢
代码如下:
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Declares server and client socket, as well as the input and the output stream
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out;
//BufferedReader in;
BufferedReader in;
try{
InetAddress addr = InetAddress.getByName("192.168.2.2");
//Opens a server socket on port 4000
serverSocket = new ServerSocket(4000) ;
//Sets the timeout
serverSocket.setSoTimeout(30000);
System.out.println("Server has connected");
//Create a connection to server
System.out.println("Server listening connection from client ....");
//Listens and waits for client's connection to the server
clientSocket = serverSocket.accept();
// Creates input and output streams to socket
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Reads response from socket
while((in.readLine())!= null ){
System.out.println ( in.readLine() );
}
System.out.println ( "Closing connection ....");
//Terminates connection
clientSocket.close();
serverSocket.close();
System.out.println("Connecton successfully closed");
}
catch(IOException e){
System.out.println(e);
}
}
}
您指定了 30 秒的超时时间,对吗? :
serverSocket.setSoTimeout(30000);
所以 30 秒后,无论是在调试器中停止还是在 运行 中停止,这都会超时并抛出异常。
Could someone help me find out where the error might be in my code?
您的代码中没有可能导致此问题的错误。很明显,您没有正确配置设备以连接到此服务器,或者设备未 运行,或者它没有连接,或者存在防火墙。调查一下。
但是:
InetAddress addr = InetAddress.getByName("192.168.2.2");
这是做什么用的?没用。
System.out.println("Server has connected");
这根本不是真的。服务器 尚未 连接。此时它所做的就是创建一个监听套接字。
while((in.readLine())!= null ){
您正在阅读一行并将其丢弃。
System.out.println ( in.readLine() );
在这里,您每 秒 打印一行,并丢弃所有奇数行。这个循环的正确写法是:
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
另请注意,此服务器只会为一个客户端提供服务,然后退出。从 accept()
到 clientSocket.close()
的所有内容都应该有一个循环,如果有多个设备,它应该为每个接受的套接字启动一个新线程来处理 I/O.