Java套接字

前言: 

  本文补充一下Java关于套接字方面的内容,因为其应用相对比较简单,所以下面介绍两个程序实例。

---------------------------------------------------------------------------------------

1.Socket对接过程

(注:这是之前在网上见过一张图片,大致过程还是比较清晰的)

分析:

  • Socket对接之后的数据传送类似于文件的读写;
  • 流套接字采用TCP传输数据流,一旦数据发送失败将重传,所以具有安全无损的特点。

2.实例一【Server->Client(1:1)】

 1 package socket;
 2 
 3 import java.io.DataInputStream;
 4 import java.io.DataOutputStream;
 5 import java.io.IOException;
 6 import java.net.Inet4Address;
 7 import java.net.InetAddress;
 8 import java.net.ServerSocket;
 9 import java.net.Socket;
10 import java.util.Date;
11 
12 public class Server {
13 
14     public Server(){
15         try {
16             //服务端开启端口8000
17             ServerSocket serverSocket = new ServerSocket(8000);
18             System.out.println("server start at:"+new Date());
19             //等待客户端相应
20             Socket socket  = serverSocket.accept();
21             InetAddress address = socket.getInetAddress();
22             System.out.println("connected with address:"+address.getHostAddress());
23             DataInputStream input = new DataInputStream(socket.getInputStream());
24             DataOutputStream output = new DataOutputStream(socket.getOutputStream());
25             
26             //while(true){
27             int num = input.readInt();
28             System.out.println(num);
29             //}
30         
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34     }
35 
36     public static void main(String[] args) {
37         new Server();
38     }
39 
40 }
 1 package socket;
 2 
 3 import java.io.DataInputStream;
 4 import java.io.DataOutputStream;
 5 import java.net.Socket;
 6 
 7 public class Client {
 8     public static void main(String[] args) throws Exception {
 9         
10         System.out.println("--client--");
11         
12         //Socket(,)内参数分别设为服务端IP和端口
13         Socket socket = new Socket("localhost", 8000);
14 
15         DataInputStream input = new DataInputStream(socket.getInputStream());
16         DataOutputStream output = new DataOutputStream(socket.getOutputStream());
17         
18         int x = 222;
19         output.writeInt(x);
20         System.out.println("data convented:"+x);
21     }
22 }

服务端输出(执行之后显示红点,表示等待中...):

 

server start at:Sat Aug 15 19:39:40 CST 2015
connected with address:127.0.0.1
222

客户端输出:

--client--
data convented:222

 

3.实例二【Server->Client(1:n)】

 1 package socket;
 2 
 3 import java.io.DataInputStream;
 4 import java.io.DataOutputStream;
 5 import java.net.InetAddress;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 import java.util.Date;
 9 
10 public class SuperServer {
11     private int countOfClient=0;
12     public SuperServer() {
13         try {
14             ServerSocket serverSocket = new ServerSocket(8000);
15             System.out.println("server start at:" + new Date());
16 
17             while (true) {
18                 Socket socket = serverSocket.accept();
19                 InetAddress address = socket.getInetAddress();
20                 System.out.println("connected with address:"
21                         + address.getHostAddress());
22                 //为每一位Client创建一个单独的线程
23                 new Thread(new ConnectTask(socket)).start();
24                 countOfClient++;
25                 System.out.println("this is the "+countOfClient + "(th) of client. ");
26             }
27         } catch (Exception e) {
28             e.printStackTrace();
29         }
30 
31     }
32 
33     public static void main(String[] args) {
34         new SuperServer();
35     }
36 
37     class ConnectTask implements Runnable {
38         private Socket socket = null;
39 
40         public ConnectTask(Socket socket) {
41             this.socket = socket;
42         }
43 
44         @Override
45         public void run() {
46             try {
47                 DataInputStream input = new DataInputStream(
48                         socket.getInputStream());
49                 DataOutputStream output = new DataOutputStream(
50                         socket.getOutputStream());
51                 
52                 int numFromClient = input.readInt();
53                 System.out.println("number from client:"+numFromClient);
54             } catch (Exception e) {
55                 e.printStackTrace();
56             }
57         }
58 
59     }
60 
61 }

  以上代码中,服务端创建多线程,给每一个对接的客户端分一个独立的线程;客户端代码不变,还是实例一的代码。程序输出跟实例一近似,只不过这是服务端一直运行着,可供多个客户端连接。(因为独立分配线程,程序运行时资源比较浪费,所以代码仅供参考,有待优化!)

 

posted @ 2015-08-15 20:03  Sea_Sky  阅读(2680)  评论(0编辑  收藏  举报

转载请注明出处! About me