关于Socket服务器与客户端双向通信时碰到的一个坑 - Ye

关于Socket服务器与客户端双向通信时碰到的一个坑 - Ye

学习Socket时,

想实现一个读写分离,能够实时接收和发送消息的socket,却一直发送消息出去却不能打印出来。

 

客户端发送消息出去后。

 

 客户端却不能打印出消息,客户端也不能打印出消息。

一直冥思苦想,一直到不到问题所在。

直到

 

 发现 PrintWriter将 write  方法 改为 println后成功打印出消息。

 

 

 

 最后 附上源码

客户端:

 1 package my.socket;
 2 
 3 import java.io.*;
 4 import java.net.Socket;
 5 
 6 public class SocketClient {
 7 
 8     public static void main(String[] args) throws IOException {
 9         Socket socket = new Socket("localhost",55532);
10 
11         ReadSocketThread readSocketThread = new ReadSocketThread(socket);
12         readSocketThread.start();
13 
14         WriteSocketThread writeSocketThread = new WriteSocketThread(socket);
15         writeSocketThread.start();
16 
17 
18 
19     }
20 }

服务端:

 1 package my.socket;
 2 
 3 import java.io.*;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 public class SocketService {
 8 
 9     public static void main(String[] args) throws IOException {
10 
11         ServerSocket serverSocket = new ServerSocket(55532);
12 
13         System.out.println("ServiceSocket已启动,等待Client连接");
14 
15         Socket socket = serverSocket.accept();
16 
17         WriteSocketThread writeSocketThread = new WriteSocketThread(socket);
18         writeSocketThread.start();
19         ReadSocketThread readSocketThread = new ReadSocketThread(socket);
20         readSocketThread.start();
21 
22     }
23 
24 }

消息写入线程:

 1 package my.socket;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStream;
 5 import java.io.PrintWriter;
 6 import java.net.Socket;
 7 import java.util.Scanner;
 8 
 9 public class WriteSocketThread extends Thread {
10 
11     private Socket socket;
12     private OutputStream os;
13     private PrintWriter pw;
14 
15     public WriteSocketThread(Socket socket){
16         this.socket = socket;
17         try {
18             this.os = socket.getOutputStream();
19         } catch (IOException e) {
20             e.printStackTrace();
21         }
22         this.pw = new PrintWriter(os);
23     }
24 
25     @Override
26     public void run() {
27 
28         pw.flush();
29         while (true) {
30             Scanner scanner = new Scanner(System.in);
31             pw.println(scanner.nextLine());
32             pw.flush();
33         }
34 
35     }
36 }

消息读取线程:

 1 package my.socket;
 2 
 3 
 4 import java.io.BufferedReader;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.InputStreamReader;
 8 import java.net.Socket;
 9 import java.util.Scanner;
10 
11 public class ReadSocketThread extends Thread   {
12 
13     private Socket socket;
14 
15     private Scanner scanner;
16 
17 
18     public ReadSocketThread(Socket socket) throws IOException {
19         this.socket = socket;
20         this.scanner = new Scanner(socket.getInputStream());
21     }
22 
23     @Override
24     public void run() {
25         while(true){
26             try {
27                 System.out.println(scanner.nextLine());
28 
29             } catch (Exception e) {
30                 e.printStackTrace();
31                 return;
32             }
33         }
34     }
35 }

 

参考链接:https://blog.csdn.net/paullinjie/article/details/80577155