Java套接字编程–套接字服务器,客户端示例
时间:2020-02-23 14:36:51 来源:igfitidea点击:
欢迎使用Java Socket编程示例。
每个服务器都是在特定系统上运行并在特定端口上侦听的程序。
套接字绑定到端口号,当我们运行任何服务器时,它仅侦听套接字并等待客户端请求。
例如,运行在端口8080上的tomcat服务器等待客户端请求,一旦获得任何客户端请求,它就会响应它们。
Java套接字编程
套接字是网络上运行的两个程序之间双向通信链接的一个端点。
套接字绑定到一个端口号,以便TCP层可以识别应发送数据的应用程序。
在Java套接字编程示例教程中,我们将学习如何编写Java套接字服务器和Java套接字客户端程序。
我们还将学习服务器客户端程序如何在套接字上读写数据。
java.net.Socket和java.net.ServerSocket是实现Socket和Socket服务器的Java类。
Java套接字服务器示例
package com.theitroad.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 hyman * */ 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.theitroad.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 hyman * */ 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类。
当您运行套接字服务器时,它将仅显示" Waiting for client request"(等待客户端请求),然后等待客户端请求。
现在,当您运行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