UDP Socket(long)

来源:互联网 发布:贵州网络推广招聘 编辑:程序博客网 时间:2024/06/07 05:38

UDP Socket 发送long类型数据包:

package Long;import java.net.*;import java.io.*;public class Server {public static void main(String[] args) throws Exception{byte[] buf=new byte[1024];DatagramPacket dp=new DatagramPacket(buf,buf.length);DatagramSocket ds=new DatagramSocket(5555);while(true){ds.receive(dp);ByteArrayInputStream bis=new ByteArrayInputStream(buf);DataInputStream dis=new DataInputStream(bis);System.out.println(dis.readLong());}}}


 

package Long;import java.net.*;import java.io.*;public class Client {public static void main(String[] args) throws Exception{long l=10000l;ByteArrayOutputStream bos=new ByteArrayOutputStream();DataOutputStream dos=new DataOutputStream(bos);dos.writeLong(l);byte[] buf=bos.toByteArray();DatagramPacket dp=new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",5555));DatagramSocket ds=new DatagramSocket();for(int i=1;i<=5;i++){ds.send(dp);}ds.close();}}