[英]Why do I not get any messages from my TCP Server and Client?
I am trying to implement a TCP Server and Client in Java. 我正在尝试用Java实现TCP服务器和客户端。 The Client should send a message to the Server and the Server should send the same message back to the Client.
客户端应向服务器发送一条消息,服务器应将相同的消息发送回客户端。 I get the connection between both, but there is not any message.
我得到了两者之间的连接,但是没有任何消息。 I have already searched a lot in the internet and my code is so simliar to other code, but my solution is still not working.
我已经在互联网上进行了大量搜索,并且我的代码与其他代码非常相似,但是我的解决方案仍然无法正常工作。 I am pretty new to this topic so I am happy about every advice.
我对这个话题还很陌生,所以我对每一个建议都很满意。 Here is my Client code:
这是我的客户代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Client
{
public static void main(String[] args)
{
final int PORT = 5000;
Socket socket = null;
try
{
socket = new Socket("localhost", PORT);
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
String message = "Hello World";
bw.write(message);
bw.flush();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String echo = br.readLine();
System.out.println(echo);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Server code: 服务器代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args)
{
final int PORT = 5000;
ServerSocket serverSocket = null;
Socket socket = null;
try
{
serverSocket = new ServerSocket(PORT);
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new
InputStreamReader(is));
String message = br.readLine();
System.out.println(message);
OutputStream os = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(os));
bw.write("ECHO: " + message);
bw.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
serverSocket.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
It's because you're using readLine()
to receive the message and you never terminate the message with an end of line . 这是因为您使用的是
readLine()
来接收消息,而您绝不会以line结尾来终止消息。
Add a new line to your messsage and the readLine() method will see it and return a value to you. 在您的消息中添加新行,readLine()方法将看到它并向您返回一个值。
String message = "Hello World\\n";
Note you're going to have the same problem on the return path, because you're also using readLine()
on the client. 请注意,您将在返回路径上遇到相同的问题,因为您还将在客户端上使用
readLine()
。 Add a new line to the server's reply: bw.write("ECHO: " + message + '\\n');
在服务器的回复中添加新行:
bw.write("ECHO: " + message + '\\n');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.