谁有java udp 广播的代码哦 我在网上找了下,用了几个,发现都有问题,因此想问下,谁有一个在局域网里,java udp广播 的代码哦,请给一个哦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DatagramPacket pd = new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), port); DatagramSocket sendSocket = new DatagramSocket(); sendSocket.setBroadcast(true); sendSocket.send(pd); import java.net.*;class WriteServer{ public static int serverPort = 666 ; public static int clientPort = 999 ; public static int buffer_size = 1024 ; public static DatagramSocket ds ; public static byte buffer[] = new byte[buffer_size]; public static void TheServer() throws Exception{ int pos = 0; while(true){ int c = System.in.read(); switch(c){ case -1: System.out.println("Server Quits."); return; case '\r': break; case '\n': ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientPort)); pos = 0; break; default: buffer[pos++] = (byte)c; } } } public static void TheClient() throws Exception{ while(true){ DatagramPacket p = new DatagramPacket (buffer,buffer.length); ds.receive(p); System.out.println(new String(p.getData(),0,p.getLength())); } } public static void main(String[] args) throws Exception{ if(args.length == 1){ ds = new DatagramSocket(serverPort); TheServer(); }else{ ds = new DatagramSocket(clientPort); TheClient(); } }} 运行 java WriteServer然后 java WriteServer 1 java更改系統時間的問題 新手求助,带中文字符串分割问题 帮忙看下这个Java键入程序(The type keybord must implement the inherited abstract method ) 懂C的进来 碰到一个算法题,答案是用C写的,很简洁,谁来把它转换成Java语言? 用JAVA求出2組日期相差多少天? JAVA中TreeSet的问题 请问各位高人,北大清鸟的ACCP培训怎么样??? 困惑? classpath 请教:有关uml中类图的一些问题??? 下面是一道Q&A,大家看一下 netbeans中为什么有的html文件打开错误 java类命名规则
DatagramPacket pd = new DatagramPacket(data, data.length,
InetAddress.getByName("255.255.255.255"), port);
DatagramSocket sendSocket = new DatagramSocket();
sendSocket.setBroadcast(true);
sendSocket.send(pd);
class WriteServer{
public static int serverPort = 666 ;
public static int clientPort = 999 ;
public static int buffer_size = 1024 ;
public static DatagramSocket ds ;
public static byte buffer[] = new byte[buffer_size]; public static void TheServer() throws Exception{
int pos = 0;
while(true){
int c = System.in.read();
switch(c){
case -1:
System.out.println("Server Quits.");
return;
case '\r':
break;
case '\n':
ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientPort));
pos = 0;
break;
default:
buffer[pos++] = (byte)c;
}
}
}
public static void TheClient() throws Exception{
while(true){
DatagramPacket p = new DatagramPacket (buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
public static void main(String[] args) throws Exception{
if(args.length == 1){
ds = new DatagramSocket(serverPort);
TheServer();
}else{
ds = new DatagramSocket(clientPort);
TheClient();
}
}
} 运行 java WriteServer
然后 java WriteServer 1