首页 > 代码库 > javase套接字编程

javase套接字编程

1、沟通多线程

 1 package com.socket;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStream;
 7 import java.net.InetSocketAddress;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 import java.net.SocketAddress;
11 
12 public class Communication extends Thread{
13     private Socket socket;
14     private String clientInfo;
15 
16     public Communication(Socket socket) {
17         super();
18         this.socket = socket;
19         clientInfo=getClientInfo();
20     }
21     public void run(){
22         //System.out.println("有连接了");
23         InputStream is;
24         try {
25             is = socket.getInputStream();//获取输入流,读取客户端内容
26             OutputStream os = socket.getOutputStream();//获取输出,回传给客户
27             InputStreamReader isr = new InputStreamReader(is);
28             char[] buf=new char[1024];
29             int len=0;
30             while((len=isr.read(buf))!=-1){
31                 String msg=new String(buf,0,len);
32                 
33                 System.out.println("["+clientInfo+"]"+msg);
34                 os.write(("[from chaofei]"+msg).getBytes());
35             }
36         } catch (IOException e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }
40     }
41     //获取远程客户端信息
42     private String getClientInfo(){
43         try {
44             InetSocketAddress addr = (InetSocketAddress) socket.getRemoteSocketAddress();
45             String ip = addr.getAddress().getHostAddress();
46             int port = addr.getPort();
47             return ip+":"+port;
48         } catch (Exception e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         }
52         return null;
53         
54     }
55 }

2、服务器端

 1 package com.socket;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 import org.junit.Test;
10 
11 /**
12  * 只接受客户端内容,不进行回复
13  * 
14  * @author feigu
15  *
16  */
17 public class MySever {
18     @Test
19     public void start() {
20         try {
21             ServerSocket ss = new ServerSocket(8888);
22             while (true) {
23 
24                 Socket socket = ss.accept();// 接收客户端请求
25                 new Communication(socket).start();
26             }
27         } catch (IOException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31     }
32 }

3、客户端

 1 package com.socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStream;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 import org.junit.Test;
12 
13 public class MyClient {
14     @Test
15     public void send() {
16         try {
17             Socket s = new Socket("localhost", 8888);
18             OutputStream os = s.getOutputStream();
19             InputStream is = s.getInputStream();
20             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21             String line = null;
22             byte[] buf = new byte[1024];
23             int len = 0;
24             while ((line = br.readLine()) != null) {
25                 os.write(line.getBytes());
26                 len = is.read(buf);
27                 System.out.println(new String(buf, 0, len));
28             }
29 
30         } catch (IOException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34     }
35 }

 

javase套接字编程