本帖最后由 Sword 于 2013-5-9 22:34 编辑
我按照视频编了个键盘输入,然后发送到接受端的程序。但是老是提示错误。我改了很久,没改出来错误,求高手指点。
接受端:
import java.net.*;
import java.io.*;
public class UDPRecKeyIn {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DatagramSocket ds=new DatagramSocket(10001);
while(true)
{
byte[] by=new byte[1024];
DatagramPacket dp=new DatagramPacket(by,by.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,by.length );
System.out.println(ip+data);
}
}
}
发送端package day23;
import java.net.*;
import java.io.*;
public class UDPSendkeyIN {
public static void main(String[] args)throws Exception {
DatagramSocket ds=new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equalsIgnoreCase(line))
break;
byte[] by=new byte[1024];
DatagramPacket dp=new DatagramPacket(by,by.length,InetAddress.getByName("192.168.1.254"),10001);
ds.send(dp);
}
ds.close();
}
}
|
|