简单的 Java 客户端和服务器无法正确交互
我不明白为什么下面的代码不起作用。客户端向服务器发送消息,服务器将消息打印到标准输出。
服务器代码:
import java.net.*;
import java.io.*;
import java.math.BigInteger;
public class server
{
public static void main(String args[])
{
try
{
ServerSocket server = new ServerSocket(8080);
while (true)
{
// initializations
Socket connection = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream());
// listen for client message
String message = in.readLine();
// print raw message from client
System.out.println(message);
// close resources
if (out != null)
out.close();
if (in != null)
in.close();
if (connection != null)
connection.close();
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(1);
}
}
}
客户端代码:
import java.net.*;
import java.io.*;
import java.math.BigInteger;
public class client
{
public static void main(String args[])
{
try
{
// initializations
Socket connection = new Socket("localhost", 8080);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream());
// send message to server
out.println("Hello, world!");
// close resources
if (in != null)
in.close();
if (out != null)
out.close();
if (connection != null)
connection.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(1);
}
}
}
有什么见解吗?谢谢!
I can't understand why the code below doesn't work. The client sends a message to the server, and the server prints the message to standard output.
Code for the server:
import java.net.*;
import java.io.*;
import java.math.BigInteger;
public class server
{
public static void main(String args[])
{
try
{
ServerSocket server = new ServerSocket(8080);
while (true)
{
// initializations
Socket connection = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream());
// listen for client message
String message = in.readLine();
// print raw message from client
System.out.println(message);
// close resources
if (out != null)
out.close();
if (in != null)
in.close();
if (connection != null)
connection.close();
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(1);
}
}
}
Code for the client:
import java.net.*;
import java.io.*;
import java.math.BigInteger;
public class client
{
public static void main(String args[])
{
try
{
// initializations
Socket connection = new Socket("localhost", 8080);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream());
// send message to server
out.println("Hello, world!");
// close resources
if (in != null)
in.close();
if (out != null)
out.close();
if (connection != null)
connection.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.exit(1);
}
}
}
Any insights? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要找出此类问题,您应该做的是隔离问题的根源。是服务器部分还是客户端部分?对服务器的一个简单测试是启动它,然后远程登录到该端口(例如“telnet 127.0.0.1 8080”),输入一些内容并查看它是否正在输出。 (顺便说一句,您的服务器代码工作正常)。
这样做可以让您专注于客户端代码。正如 Affe 所说,您只是没有刷新输入流。学习代码故障排除方法至少与学习编写代码一样重要。
另外,按照惯例,Java 类以大写字母开头,因此应该是“Server”和“Client”
What you should be doing to figure out this type of problem, is to isolate where the problem comes from. Is it the Server portion, or the client portion? An easy test for the server is to start it up, then telnet to that port (ex. "telnet 127.0.0.1 8080") type in something and see if it is outputing. (btw, your server code works fine).
Doing this would allow you to focus on your client code. As Affe stated, you simply didn't flush out the input stream. Learning methodologies for troubleshooting code is at least as important as learning to write code.
Also as an aside, by convention, Java classes start with a capital letter, so it should be "Server" and "Client"
默认的 PrintWriter 不会自动刷新。 (并且不会在关闭时刷新,因为抽象作家可能会欺骗性地让您相信。要么这样做:
要么
That default PrintWriter does not autoflush. (and doesnot flush on close as abstract Writer deceptively may lead you to believe. Either do:
or else
在客户端写入流后添加刷新:
同时确保服务器套接字未被防火墙阻止
Add a flush after you write to the stream in your client :
Also Make sure the server socket is not blocked by firewall