Socket编程——客户端,服务器端的读写操作

URL网络编程,最大的特征就是一对一的响应!

1:客户端“写”,服务器端用于“读”

Socket编程——客户端,服务器端的读写操作_第1张图片

 

 1 package coreBookSocket2;
 2 
 3 import java.io.InputStreamReader;
 4 import java.io.Reader;
 5 import java.net.ServerSocket;
 6 import java.net.Socket;
 7 
 8 /*
 9  * 这边为服务器端的读取
10  */
11 
12 /*
13  * 1:首先建立一个ServerSocket监听在端口8888上面,服务器端只需要定义和客户端相同的端口号即可
14  * 2:服务器端尝试接受来自客户端的请求
15  * 3:和客户端连接好了之后,就可以读取来自客户端的输入信息
16  * 4:定义一个StringBuilder对象用来保存来自客户端的信息
17  * 5:关闭Reader,client,server等占用资源的对象
18  */
19 public class clientWtiteserverOpen {
20     
21     public static void main(String[] args) throws Exception{
22         ServerSocket server=new ServerSocket(8888);
23         Socket client =server.accept();
24         Reader reader=new InputStreamReader(client.getInputStream());        //客户端获取自己的输入流
25         char[] ch=new char[64];
26         int len;
27         StringBuilder sb=new StringBuilder();
28         while( ( len=reader.read(ch) )!=-1)
29         {
30             sb.append(ch, 0, len);
31         }
32         System.out.println("From Client:"+sb);
33         reader.close();
34         client.close();
35         server.close();
36     }
37 }

 

 1 package coreBookSocket2;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStreamWriter;
 5 import java.io.Writer;
 6 import java.net.Socket;
 7 import java.net.UnknownHostException;
 8 /*
 9  * 这边为客户端的写出
10  */
11 
12 /*
13  * 1:首先建立一个客户端的监听端口和IP地址,用相同的端口号与服务器端建立连接
14  * 2:从客户端向服务器端写出数据
15  */
16 public class clientWriteserverOpen2 {
17     public static void main(String[] args) throws UnknownHostException, IOException {
18         
19         Socket client=new Socket("127.0.0.1", 8888);
20         Writer writer=new OutputStreamWriter(client.getOutputStream());
21         writer.write("Hello server");
22         writer.flush();
23         writer.close();
24         client.close();
25         
26     }
27 
28 }

 2:客户端先写后读,服务器端先读后写

服务器端:

 1 package ClientAndServerWriteBoth;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 import java.io.OutputStreamWriter;
 6 import java.io.Reader;
 7 import java.io.Writer;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 /*
12  * 服务器端的编写
13  *  服务器端是先读后写,那么客户端必定是先写后读
14  */
15 
16 public class server {
17     public static void main(String[] args) throws IOException {
18         
19         ServerSocket server=new ServerSocket(6666);
20         Socket client=server.accept();
21         
22         Reader reader=new InputStreamReader(client.getInputStream());
23         char[] ch=new char[20];
24         int len;
25         StringBuilder sb=new StringBuilder();
26         String temp;
27         int index;
28         
29         while(( len=reader.read(ch)) !=-1)
30         {
31             temp=new String(ch, 0, len);
32             if( (index = temp.indexOf("eof")) != -1)//遇到eof时就结束接收,    这个地方没有怎么看明白?为什么到eof就结束?
33             {
34                 sb.append(temp.substring(0, index));
35                 break;
36             }
37             sb.append(temp);
38         }
39         System.out.println("---From Client:  "+sb);
40         
41         Writer writer=new OutputStreamWriter(client.getOutputStream());
42         writer.write("Hello Client I'm server! ");
43         writer.flush();
44         writer.close();
45         reader.close();
46         client.close();
47         server.close();
48     }
49 }

客户端:

 1 package ClientAndServerWriteBoth;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 import java.io.OutputStreamWriter;
 6 import java.io.Reader;
 7 import java.io.Writer;
 8 import java.net.Socket;
 9 import java.net.UnknownHostException;
10 
11 /*
12  * 客户端的编写,客户端先写后读
13  * 1:客户端设定特定的端口号与服务器端连接
14  */
15 
16 public class client {
17     public static void main(String[] args) throws UnknownHostException, IOException {
18         
19         Socket client=new Socket("127.0.0.1", 6666);
20         Writer writer=new OutputStreamWriter(client.getOutputStream());
21         writer.write("Hello server, I'm your father,client!  哈哈哈哈哈");
22         writer.flush();
23         //写完了开始读
24         
25         Reader reader=new InputStreamReader(client.getInputStream());
26         char[] ch=new char[20];
27         int len;
28         StringBuffer sb=new StringBuffer();
29         String temp;
30         int index;
31         
32         while( (len=reader.read(ch))!=-1)
33         {
34             temp=new String(ch, 0, len);
35                     if(( index=temp.indexOf("eof"))!=-1)
36                     {
37                         sb.append(temp.substring(0, index));
38                         break;
39                     }
40             sb.append(new String(ch, 0, len));
41         }
42         System.out.println("---From Server:  "+sb);
43         writer.close();
44         reader.close();
45         client.close();    
46     }
47 }

 3:一个服务器端,对应多个客户端的连接

     一个服务器端和多个客户端连接最好的方式就是用线程,因为服务器端接受客户端的连接的accept()方法是阻塞式的。

服务器端:

 1 package oneServerForLotsClient;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 import java.io.OutputStreamWriter;
 6 import java.io.Reader;
 7 import java.io.Writer;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 /*
11  * 这边主要是服务器的demo的编写
12  * 1:仍然是准备工作,让服务器端和客户端之间建立相同的端口号,实现连接。
13  * 2:----因为客户端那边是多个对象连接服务器端,因此需要一个死循环让服务器一直接受连接客户端的连接请求!!!
14  *                 建立一个新的线程去处理客户端的请求
15  *3:服务器端首先是“读取” 客户端发送过来的信息
16  */
17 
18 public class Server 
19 {
20     public static void main(String[] args) throws IOException 
21     {
22         
23         ServerSocket server=new ServerSocket(6868);
24         while(true)
25             {
26             Socket client=server.accept();
27             new Thread(new Task(client) ).start();
28             }
29     }
30 }
 1 package oneServerForLotsClient;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStreamWriter;
 7 import java.io.Writer;
 8 import java.net.Socket;
 9 /*
10  * 这个类主要是用来处理新的线程,实现Runnable接口
11  * 1:首先定一个构造方法,主要是为了初始化client对象
12  * 2:同时重写run()方法,这个方法主要开启新的方法用于 “ 读” 数据,和 “写” 数据的处理
13  */
14 
15 public class Task implements Runnable {
16 
17     private  Socket client;
18     
19     public Task(Socket client)
20     {
21         this.client=client;
22     }
23 
24     @Override
25     public void run() {
26         try {
27             handlerSocket();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32 
33     private void handlerSocket() throws IOException {
34         BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));  
35         StringBuilder sb = new StringBuilder();  
36         String temp;  
37         int index;  
38         while ((temp=br.readLine()) != null) {  
39            System.out.println(temp);  
40            if ((index = temp.indexOf("eof")) != -1) {//遇到eof时就结束接收  
41             sb.append(temp.substring(0, index));  
42                break;  
43            }  
44            sb.append(temp);  
45         }  
46         System.out.println("from client: " + sb);  
47         //读完后写一句  
48       Writer writer = new OutputStreamWriter(client.getOutputStream());  
49         writer.write("Hello Client.");  
50         writer.write("eof\n");  
51         writer.flush();  
52         writer.close();  
53         br.close();  
54         client.close();  
55         
56     }
57 
58 }

 

客户端

 1 package oneServerForLotsClient;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStreamWriter;
 7 import java.io.Writer;
 8 import java.net.Socket;
 9 import java.net.UnknownHostException;
10 
11 public class Client {
12     public static void main(String[] args) throws UnknownHostException, IOException {
13         
14         Socket client=new Socket("127.0.0.1", 6868);
15         
16         Writer writer=new OutputStreamWriter(client.getOutputStream());
17         writer.write("Hello,Server. My name is client! ");
18         writer.write("eof\n");  
19         writer.flush();
20         
21         
22         //写完了就开始读
23         
24         BufferedReader reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
25         StringBuffer buffer=new StringBuffer();
26         String temp;  
27           int index;  
28           while ((temp=reader.readLine()) != null) {  
29              if ((index = temp.indexOf("eof")) != -1) {  
30                 buffer.append(temp.substring(0, index));  
31                 break;  
32              }  
33              buffer.append(temp);  
34           }  
35           System.out.println("from server: " + buffer);  
36           writer.close();  
37           reader.close();  
38           client.close(); 
39         
40     }
41 
42 }

 

你可能感兴趣的:(Socket编程——客户端,服务器端的读写操作)