byte[] b=new byte[1024];
    b=baos.toByteArray();
    try {
        DatagramSocket ds=new Datagramsocket(1212);//1212为本程序接受包的端口
DatagramPacket dp=new DatagramPacket(b,b.length,new InetSocketAddress("127.0.0.1",8877));
        //192.168.1.14为服务器Ip,8877为服务器接收包的端口
try {
ds.send(dp);
System.out.println("succes to send to the ClientNewMsg!"); } catch (IOException e) {
e.printStackTrace();
}
} catch (SocketException e) {
e.printStackTrace();
}经测试,System.out.println("succes to send to the ClientNewMsg!");能被打印出来,这应该是发送成功了吧!
但是服务器端这边:却接受不到任何信息,这究竟是哪里出现问题呢?高手指点下!我这里还有源程序,需要的说声,我贴上!
DatagramSocket ds=null;
byte[] b=new byte[1024];
try {
ds=new DatagramSocket(8877);
while (ds!=null){
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
System.out.println("receive a packet111111111111111111111");
for(int i=0;i<Clients.size();i++){
Client c=Clients.get(i);
dp.setSocketAddress(new InetSocketAddress(c.ip,c.port));
ds.send(dp);
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}