Java网络编程--套接字Socket

一、套接字Socket

IP地址标志Internet上的计算机,端口号标志正在计算机上运行的进程(程序)。

端口号被规定为一个16位的0--65535之间的整数,其中,0--1023被预先定义的服务通信占用。

当两个程序需要通信时,可以通过Socket类建立套接字对象并连接在一起。(端口号与IP地址的组合得出的一个网络套接字)

 

二、示例

(1)客户端服务器端一对一;聊天

Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package mychat;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStreamWriter;
 8 import java.net.Socket;
 9 import java.net.UnknownHostException;
10 
11 public class Client {
12 
13     /**
14      * @param args
15      */
16     public static void main(String[] args) {
17         // TODO Auto-generated method stub
18         Socket client = null;
19         //读服务器说的话
20         BufferedReader br = null;
21         //写个服务器
22         BufferedWriter bw = null;
23         //读键盘
24         BufferedReader input = null;
25         try {
26             client = new Socket("127.0.0.1",6666);
27             while(true)
28             {
29             //从键盘写
30                 System.out.println("客户端说:");
31                 input = new BufferedReader(new InputStreamReader(System.in));
32                 String str = input.readLine();
33                 //写给服务器
34                 bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
35                 bw.write(str);
36                 bw.newLine();
37                 bw.flush();
38                 //接受服务器信息
39                 br = new BufferedReader(new InputStreamReader(client.getInputStream()));
40                 String line =null;
41                 line = br.readLine();
42                 System.out.println("服务器说"+line);
43             }
44             
45         
46         } catch (UnknownHostException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         } catch (IOException e) {
50             // TODO Auto-generated catch block
51             e.printStackTrace();
52         }
53         
54     }
55 
56 }
Client.java客户端
Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package mychat;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStreamWriter;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 public class Server {
12 
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         BufferedReader input = null;
16         BufferedWriter bw =null;
17         BufferedReader br = null;
18         Socket socket = null;
19         try {
20             ServerSocket server = new ServerSocket(6666);
21             
22             System.out.println("等待客户端响应");
23             socket = server.accept();
24             System.out.println("与客户端建立连接");
25             //
26             br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
27             //
28             bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
29             input = new BufferedReader(new InputStreamReader(System.in));
30             
31             
32             while(true)
33             {
34                 String line = br.readLine();
35                     System.out.println("客户端说:"+line);
36                 //
37                     
38                 System.out.println("请输入服务器说的话");
39                 String str = input.readLine();
40                 bw.write(str);
41                 bw.newLine();
42                 bw.flush();
43                 System.out.println("服务器说:"+str);
44             }
45             
46         } catch (IOException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50         finally{
51             try {
52                 br.close();
53             } catch (IOException e) {
54                 // TODO Auto-generated catch block
55                 e.printStackTrace();
56             }
57             try {
58                 bw.close();
59             } catch (IOException e) {
60                 // TODO Auto-generated catch block
61                 e.printStackTrace();
62             }
63             try {
64                 input.close();
65             } catch (IOException e) {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69             try {
70                 socket.close();
71             } catch (IOException e) {
72                 // TODO Auto-generated catch block
73                 e.printStackTrace();
74             }
75             
76         }
77     }
78 
79 }
Server.java服务器端

(2)客户端上传文件(字符)

Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadzifu;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileReader;
 7 import java.io.IOException;
 8 import java.io.InputStreamReader;
 9 import java.io.OutputStreamWriter;
10 import java.net.Socket;
11 import java.net.UnknownHostException;
12 
13 public class Client {
14 
15     /**
16      * @param args
17      */
18     public static void main(String[] args) {
19         // TODO Auto-generated method stub
20         Socket client = null;
21         //读文件名
22         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
23         //读本地文件
24         File f=null;
25         BufferedReader brFile = null;
26         BufferedWriter bw = null;
27         try {
28             client = new Socket("127.0.0.1",6666);
29             bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
30             System.out.println("请输入要上传的文件");
31             String fileName = input.readLine();
32             brFile = new BufferedReader(new FileReader(("src/chat/"+fileName)));
33             String line =null;
34             while((line= brFile.readLine())!=null)
35             {
36                 //写个客户端
37                 bw.write(line);
38                 bw.newLine();
39                 bw.flush();
40                 
41             }
42             
43         } catch (UnknownHostException e) {
44             // TODO Auto-generated catch block
45             e.printStackTrace();
46         } catch (IOException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50         finally{
51             try {
52                 bw.close();
53             } catch (IOException e) {
54                 // TODO Auto-generated catch block
55                 e.printStackTrace();
56             }
57             try {
58                 brFile.close();
59             } catch (IOException e) {
60                 // TODO Auto-generated catch block
61                 e.printStackTrace();
62             }
63             try {
64                 input.close();
65             } catch (IOException e) {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69         }
70         
71         
72     }
73 
74 }
Client.java客户端
Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadzifu;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileOutputStream;
 7 import java.io.FileWriter;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.io.OutputStreamWriter;
11 import java.net.ServerSocket;
12 import java.net.Socket;
13 
14 public class Server {
15 
16     /**
17      * @param args
18      */
19     public static void main(String[] args) {
20         // TODO Auto-generated method stub
21         ServerSocket server = null;
22         Socket socket = null;
23         BufferedReader br =null;
24         BufferedWriter bwFile = null;
25         try {
26             server = new ServerSocket(6666);
27             System.out.println("等待客户端连接");
28             socket = server.accept();
29             System.out.println("与客户端建立连接");
30             File f = new File("src/loadzifu/a.txt");
31             bwFile = new BufferedWriter(new FileWriter(f));
32             br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
33             String str = null;
34             while((str=br.readLine())!=null)
35             {
36                 bwFile.write(str);
37                 bwFile.newLine();
38                 bwFile.flush();
39             }
40             
41         } catch (IOException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }
45         finally{
46             try {
47                 br.close();
48             } catch (IOException e) {
49                 // TODO Auto-generated catch block
50                 e.printStackTrace();
51             }
52             try {
53                 bwFile.close();
54             } catch (IOException e) {
55                 // TODO Auto-generated catch block
56                 e.printStackTrace();
57             }
58             try {
59                 socket.close();
60             } catch (IOException e) {
61                 // TODO Auto-generated catch block
62                 e.printStackTrace();
63             }
64         }
65         
66     }
67 
68 }
Server.java服务器端

(3)客户端上传文件(字节)

Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadzijie;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.BufferedReader;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.net.Socket;
12 import java.net.UnknownHostException;
13 
14 public class Client {
15 
16     public static void main(String[] args) {
17         // TODO Auto-generated method stub
18         Socket client = null;
19         //读文件
20         BufferedInputStream bisFile = null;
21         //源文件
22         File f = null;
23         //读键盘
24         BufferedReader input = null;
25         //写个服务器
26         BufferedOutputStream bos = null;
27         try {
28             client = new Socket("127.0.0.1",7777);
29             
30             System.out.println("请输入要复制的文件");
31             input = new BufferedReader(new InputStreamReader(System.in));
32             String fileName = input.readLine();
33             f = new File("src/chat/"+fileName);
34             //读文件
35             bisFile = new BufferedInputStream(new FileInputStream(f));
36             bos = new BufferedOutputStream(client.getOutputStream());
37             int len=0;
38             byte bytes[] = new byte[1024];
39             while((len=bisFile.read(bytes))!=-1)
40             {
41                 bos.write(bytes,0,len);
42                 bos.flush();
43             }
44             
45             
46             
47             
48         } catch (UnknownHostException e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         } catch (IOException e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }finally{
55             try {
56                 bos.close();
57             } catch (IOException e) {
58                 // TODO Auto-generated catch block
59                 e.printStackTrace();
60             }
61             try {
62                 client.close();
63             } catch (IOException e) {
64                 // TODO Auto-generated catch block
65                 e.printStackTrace();
66             }
67             try {
68                 bisFile.close();
69             } catch (IOException e) {
70                 // TODO Auto-generated catch block
71                 e.printStackTrace();
72             }
73         }
74         
75     }
76 
77 }
Client.java客户端
Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadzijie;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.BufferedWriter;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 import java.net.ServerSocket;
11 import java.net.Socket;
12 
13 public class Server {
14 
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17             ServerSocket server = null;
18             Socket socket = null;
19             File f = null;
20             BufferedInputStream bis = null;
21             BufferedOutputStream bosFile = null;
22             
23             try {
24                 server = new ServerSocket(7777);
25                 System.out.println("等待客户端连接");
26                 socket = server.accept();
27                 System.out.println("与客户端建立连接");
28                 f = new File("src/loadzijie/dd.bmp");
29                 bis = new BufferedInputStream(socket.getInputStream());
30                 bosFile = new BufferedOutputStream(new FileOutputStream(f));
31                 int len =0;
32                 byte bytes[] = new byte[1024];
33                 while((len = bis.read(bytes))!=-1)
34                 {
35                     //写文件
36                     bosFile.write(bytes,0,len);
37                     bosFile.flush();
38                     
39                 }
40                 System.out.println("上传成功");
41                 
42                 
43             } catch (IOException e) {
44                 // TODO Auto-generated catch block
45                 e.printStackTrace();
46             }
47             finally{
48                 try {
49                     bis.close();
50                 } catch (IOException e) {
51                     // TODO Auto-generated catch block
52                     e.printStackTrace();
53                 }
54                 try {
55                     bosFile.close();
56                 } catch (IOException e) {
57                     // TODO Auto-generated catch block
58                     e.printStackTrace();
59                 }
60                 try {
61                     socket.close();
62                 } catch (IOException e) {
63                     // TODO Auto-generated catch block
64                     e.printStackTrace();
65                 }
66             }
67     }
68 
69 }
Server.java服务器端

(4)服务器端多线程接受客户端文件

Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadget;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.BufferedReader;
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.net.Socket;
11 import java.net.UnknownHostException;
12 
13 public class Client {
14 
15     /**
16      * @param args
17      */
18     public static void main(String[] args) {
19         // TODO Auto-generated method stub
20         Socket client = null;
21         File f = null;
22         //读键盘
23         BufferedReader input = null;
24         //读文件
25         BufferedInputStream brFile = null;
26         //上传服务器
27         BufferedOutputStream bos = null;
28         try {
29             client = new Socket("127.0.0.1",7777);
30             System.out.println("请输入要上传的文件");
31             input = new BufferedReader(new InputStreamReader(System.in));
32             String fileName = input.readLine();
33             bos = new BufferedOutputStream(client.getOutputStream());
34             f = new File(("src/chat/"+fileName));
35             //读文件
36             brFile = new BufferedInputStream(new FileInputStream(f));
37             int len =0;
38             byte bytes[]= new byte[1024];
39             while((len=brFile.read(bytes))!=-1)
40             {
41                 bos.write(bytes,0,len);
42                 bos.flush();
43             }
44         
45         } catch (UnknownHostException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         } catch (IOException e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         }
52         finally{
53             try {
54                 bos.close();
55             } catch (IOException e1) {
56                 // TODO Auto-generated catch block
57                 e1.printStackTrace();
58             }
59             try {
60                 brFile.close();
61             } catch (IOException e1) {
62                 // TODO Auto-generated catch block
63                 e1.printStackTrace();
64             }
65             try {
66                 client.close();
67             } catch (IOException e) {
68                 // TODO Auto-generated catch block
69                 e.printStackTrace();
70             }
71         }
72         
73     }
74 
75 }
Client.java客户端
Java网络编程--套接字SocketJava网络编程--套接字Socket
 1 package loadget;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 
11 public class Server {
12 
13     /**
14      * @param args
15      */
16     public static void main(String[] args) {
17         // TODO Auto-generated method stub
18         ServerSocket server = null;
19         //接受文件
20         
21         
22         try {
23             server = new ServerSocket(7777);
24             
25             while(true)
26             {
27                 
28                 System.out.println("等待与客户端连接");
29                 final Socket sock = server.accept();
30                 System.out.println("与客户端建立连接");
31                 new Thread(new Runnable() {
32                     BufferedInputStream bis = null;
33                             //写文件
34                             BufferedOutputStream bos =null;
35                     @Override
36                     public void run() {
37                         // TODO Auto-generated method stub
38                         try {
39                             
40                             bis = new BufferedInputStream(sock.getInputStream());
41                             int a = (int)(Math.random()*10000+2);
42                             System.out.println("a:"+a);
43                             File fileName = new File("src/loadget/a.txt");
44                             bos = new BufferedOutputStream(new FileOutputStream(fileName));
45                             int len =0;
46                             byte bytes[] = new byte[1024];
47                             while((len=bis.read(bytes))!=-1)
48                             {
49                                 bos.write(bytes,0,len);
50                                 bos.flush();
51                             }
52                             
53                         } catch (IOException e) {
54                             // TODO Auto-generated catch block
55                             e.printStackTrace();
56                         }
57                         finally{
58                             try {
59                                 bos.close();
60                             } catch (IOException e) {
61                                 // TODO Auto-generated catch block
62                                 e.printStackTrace();
63                             }
64                             try {
65                                 bis.close();
66                             } catch (IOException e) {
67                                 // TODO Auto-generated catch block
68                                 e.printStackTrace();
69                             }
70                             try {
71                                 sock.close();
72                             } catch (IOException e) {
73                                 // TODO Auto-generated catch block
74                                 e.printStackTrace();
75                             }
76                         }
77                         
78                     }
79                 }).start();
80             }
81             
82             
83         } catch (IOException e) {
84             // TODO Auto-generated catch block
85             e.printStackTrace();
86         }    
87     }
88 }
Server.java服务器端

 

上一篇:Android手机tcpdump抓包


下一篇:《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之63---BREW 应用的开发流程