udp.java

来自「简单清晰具有启发性的实例。 Net1和Client1类实现了TCP的简单应用。」· Java 代码 · 共 29 行

JAVA
29
字号
import java.net.*;
import java.util.*;
public class Udp{
	DatagramSocket ds;
	DatagramPacket dp;
	InetAddress addr;
	
	public static final int PORT = 1111;
	public static void main(String[] args)throws Exception{
			Udp udp = new Udp(args[0]);
			udp.go();
		}
	public Udp(String target)throws Exception{
			addr = InetAddress.getByName(target);
			ds = new DatagramSocket();
		}
	public void go()throws Exception{
			byte[] buff;
			for(;;){
					Thread.sleep(2000);
					System.out.println("sending");
					String s = (new Date()).toString();
					buff = s.getBytes();
					dp = new DatagramPacket(buff,buff.length,addr,PORT);
					ds.send(dp);
				}
		}
	
}

⌨️ 快捷键说明

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