首页 > 代码库 > 初次试验最简单的socket编程

初次试验最简单的socket编程

因为电脑限制同时为了操作方便,所以在本地的2个不同的工作空间的不同工程下测试:

workspace-1:

服务器端代码 ServerSocketTest.java

 1 package socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStream;
 7 import java.io.PrintWriter;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 import java.util.Scanner;
11 
12 public class ServerSocketTest {
13 
14     public static void main(String[] args) {
15         try {
16             while(true){
17                 ServerSocket serverSocket = new ServerSocket(10086);
18                 Socket socket = serverSocket.accept();
19                 InputStream inputStream = socket.getInputStream();
20                 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
21                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
22                 String data = http://www.mamicode.com/null;
23                 while((data = http://www.mamicode.com/bufferedReader.readLine()) != null){
24                     System.out.println("客户端发来消息说:"+data);
25                 }
26                 socket.shutdownInput();
27                 
28                 OutputStream outputStream = socket.getOutputStream();
29                 PrintWriter printWriter = new PrintWriter(outputStream);
30                 Scanner in = new Scanner(System.in);
31                 printWriter.write(in.nextLine());
32                 printWriter.flush();
33                 printWriter.close();
34                 outputStream.close();
35                 
36                 bufferedReader.close();
37                 inputStreamReader.close();
38                 inputStream.close();
39                 socket.close();
40                 serverSocket.close();
41             }
42         } catch (Exception e) {
43             e.printStackTrace();
44         }
45     }
46 }

workspace-2:

客户端代码 SocketTest.java

 1 package socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStream;
 7 import java.io.PrintWriter;
 8 import java.net.Socket;
 9 import java.util.Scanner;
10 
11 public class SocketTest {
12 
13     public static void main(String[] args) {
14         try {
15             while(true){
16                 Socket socket = new Socket("127.0.0.1", 10086);
17                 OutputStream outputStream = socket.getOutputStream();
18                 PrintWriter printWriter = new PrintWriter(outputStream);
19                 Scanner in = new Scanner(System.in);
20                 printWriter.write(in.nextLine());
21                 printWriter.flush();
22                 socket.shutdownOutput();
23                 
24                 InputStream inputStream = socket.getInputStream();
25                 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
26                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
27                 String data = http://www.mamicode.com/null;
28                 while((data = http://www.mamicode.com/bufferedReader.readLine()) != null){
29                     System.out.println("服务器说:"+data);
30                 }
31                 
32                 bufferedReader.close();
33                 inputStreamReader.close();
34                 inputStream.close();
35                 printWriter.close();
36                 outputStream.close();
37                 socket.close();
38             }
39         } catch (Exception e) {
40             e.printStackTrace();
41         }
42         
43     }
44 }

服务器端控制台输入与显示:

技术分享

客户端控制台输入与显示:

技术分享

 

1、目前还没有查阅资料,不知道如何实现多次实时输入传输,本文中只是使用最外层的while循环来暂时实现实时输入传输,不断在new流与关闭流,这种方法效率应该很低。

2、文中只是测试了本地127.0.0.1传输信息可以实现,没有测试不同IP地址的信息传输

3、在一方控制台输入要发送的信息回车后,在另一方要先按回车键,才会显示出接受到的信息,因为当断点走到Scanner输入的时候,此时如果不按一下回车键,代码不会往后走,所以也

  不会获取输入流,无法显示接收的信息。

初次试验最简单的socket编程