Java UDP伺服器和客戶端原始碼 -javarevisited
Java中的DatagramPacket和DatagramSocket類支援在應用程式級別利用UDP套接字通訊。讓我們用Java編寫一個簡單的伺服器和一個客戶端,它們使用UDP套接字通訊相互通訊。
在此示例中,套接字伺服器String從客戶端接收型別的資料,並將大寫的字串傳送回客戶端。
public class UDPServer{ // Server UDP socket runs at this port public final static int SERVICE_PORT=50001; public static void main(String[] args) throws IOException{ try{ // Instantiate a new DatagramSocket to receive responses from the client DatagramSocket serverSocket = new DatagramSocket(SERVICE_PORT); /* Create buffers to hold sending and receiving data. It temporarily stores data in case of communication delays */ byte[] receivingDataBuffer = new byte[1024]; byte[] sendingDataBuffer = new byte[1024]; /* Instantiate a UDP packet to store the client data using the buffer for receiving data*/ DatagramPacket inputPacket = new DatagramPacket(receivingDataBuffer, receivingDataBuffer.length); System.out.println("Waiting for a client to connect..."); // Receive data from the client and store in inputPacket serverSocket.receive(inputPacket); // Printing out the client sent data String receivedData = new String(inputPacket.getData()); System.out.println("Sent from the client: "+receivedData); /* * Convert client sent data string to upper case, * Convert it to bytes * and store it in the corresponding buffer. */ sendingDataBuffer = receivedData.toUpperCase().getBytes(); // Obtain client's IP address and the port InetAddress senderAddress = inputPacket.getAddress(); int senderPort = inputPacket.getPort(); // Create new UDP packet with data to send to the client DatagramPacket outputPacket = new DatagramPacket( sendingDataBuffer, sendingDataBuffer.length, senderAddress,senderPort ); // Send the created packet to client serverSocket.send(outputPacket); // Close the socket connection serverSocket.close(); } catch (SocketException e){ e.printStackTrace(); } } } |
客戶端:
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class UDPClient{ /* The server port to which the client socket is going to connect */ public final static int SERVICE_PORT = 50001; public static void main(String[] args) throws IOException{ try{ /* Instantiate client socket. No need to bind to a specific port */ DatagramSocket clientSocket = new DatagramSocket(); // Get the IP address of the server InetAddress IPAddress = InetAddress.getByName("localhost"); // Creating corresponding buffers byte[] sendingDataBuffer = new byte[1024]; byte[] receivingDataBuffer = new byte[1024]; /* Converting data to bytes and storing them in the sending buffer */ String sentence = "Hello from UDP client"; sendingDataBuffer = sentence.getBytes(); // Creating a UDP packet DatagramPacket sendingPacket = new DatagramPacket(sendingDataBuffer,sendingDataBuffer.length,IPAddress, SERVICE_PORT); // sending UDP packet to the server clientSocket.send(sendingPacket); // Get the server response .i.e. capitalized sentence DatagramPacket receivingPacket = new DatagramPacket(receivingDataBuffer,receivingDataBuffer.length); clientSocket.receive(receivingPacket); // Printing the received data String receivedData = new String(receivingPacket.getData()); System.out.println("Sent from the server: "+receivedData); // Closing the socket connection with the server clientSocket.close(); } catch(SocketException e) { e.printStackTrace(); } } } |
執行,在伺服器端看到:
Waiting for a client to connect... Sent from the client: Hello from UDP client |
客戶端看到:
sent from the server: HELLO FROM UDP CLIENT |
相關文章
- Redis原始碼剖析——客戶端和伺服器Redis原始碼客戶端伺服器
- TCP/UDP簡易通訊框架原始碼,支援輕鬆管理多個TCP服務端(客戶端)、UDP客戶端TCPUDP框架原始碼服務端客戶端
- 【windows socket+UDP伺服器客戶端】WindowsUDP伺服器客戶端
- Tars-Java客戶端原始碼分析Java客戶端原始碼
- 002 Rust 網路程式設計,實現 UDP 伺服器和客戶端Rust程式設計UDP伺服器客戶端
- node.js實現的簡單udp廣播伺服器和客戶端程式碼例項Node.jsUDP伺服器客戶端
- Linux 伺服器zabbix原始碼客戶端(agent)安裝Linux伺服器原始碼客戶端
- Golang 實現客戶端與伺服器端UDP協議連線通訊Golang客戶端伺服器UDP協議
- Java Netty伺服器客戶端聊天示範程式碼JavaNetty伺服器客戶端
- Telegram原始碼之安卓客戶端配置原始碼安卓客戶端
- MQTT伺服器搭建服務端和客戶端MQQT伺服器服務端客戶端
- java websocket 客戶端JavaWeb客戶端
- 《球球大作戰》原始碼解析:伺服器與客戶端架構原始碼伺服器客戶端架構
- 【Java】Java多執行緒實現的聊天客戶端和伺服器Java執行緒客戶端伺服器
- MapReduce——客戶端提交任務原始碼分析客戶端原始碼
- java獲取客戶端ip和macJava客戶端Mac
- 使用Netty實現HTTP2伺服器/客戶端的原始碼和教程 - BaeldungNettyHTTP伺服器客戶端原始碼
- ubuntu 下面 svn 伺服器端和客戶端的配置和使用Ubuntu伺服器客戶端
- python 實現 TCP、UDP 客戶端最簡流程PythonTCPUDP客戶端
- Netty原始碼分析(三):客戶端啟動Netty原始碼客戶端
- Zookeeper Java 客戶端搭建Java客戶端
- Zookeeper--Java客戶端Java客戶端
- Java 客戶端 Jedis和JedisPool 連線池Java客戶端
- React 伺服器端渲染和客戶端渲染效果對比React伺服器客戶端
- UNIX網路程式設計學習(18)--UDP回射(伺服器+客戶端)程式程式設計UDP伺服器客戶端
- rsync客戶端一鍵安裝rsync指令碼(原始碼)客戶端指令碼原始碼
- 藍芽客戶端和伺服器的實現藍芽客戶端伺服器
- 實現伺服器和客戶端資料互動,Java Socket有妙招伺服器客戶端Java
- plsql 客戶端亂碼SQL客戶端
- MQTT客戶端JAVA程式碼----fusesource mqtt-clientMQQT客戶端Javaclient
- xxl-job原始碼閱讀一(客戶端)原始碼客戶端
- 走近原始碼:Redis命令執行過程(客戶端)原始碼Redis客戶端
- Fabric 1.0原始碼分析(36) Peer #EndorserClient(Endorser客戶端)原始碼client客戶端
- zookeeper的Java客戶端APIJava客戶端API
- JAVA FTP客戶端問題JavaFTP客戶端
- 《samba搭建win客戶端和linux客戶端的區別》Samba客戶端Linux
- 求助:c#客戶端如何跟java伺服器通訊C#客戶端Java伺服器
- Angular 伺服器端渲染場景裡,伺服器端和客戶端渲染出的 HTML 原始碼有可能不完全一致Angular伺服器客戶端HTML原始碼