62 网络编程(三)——UDP编程

UDP编程标准步骤

服务器端

  • 使用DatagramSocket创建服务端:DatagramSocket server = new DatagramSocket(port);//参数为自定义端口号
  • 准备接受容器1:必须是byte数组
  • 使用DatagramPacket创建接受容器2:DatagramPacket packet = new DatagramPacket(容器1,start,length);//容器2的名字写为了packet
  • 接受数据:serve.receive(packet);
  • 处理数据:此时数据已经存储到容器1中,对容器1处理即可
  • 关闭:根据需要关闭服务端  server.close()

客户端

  • 使用DatagramSocket创建客户端:DatagramSocket client = new DatagramSocket(port);//参数为自定义端口号
  • 准备数据:必须是byte数组
  • 使用DatagramPacket创建packet包:DatagramPacket packet = new DatagramPacket(数据数组,start,数组length,new InetSocketAddress("目标主机地址",port));
  • 发送数据:client.send(packet);
  • 关闭 :client.close()

以下为一个实例

先启用服务器端,再启动客户端,然后查看服务器端的console结果

Server端

public static void main(String[] args) throws IOException{
		System.out.println("服务端启动");
		//创建客户端
		DatagramSocket server = new DatagramSocket(9917);
		//创建容器
		byte[] container = new byte[1024*60];
		//创建packet
		DatagramPacket packet = new DatagramPacket(container,0,container.length);
		//接收数据
		server.receive(packet);
		//处理数据
		System.out.println(new String(container,0,container.length));
		//关闭
		server.close();
	}

  

client端

public static void main(String[] args) throws IOException{
		System.out.println("客户端启动");
		//创建客户端
		DatagramSocket client = new DatagramSocket(12454);
		//准备数据
		String str = "最后一遍了";
		byte[] datas = str.getBytes();
		//创建packet
		DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9917));
		//发送
		client.send(packet);
		//关闭
		client.close();
	}

  

运行结果:

服务端启动
最后一遍了

  

多线程UDP发送与接收

我们将客户端与服务器端各写为一个实现了Runnale的类,这样在其它地方就可以使用线程来启动这两个端,达到双向交流的目的。

Server端

package _20191218;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * UDP实现接收
 * @author TEDU
 *
 */
public class ThreadReceive implements Runnable{
	private int port;
	public ThreadReceive(int port) {
		this.port = port;
	}
	public void run() {
		//创建服务器端
		System.out.println("服务端开启");
		DatagramSocket server = null;
		try {
			 server = new DatagramSocket(port);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//创建容器
		byte[] container;
		//创建packet
		DatagramPacket packet;
		while(true) {
			container = new byte[1024*10];
			packet = new DatagramPacket(container,0,container.length);
			//接收
			try {
				server.receive(packet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//处理
			String message = new String(container,0,packet.getLength());
			System.out.println(message);
//			System.out.println(message.equals("quit"));
			//释放
			if(message.equals("quit")) {
				break;
			}
		}
		server.close();//释放
	}
}

  

Client端

package _20191218;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;
/**
 * UDP实现发送
 * @author TEDU
 *
 */
public class ThreadSend implements Runnable{
	private int port;
	private int toPort;
	private String address;
	public ThreadSend(int port,int toPort,String address){
		this.port = port;
		this.toPort = toPort;
		this.address = address;
	}
	public void run() {
		//创建客户端
		System.out.println("客户端开启");
		DatagramSocket client = null;
		BufferedReader reader = null;
		Scanner scan = new Scanner(System.in);
		String message = null;
		byte[] datas; 
		DatagramPacket packet;
		try {
			reader = new BufferedReader(new InputStreamReader(System.in));
			client = new DatagramSocket(port);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//准备数据
		while(true) {
//			message = scan.nextLine();
			try {
				message = reader.readLine();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			datas = message.getBytes();
			//packet
			packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(address,toPort));
			//发送
			try {
				client.send(packet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			message = new String(datas,0,packet.getLength());
			if(message.equals("quit")) break;
		}
		client.close();
	}
}

  

用户1

package _20191218;

public class Talker1 {
	public static void main(String[] args) {
		//接收
		new Thread(new ThreadReceive(5577)).start();//接收端口 5577
		//发送
		new Thread(new ThreadSend(5566,6677,"localhost")).start();//发送端口 5566
	}
}

  

用户2

package _20191218;

public class Talker2 {
	public static void main(String[] args) {
		//接收
		new Thread(new ThreadReceive(6677)).start();//接收端口 6677
		//发送
		new Thread(new ThreadSend(6666,5577,"localhost")).start();
	}
}

  

运行测试

posted @ 2019-12-16 17:16  Scorpicat  阅读(212)  评论(0编辑  收藏  举报