Java 套接字编程 - 套接字服务器、客户端示例
欢迎来到 Java Socket 编程示例。每个服务器都是在特定系统上运行并侦听特定端口的程序。套接字绑定到端口号,当我们运行任何服务器时,它只会侦听套接字并等待客户端请求。例如,在端口 8080 上运行的 tomcat 服务器等待客户端请求,一旦收到任何客户端请求,它就会响应它们。
Java 套接字编程
套接字是网络上运行的两个程序之间的双向通信链路的一个端点。套接字绑定到端口号,以便 TCP 层可以识别要发送数据的应用程序。在 Java 套接字编程示例教程中,我们将学习如何编写Java 套接字服务器和Java 套接字客户端程序。我们还将学习服务器客户端程序如何在套接字上读取和写入数据。java.net.Socket和java.net.ServerSocket是实现 Socket 和 Socket 服务器的 Java 类。
Java 套接字服务器示例
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* This class implements java Socket server
* @author pankaj
*
*/
public class SocketServerExample {
//static ServerSocket variable
private static ServerSocket server;
//socket server port on which it will listen
private static int port = 9876;
public static void main(String args[]) throws IOException, ClassNotFoundException{
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for the client request");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//write object to Socket
oos.writeObject("Hi Client "+message);
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down Socket server!!");
//close the ServerSocket object
server.close();
}
}
Java 套接字客户端
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* This class implements java socket client
* @author pankaj
*
*/
public class SocketClientExample {
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException{
//get the localhost IP address, if server is running on some other IP, you need to use that
InetAddress host = InetAddress.getLocalHost();
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
for(int i=0; i<5;i++){
//establish socket connection to server
socket = new Socket(host.getHostName(), 9876);
//write to socket using ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Sending request to Socket Server");
if(i==4)oos.writeObject("exit");
else oos.writeObject(""+i);
//read the server response message
ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message: " + message);
//close resources
ois.close();
oos.close();
Thread.sleep(100);
}
}
}
要测试服务器-客户端通信的 Java 套接字编程,首先我们需要运行SocketServerExample
类。当您运行套接字服务器时,它只会打印“等待客户端请求”,然后等待客户端请求。现在,当您运行 SocketClientExample 类时,它将向 Java 套接字服务器发送请求并将响应消息打印到控制台。这是 Java 套接字服务器程序的输出SocketServerExample
。
Waiting for the client request
Message Received: 0
Waiting for the client request
Message Received: 1
Waiting for the client request
Message Received: 2
Waiting for the client request
Message Received: 3
Waiting for the client request
Message Received: exit
Shutting down Socket server!!
以下是 Java 套接字客户端程序的输出SocketClientExample
。
Sending request to Socket Server
Message: Hi Client 0
Sending request to Socket Server
Message: Hi Client 1
Sending request to Socket Server
Message: Hi Client 2
Sending request to Socket Server
Message: Hi Client 3
Sending request to Socket Server
Message: Hi Client exit
这就是 Java 中 Socket 编程的快速总结。希望您能够开始使用 Java Socket 服务器和 Java Socket 客户端编程。参考:Oracle Doc