首页 > 代码库 > Java网络编程之tcp的socket通信

Java网络编程之tcp的socket通信

1、客户端MyClient.java

 1 import java.io.*;
 2 import java.net.*;
 3 
 4 public class MyClient
 5 {
 6     public static void main(String[] args)throws Exception
 7     {
 8         Socket s = new Socket("192.168.1.1" , 30000);
 9         // 客户端启动ClientThread线程不断读取来自服务器的数据
10         new Thread(new ClientThread(s)).start();11         // 获取该Socket对应的输出流
12         PrintStream ps = new PrintStream(s.getOutputStream());
13         String line = null;
14         // 不断读取键盘输入
15         BufferedReader br = new BufferedReader(
16             new InputStreamReader(System.in));
17         while ((line = br.readLine()) != null)
18         {
19             // 将用户的键盘输入内容写入Socket对应的输出流
20             ps.println(line);
21         }
22     }
23 }

2、加入多线程,ClientThread.java

 1 import java.io.*;
 2 import java.net.*;
 3 
 4 public class ClientThread implements Runnable
 5 {
 6     // 该线程负责处理的Socket
 7     private Socket s;
 8     // 该线程所处理的Socket所对应的输入流
 9     BufferedReader br = null;
10     public ClientThread(Socket s)
11         throws IOException
12     {
13         this.s = s;
14         br = new BufferedReader(
15             new InputStreamReader(s.getInputStream()));
16     }
17     public void run()
18     {
19         try
20         {
21             String content = null;
22             // 不断读取Socket输入流中的内容,并将这些内容打印输出
23             while ((content = br.readLine()) != null)
24             {
25                 System.out.println(content);
26             }
27         }
28         catch (Exception e)
29         {
30             e.printStackTrace();
31         }
32     }
33 }

 

3、服务器端MyServer.java

 1 import java.net.*;
 2 import java.io.*;
 3 import java.util.*;
 4 
 5 public class MyServer
 6 {
 7     // 定义保存所有Socket的ArrayList
 8     public static ArrayList<Socket> socketList
 9         = new ArrayList<Socket>();
10     public static void main(String[] args)
11         throws IOException
12     {
13 System.out.println("222222……");
14         ServerSocket ss = new ServerSocket
15 
16 (30000);
17         while(true)
18         {
19 System.out.println("服务器等待链接……");
20             // 此行代码会阻塞,将一直等待别人的连接
21             Socket s = ss.accept();
22             socketList.add(s);
23             // 每当客户端连接后启动一条ServerThread线程为该客户端服务
24             new Thread(new ServerThread
25 
26 (s)).start();
27         }
28     }
29 }

 

4、为服务器端通信加入多线程

 1 import java.io.*;
 2 import java.net.*;
 3 import java.util.*;
 4 
 5 // 负责处理每个线程通信的线程类
 6 public class ServerThread implements Runnable {
 7     // 定义当前线程所处理的Socket
 8     Socket s = null;
 9     // 该线程所处理的Socket所对应的输入流
10     BufferedReader br = null;
11 
12     public ServerThread(Socket s) throws IOException {
13         System.out.println("4444444……");
14         this.s = s;
15         // 初始化该Socket对应的输入流
16     String ip=s.getInetAddress().getHostAddress();
17         System.out.println("ip地址为"+ip+"........connected!!!");
18         br = new BufferedReader(new InputStreamReader(s.getInputStream(),
19                 "utf-8")); // 
20     }
21 
22     public void run() {
23         System.out.println("555555……");
24         try {
25             System.out.println("4444444……");
26             String content = null;
27             // 采用循环不断从Socket中读取客户端发送过来的数据
28             while ((content = readFromClient()) != null) {
29                 /*System.out.println("---"
30                         + Arrays.toString(content.getBytes("utf-8")));*/
31                 System.out.println("---" +"客户端发来的数据为"+content);
32                 System.out.println("===============");
33                 // 遍历socketList中的每个Socket,
34                 // 将读到的内容向每个Socket发送一次
35                 for (Iterator<Socket> it = MyServer.socketList.iterator(); it
36                         .hasNext();) {
37                     Socket s = it.next();
38                     try {
39                         System.out.println("666666");
40                         OutputStream os = s.getOutputStream();
41                         os.write((content+"服务器反馈给客户端ok" + "\n").getBytes("utf-8"));
42                         System.out.println("777777");
43                     } catch (SocketException e) {
44                         e.printStackTrace();
45                         // 删除该Socket。
46                         it.remove();
47                         System.out.println(MyServer.socketList);
48                     }
49                 }
50             }
51         } catch (IOException e) {
52             e.printStackTrace();
53         }
54     }
55 
56     // 定义读取客户端数据的方法
57     private String readFromClient() {
58         try {
59             return br.readLine();
60         }
61         // 如果捕捉到异常,表明该Socket对应的客户端已经关闭
62         catch (IOException e) {
63             e.printStackTrace();
64             System.out.println("客户端已经关闭!!!");
65             // 删除该Socket。
66             MyServer.socketList.remove(s); 
67         }
68         return null;
69     }
70 }

 

Java网络编程之tcp的socket通信