?? datagramserver.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 + -