udp

socket

  • 通信的两端都有socket
  • 网络通信其实就是socket间的通信
  • 数据在两个socket间通过IO传输

udp接收和发送数据

/**
* 创建UDP发送端
* 1,建立udp的socket服务
* 2,将要发送的数据封装到数据包
* 3,通过udp的socket服务发送数据包
* 4,关闭socket服务
*/
System.out.println("发送端");

DatagramSocket dSocket = new DatagramSocket();

String string = "udp来了";
byte[] buf = string.getBytes();
DatagramPacket dPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.0.102"),10000);

dSocket.send(dPacket);

dSocket.close();

/**
* UDP接收端
* 1,建立UDP的socket服务
* 2,创建数据包,存储接收到的数据,用数据包的方法解析数据
* 3,使用socket服务的receive方法将接收到的数据存储到数据包
* 4,数据包解析数据
* 5,关闭资源
*/
System.out.println("接收端");

DatagramSocket dpSocket = new DatagramSocket(10000);

byte[] buf = new byte[1024];
DatagramPacket dPacket = new DatagramPacket(buf, buf.length);

dpSocket.receive(dPacket);

String ipString = dPacket.getAddress().getHostAddress();
int port = dPacket.getPort();
String text=new String(dPacket.getData(),0,dPacket.getLength());
System.out.println(ipString + port + text);
dpSocket.close();

聊天

public class Send implements Runnable {

	private DatagramSocket ds;
	
	public Send(DatagramSocket ds) {
		this.ds = ds;
	}
	
	@Override
	public void run() {
		try {
			BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
			String lineString = null;
			while((lineString=bufr.readLine())!=null) {
				byte[] buf = lineString.getBytes();
				DatagramPacket dPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.0.102"),10000);
				
				ds.send(dPacket);
				if("exit".equals(lineString)) {
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

public class Recive implements Runnable {

	private DatagramSocket ds;
	
	public Recive(DatagramSocket ds) {
		this.ds = ds;
	}
	
	@Override
	public void run() {
		try {
			while(true) {
				byte[] buf = new byte[1024];
				DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
				
				ds.receive(dPacket);
				
				String ipString = dPacket.getAddress().getHostAddress();
				int port = dPacket.getPort();
				String text=new String(dPacket.getData(),0,dPacket.getLength());
				System.out.println(ipString + port + text);
				if(text.equals("exit")) {
					System.out.println("退出聊天室");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ds.close();
		}
	}

}

public class Chat {
	public static void main(String[] args) throws SocketException {
		DatagramSocket send = new DatagramSocket();
		DatagramSocket rece = new DatagramSocket(10000);
		
		new Thread(new Send(send)).start();
		new Thread(new Recive(rece)).start();
	}
}

传送类型数据

public class UDPSend {
	public static void main(String[] args) throws IOException {
		/**
		 * 创建UDP发送端
		 * 1,建立udp的socket服务
		 * 2,将要发送的数据封装到数据包
		 * 3,通过udp的socket服务发送数据包
		 * 4,关闭socket服务
		 */
		System.out.println("发送端");
		
		DatagramSocket dSocket = new DatagramSocket();
		
//		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//		String lineString = null;
//		while((lineString=bufr.readLine())!=null) {
//			byte[] buf = lineString.getBytes();
//			DatagramPacket dPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.0.102"),10000);
//			
//			dSocket.send(dPacket);
//			if("exit".equals(lineString)) {
//				break;
//			}
//		}
		byte[] buf = convert(81.23);
		DatagramPacket dPacket = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.0.102"),10000);
		
		dSocket.send(dPacket);
		
		dSocket.close();
	}
	
	public static byte[] convert(double num) throws IOException {
		byte[] data=null;
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(bos);
		dos.writeDouble(num);
		dos.flush();
		data = bos.toByteArray();
		return data;
	}
}

public class UDPRecive {
	public static void main(String[] args) throws IOException {
		/**
		 * UDP接收端
		 * 1,建立UDP的socket服务
		 * 2,创建数据包,存储接收到的数据,用数据包的方法解析数据
		 * 3,使用socket服务的receive方法将接收到的数据存储到数据包
		 * 4,数据包解析数据
		 * 5,关闭资源
		 */
		System.out.println("接收端");
		
		DatagramSocket dpSocket = new DatagramSocket(10000);
		
		byte[] buf = new byte[1024];
		DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
		while(true) {
			dpSocket.receive(dPacket);
			
			String ipString = dPacket.getAddress().getHostAddress();
			int port = dPacket.getPort();
			//String text=new String(dPacket.getData(),0,dPacket.getLength());
			System.out.println(ipString + " " + port + " "+convert(dPacket.getData()));
		}
		
		
		//dpSocket.close();
		
	}
	public static double convert(byte[] buf) throws IOException {
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf));
		double num = dis.readDouble();
		dis.close();
		return num;
	}
}


书籍推荐