客户端发送请求,服务器没收到,直接打印到客户端上面了
来源:2-4 Socket TCP牛刀小试-服务端实现
qq_慕莱坞4316410
2020-05-10
package com.imooc.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(2000);
System.out.println(“服务器准备开始,正在启动服务器~~~~”);
System.out.println(“服务器的信息:”+serverSocket.getInetAddress()+" p:"+serverSocket.getLocalPort());
for(;;) {
Socket sokcet = serverSocket.accept();
ServerListen Listen = new ServerListen(sokcet);
Listen.start();
}
}
private static class ServerListen extends Thread{
private Socket socket;
private boolean flag=true;
public ServerListen(Socket sokcet) throws IOException{
this.socket=sokcet;
}
@Override
public void run(){
super.run();
System.out.println("客户端的信息输出:"+socket.getInetAddress()+" p:"+socket.getPort());
try{
PrintStream printStream = new PrintStream(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
do {
String read = reader.readLine();
if ("bye".equalsIgnoreCase(read)) {
flag=false;
printStream.println("byte");
} else {
printStream.println(read);
printStream.println("回收:" + read.length());
}
}while (flag);
printStream.close();
reader.close();
}catch (Exception e){
System.out.println("客户端接收异常~~~准备关闭服务器~~~~");
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("客户端已退出:" + socket.getInetAddress() +
" P:" + socket.getPort());
}
}
}
这是服务器的代码
package com.imooc.socket;
import java.io.*;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket=new Socket();
socket.setSoTimeout(3000);
socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000);
System.out.println("连接服务器成功,正在接收服务器的信息~~~~~~");
System.out.println("~~~~客户端的信息:"+socket.getLocalAddress()+" p:"+socket.getLocalPort());
System.out.println("~~~~服务器的信息:"+socket.getInetAddress()+" p:"+socket.getPort());
try {
clientSocket(socket);
} catch (IOException e) {
System.out.println("异常退出~~~~关闭服务器");
}
System.out.println("关闭客户端~~~~~");
socket.close();
}
public static void clientSocket(Socket client) throws IOException {
InputStream input=System.in;
BufferedReader reader=new BufferedReader(new InputStreamReader(input));
OutputStream outputStream=client.getOutputStream();
PrintStream clientPrint=new PrintStream(outputStream);
InputStream inputStream=client.getInputStream();
BufferedReader clientRead=new BufferedReader(new InputStreamReader(inputStream));
boolean flag=true;
do {
String read = reader.readLine();
clientPrint.println(read);
String clientReader=clientRead.readLine();
if("bye".equalsIgnoreCase(clientReader)){
flag=false;
}else{
System.out.println(clientReader);
}
}while (flag);
clientPrint.close();
clientRead.close();
}
}
这是客户端端的,老师一直没发现是哪里错误
1回答
-
Qiujuer
2020-05-20
从日志输出来看其实是正常的:
这是客户端的日志输出,服务器并没有日志输出。可以通过日志发现,客户端发送了数据,服务器有回送数据,但是客户端没有完全读取数据。
客户端从软键盘读取一行,并输出给服务器。
服务器从客户端读取一行,但是回送了两行。
问题就出现在这里了,服务器是读取-输出-读取-输出-读取.... 穿行的方式,并不是全双工的方式。所以一次输出后只会读取一行。
服务器也是穿行,但是问题就出现在:读取-输出-输出-读取-输出-输出... 每次读取后都输出两次。所以客户端就没有完全接收数据。服务器改一下:
读取-输出-打印控制台-读取-输出-打印控制台......
这样的流程就OK了。00
相似问题