Java实现UDP之Echo客户端和服务端
代码内容
采用UDP协议编写服务器端代码(端口任意)
编写客户机的代码访问该端口
客户机按行输入
服务器将收到的字符流和接收到的时间输出在服务器console
原样返回给客户机在客户机console显示出来
代码实现
/* UDPEchoClient.java */
import java.io.*;
import java.net.*;
public class UDPEchoClient {
public final static String serverIP = "localhost";
public final static int serverPort = 13;
public static String userName = null;
@SuppressWarnings("resource")
public static void main(String[] args) {
DatagramSocket client = null;
try {
/* 获取服务器地址 */
InetAddress server = InetAddress.getByName(serverIP);
/* 创建客户端DatagramSocket */
client = new DatagramSocket();
/* 从用户键盘读取输入 */
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));