//实现简单的UDP通讯。
import java.net.*;
import java.io.*;
class UdpSent2
{
public static void main(String[] args)throws Exception
{
DatagramSocket ds = new DatagramSocket(10003);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//byte[] b1;
String line = null;
while((line = br.readLine())!=null)
{
if("886".equals(line));
break;
byte[] buf= line.getBytes();//怎么这句代码老是报这个错误:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
//Unreachable code
//at UdpSent2.main(UdpSent2.java:17)
//这个错误是什么意思呀?????
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10005);
ds.send(dp);
}
ds.close();
}
}
class UdpReiceve2
{
public static void main(String[] args)throws Exception
{
DatagramSocket ds = new DatagramSocket(10005);
while(true)
{
byte[] b = new byte[1024*64];
DatagramPacket dp = new DatagramPacket(b,b.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
int p = dp.getPort();
System.out.println(ip);
System.out.println(data);
System.out.println(p);
}
}
}
|
|