⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramserver.java

📁 SSD8练习3 自己做的 拿出来交流交流
💻 JAVA
字号:
/**
 * This class models a server using UDP to serve the client.
 * 
 * @author tyrant
 * @version 1.0.0
 */

import java.net.*;

public class DatagramServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		/* datagram socket to serve the request from the client. */
		DatagramSocket serverSocket = new DatagramSocket(6000);
		System.out.println("Server starts...");

		/* byte arrays to store one byte to receive and to send. */
		byte[] receiveByte = new byte[1];
		byte[] sendByte = new byte[1];

		/*
		 * solve the request from the client all the time.
		 */
		while (true) {
			// get data from the client
			DatagramPacket receivePacket = new DatagramPacket(receiveByte,
					receiveByte.length);
			serverSocket.receive(receivePacket);

			// send data to the client
			InetAddress IPAddress = receivePacket.getAddress();
			int port = receivePacket.getPort();
			byte send = receivePacket.getData()[0];
			sendByte[0] = send;
			DatagramPacket sendPacket = new DatagramPacket(sendByte,
					sendByte.length, IPAddress, port);
			serverSocket.send(sendPacket);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -