Java Socket 编程 - Socket 服务器、客户端示例

欢迎来到Java Socket编程示例. 每个服务器都是在特定系统上运行的程序,并在特定端口上收听。 接口被绑定到端口号码,当我们运行任何服务器时,它只是在接口上收听并等待客户端请求。

Java Socket 编程

java socket, java socket programming, java socket example A socket is one endpoint of a two-way communication link between two programs running on the network. The socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. In java socket programming example tutorial, we will learn how to write java socket server and java socket client program. We will also learn how server client program read and write data on the socket. java.net.Socket and java.net.ServerSocket are the java classes that implements Socket and Socket server.

Java Socket 服务器示例

 1package com.journaldev.socket;
 2
 3import java.io.IOException;
 4import java.io.ObjectInputStream;
 5import java.io.ObjectOutputStream;
 6import java.lang.ClassNotFoundException;
 7import java.net.ServerSocket;
 8import java.net.Socket;
 9
10/**
11 * This class implements java Socket server
12 * @author pankaj
13 *
14 */
15public class SocketServerExample {
16
17    //static ServerSocket variable
18    private static ServerSocket server;
19    //socket server port on which it will listen
20    private static int port = 9876;
21
22    public static void main(String args[]) throws IOException, ClassNotFoundException{
23        //create the socket server object
24        server = new ServerSocket(port);
25        //keep listens indefinitely until receives 'exit' call or program terminates
26        while(true){
27            System.out.println("Waiting for the client request");
28            //creating socket and waiting for client connection
29            Socket socket = server.accept();
30            //read from socket to ObjectInputStream object
31            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
32            //convert ObjectInputStream object to String
33            String message = (String) ois.readObject();
34            System.out.println("Message Received: " + message);
35            //create ObjectOutputStream object
36            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
37            //write object to Socket
38            oos.writeObject("Hi Client "+message);
39            //close resources
40            ois.close();
41            oos.close();
42            socket.close();
43            //terminate the server if client sends exit request
44            if(message.equalsIgnoreCase("exit")) break;
45        }
46        System.out.println("Shutting down Socket server!!");
47        //close the ServerSocket object
48        server.close();
49    }
50
51}

Java Socket 客户端

 1package com.journaldev.socket;
 2
 3import java.io.IOException;
 4import java.io.ObjectInputStream;
 5import java.io.ObjectOutputStream;
 6import java.net.InetAddress;
 7import java.net.Socket;
 8import java.net.UnknownHostException;
 9
10/**
11 * This class implements java socket client
12 * @author pankaj
13 *
14 */
15public class SocketClientExample {
16
17    public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
18        //get the localhost IP address, if server is running on some other IP, you need to use that
19        InetAddress host = InetAddress.getLocalHost();
20        Socket socket = null;
21        ObjectOutputStream oos = null;
22        ObjectInputStream ois = null;
23        for(int i=0; i<5;i++){
24            //establish socket connection to server
25            socket = new Socket(host.getHostName(), 9876);
26            //write to socket using ObjectOutputStream
27            oos = new ObjectOutputStream(socket.getOutputStream());
28            System.out.println("Sending request to Socket Server");
29            if(i==4)oos.writeObject("exit");
30            else oos.writeObject(""+i);
31            //read the server response message
32            ois = new ObjectInputStream(socket.getInputStream());
33            String message = (String) ois.readObject();
34            System.out.println("Message: " + message);
35            //close resources
36            ois.close();
37            oos.close();
38            Thread.sleep(100);
39        }
40    }
41}

要测试 Java 接口编程的服务器-客户端通信,我们首先需要运行SocketServerExample类。当你运行接口服务器时,它只会打印等待客户端请求,然后等待客户端请求。

 1Waiting for the client request
 2Message Received: 0
 3Waiting for the client request
 4Message Received: 1
 5Waiting for the client request
 6Message Received: 2
 7Waiting for the client request
 8Message Received: 3
 9Waiting for the client request
10Message Received: exit
11Shutting down Socket server!!

以下是Java插件客户端SocketClientExample程序的输出。

 1Sending request to Socket Server
 2Message: Hi Client 0
 3Sending request to Socket Server
 4Message: Hi Client 1
 5Sending request to Socket Server
 6Message: Hi Client 2
 7Sending request to Socket Server
 8Message: Hi Client 3
 9Sending request to Socket Server
10Message: Hi Client exit

我希望你可以开始使用 java 接口服务器和 java 接口客户端编程。

Published At
Categories with 技术
Tagged with
comments powered by Disqus