udp实现数据传输,怎么老有问题 请指点下
服务器端:
import java.net.*;
public class ServerUdp
{
public static void main(String[] args)
{try {byte[]buff=new byte[256];
InetAddress address = InetAddress.getByName( "127.0.0.1 ");
System.out.println( "Checking at: " + address);
DatagramSocket socket=new DatagramSocket(8000);
DatagramPacket topacket=new DatagramPacket(buff,buff.length,address,8000);
System.out.println( "Server started ");
socket.receive(topacket);
System.out.println( "received! ");
}catch(Exception e){System.out.println( "Not received! ");}
}
}
客户端(发送数据)
import java.net.*;
class ClientUdp
{
public static void main(String[] args)
{ try{
InetAddress address = InetAddress.getByName( "127.0.0.1 ");
System.out.println( "Checking at: " + address);
DatagramSocket socket=new DatagramSocket(8000);
byte[]buff=new byte[256];
String str= "-help ";
buff=str.getBytes();
DatagramPacket frompacket=new DatagramPacket(buff,buff.length,address,8000);
System.out.println( "translating.. ");
socket.send(frompacket);
System.out.println( "pass ");
}catch(Exception e){ System.out.println( "error! ");
e.printStackTrace();
}
}
}
是不绑定的问题?
怎么解决
------解决方案--------------------你没说是什么问题啊? 唯一的带有问号的那一句还特别奇怪.