简单Java客户端和服务器不能正确交互
我不明白为什么下面的代码不起作用。客户端向服务器发送消息,服务器将消息打印到标准输出 服务器的代码:简单Java客户端和服务器不能正确交互,java,network-programming,network-protocols,Java,Network Programming,Network Protocols,我不明白为什么下面的代码不起作用。客户端向服务器发送消息,服务器将消息打印到标准输出 服务器的代码: import java.net.*; import java.io.*; import java.math.BigInteger; public class server { public static void main(String args[]) { try { ServerSocket server = new S
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);
}
}
}
有什么见解吗?谢谢 默认PrintWriter不会自动刷新。(并且不要像抽象作者欺骗地让你相信的那样接近。或者:
PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
否则
out.println("Hello, world!");
out.flush();
默认的PrintWriter不会自动刷新。(并且不会在关闭时刷新,因为抽象的Writer欺骗地可能会让您相信。或者:
PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
否则
out.println("Hello, world!");
out.flush();
要解决这类问题,您应该做的是隔离问题的来源。是服务器部分还是客户端部分?服务器的一个简单测试是启动它,然后telnet到该端口(例如“telnet 127.0.0.1 8080”)键入一些内容,看看它是否在输出(顺便说一句,您的服务器代码工作正常) 这样做可以让您专注于客户机代码。正如Affe所说,您根本没有清除输入流。学习代码故障排除方法至少与学习编写代码同等重要
另外,按照惯例,Java类以大写字母开头,因此应该是“服务器”和“客户机”要解决这类问题,你应该做的是找出问题的根源。是服务器部分还是客户端部分?服务器的一个简单测试是启动它,然后通过telnet连接到该端口(例如“telnet 127.0.0.1 8080”)键入一些内容,看看它是否在输出。(顺便说一句,你的服务器代码工作正常) 这样做可以让您专注于客户机代码。正如Affe所说,您根本没有清除输入流。学习代码故障排除方法至少与学习编写代码同等重要
另外,按照惯例,Java类以大写字母开头,因此应该是“服务器”和“客户机”在写入客户机中的流后添加刷新:
// send message to server
out.println("Hello, world!");
out.flush();
还要确保服务器套接字未被防火墙阻止在客户端写入流后添加刷新:
// send message to server
out.println("Hello, world!");
out.flush();
还要确保服务器套接字未被防火墙阻止到底是什么错误,请说明??没有“错误”;代码编译得很好。但在我启动服务器和客户端之后,服务器挂起,但没有将任何内容打印到标准输出。挂起位置?在调试器下运行或添加一些println以查找位置?客户端是否运行并终止,没有任何错误?使用“printlns”,我发现服务器挂起在.readline()中客户端挂起在out.println()上。错误的确切含义是什么?没有“错误”;代码编译得很好。但在我启动服务器和客户端之后,服务器挂起,但没有将任何内容打印到标准输出。挂起位置?在调试器下运行或添加一些println以查找位置?客户端是否运行并终止,没有任何错误?使用“printlns”,我发现服务器挂起在.readline()中客户端挂起了。println()。感谢您的建议。不幸的是,对于赋值,规范要求使用小写字母的名称。感谢您的建议。不幸的是,对于赋值,规范要求使用小写字母的名称。