标签:style blog io ar color os sp java for
package datagram; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class DatagramTester { private int port = 8000; private DatagramSocket sendSocket; private DatagramSocket receiveSocket; private static final int MAX_LENGTH = 3584; public DatagramTester() throws IOException { sendSocket = new DatagramSocket(); receiveSocket = new DatagramSocket(port); receiver.start(); sender.start(); } public static byte[] longToByte(long[] data) throws IOException { ByteArrayOutputStream bao = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bao); for(int i=0; i<data.length; i++) { dos.writeLong(data[i]); } dos.close(); return bao.toByteArray(); } public static long[] byteToLong(byte[] data) throws IOException { long[] result = new long[data.length/8]; ByteArrayInputStream bai = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bai); for(int i=0; i<data.length/8; i++) { result[i] = dis.readLong(); } return result; } public void send(byte[] bigData) throws IOException { DatagramPacket packet = new DatagramPacket(bigData, 0, 512, InetAddress.getByName("localhost"), port); int bytesSent = 0; int count = 0; while(bytesSent < bigData.length) { sendSocket.send(packet); System.out.println("SendSocket > 第" + (++count) + "次发送了" + packet.getLength() + "个字节"); bytesSent += packet.getLength(); int remain = bigData.length - bytesSent; int length = (remain > 512) ? 512 : remain; packet.setData(bigData, bytesSent, length); } } public byte[] receive() throws IOException { byte[] bigData = new byte[MAX_LENGTH]; DatagramPacket packet = new DatagramPacket(bigData, 0, MAX_LENGTH); int bytesReceived = 0; int count = 0; long beginTime = System.currentTimeMillis(); while((bytesReceived < bigData.length) && (System.currentTimeMillis() - beginTime < 60000*5)) { receiveSocket.receive(packet); System.out.println("ReceiveSocket > 第" + (++count) + "次接收到" + packet.getLength() + "个字节"); bytesReceived += packet.getLength(); packet.setData(bigData, bytesReceived, MAX_LENGTH - bytesReceived); } return packet.getData(); } public Thread sender = new Thread() { public void run() { long[] longArray = new long[MAX_LENGTH/8]; for(int i=0; i<longArray.length; i++) { longArray[i] = i+1; } try { send(longToByte(longArray)); } catch (IOException e) { e.printStackTrace(); } } }; public Thread receiver = new Thread() { public void run() { try { long[] longArray = byteToLong(receive()); for(int i=0; i<longArray.length; i++) { if(i%100 == 0) System.out.println(); System.out.print(longArray[i] + ""); } } catch (IOException e) { e.printStackTrace(); } } }; public static void main(String[] args) throws IOException { new DatagramTester(); } }
同一个 DatagramPacket 对象可以被重用,用来多次发送或接收数据
标签:style blog io ar color os sp java for
原文地址:http://www.cnblogs.com/starzou/p/4128128.html