简单套接字程序中的错误
我无法在第二个实例上发送数据。服务器只是无限期地等待客户端数据 这是我的示例服务器代码片段:
ServerSocket serv = new ServerSocket(6789);
Socket soc = serv.accept();
System.out.println("waiting for client's input");
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
String indata=in.readLine();
System.out.println("The client says: "+indata+"\n Send them some data: ");
String datum="demodata";
out.writeBytes(datum);
System.out.println("Data sent");
示例客户端:
ocket soc = new Socket("localhost",6789);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
System.out.println("Connected to: "+soc.getLocalAddress() +"\nEnter data to be sent: ");
String outdata = br.readLine(); //take input
out.writeBytes(outdata); // send
String indata=in.readLine(); //read
System.out.println("Data Sent! Now reading data from server "+indata)
请告诉我我的问题!提前致谢
I am not able to send the data on the second instance. The server just waits infinitely for client data
Here is my sample server code snippet:
ServerSocket serv = new ServerSocket(6789);
Socket soc = serv.accept();
System.out.println("waiting for client's input");
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
String indata=in.readLine();
System.out.println("The client says: "+indata+"\n Send them some data: ");
String datum="demodata";
out.writeBytes(datum);
System.out.println("Data sent");
Sample Client:
ocket soc = new Socket("localhost",6789);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream out = new DataOutputStream(soc.getOutputStream());
System.out.println("Connected to: "+soc.getLocalAddress() +"\nEnter data to be sent: ");
String outdata = br.readLine(); //take input
out.writeBytes(outdata); // send
String indata=in.readLine(); //read
System.out.println("Data Sent! Now reading data from server "+indata)
Please tell me my problem! Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答案很简单。您想要在服务器和客户端之间交换文本行。
正确的部分在服务器端:
错误的部分在客户端:
我还没有测试代码,但看起来你只是发送一些数据,而你的服务器端正在等待
\n
(换行转义序列)出现。选项 1:
构造一个
PrintWriter
并调用相应的println
方法。选项 2:
手动附加换行符 (
\n
)。之后,服务器端的
readLine
将识别以\n
结尾的行并继续执行。Answer is quite simple. You want to exchange lines of text between server and client.
The correct part is on the server side:
The wrong part is on the client side:
I haven't tested the code, but it seems you are just sending some data and your server side waits for
\n
(newline escape sequence) to appear.Option 1:
Construct a
PrintWriter
and call the correspondingprintln
method.Option 2:
Append a newline (
\n
) manually.Afterwards,
readLine
on the server side will recognize the line terminated by\n
and will proceed.我认为您忘记刷新输出流。
在服务器端和客户端的
out.writeBytes()
后面添加一行:out.flush()
。I think that you forgot to flush output stream.
Add the line:
out.flush()
just afterout.writeBytes()
on both server and client sides.您的客户端不会在其写入的输出中发送换行符。并且您的服务器通过在 BufferedReader 上调用
readLine
隐式地期待这个换行符(它期望通过该方法读取的数据中有一个终止换行符)。调用out.writeBytes()
后,在客户端和服务器代码中添加以下内容:或者,您也可以使用 PrintWriter 的方法,只使用其
println
方法。Your client is not sending a newline character in the output it is writing. And your server is expecting this newline, implicitly, by calling
readLine
on BufferedReader (which expects a terminating newline character in the data it reads via that method). Add the following in both your client and server code, after callingout.writeBytes()
:Alternatively, you can use PrintWriter's instead and just utilize its
println
method.这是一些示例代码。看看它是否适合你。您可以使用 Ctrl-C 停止服务器代码,客户端将等待服务器重新出现。
另外,您可以停止服务器,它将等待客户端重新出现。
main方法有语法
Here is some sample code. See if it works for you. You can stop the server code with Ctrl-C and the client will wait for the server to reappear.
Also, you can stop the server and it will wait for the client to reappear.
The main method has syntax