java网络编程之UDP

java网络编程之UDP

java网络编程基于UDP数据报,发送循环发送数据,当发送byby时终止连接,服务端等待接收,当收到byby时服务器关闭连接

Sender发送端代码:

package cn.csuft.poorguy.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Sender {
	public static void main(String[] args) {
		
		DatagramSocket socket = null;
		try {
			socket = new DatagramSocket();
		} catch (SocketException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()) {
			String msg = cin.nextLine();
			//UDP套接字
			try {
				
				byte []data = msg.getBytes();
				InetAddress address = InetAddress.getByName("127.0.0.1");
				int port = 7000;
				
				//创建数据包
				DatagramPacket packet = new DatagramPacket(
						data,
						data.length,
						address,
						port
						);
				
				//发送
				socket.send(packet);
				if(msg.equals("byby")) {
					socket.close();
					System.out.println("发送结束");
					break;
				}
				System.out.println("send");
			} catch (SocketException e) {
				e.printStackTrace();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				socket.close();
			}	
		}
	}
}

  

Receive接收端代码:

package cn.csuft.poorguy.chat;

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

public class Receive {
	public static void main(String[] args) {
		System.out.println("服务器启动");
		DatagramSocket socket = null;
		try {
			//创建UDP套接字
			socket = new DatagramSocket(7000);
		} catch (SocketException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		while(true) {
			try {
				
				//数据包
				byte []buf = new byte[256];
				DatagramPacket packet = new DatagramPacket(buf, buf.length);
				
				//接收
				socket.receive(packet);
				
				//打印内容
				byte []data = packet.getData();
				String msg = new String(data,0,packet.getLength());
				if(msg.equals("byby")) {
					socket.close();
					System.out.println("服务器已经关闭");
					break;
				}
				System.out.println(msg);

			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

  

测试结果:

发送方发送结果:

接收方接受结果:

 

使用两个线程,实现实时通信,一个接收一个发送

发送端程序:

package cn.csuft.poorguy.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SendTask implements Runnable {

	DatagramSocket socket = null;
	public SendTask() {
		// TODO Auto-generated constructor stub
	}
	
	public SendTask(DatagramSocket socket) {
		this.socket = socket;
	}
	
	@Override
	public void run() {
		String msg;
		Scanner cin = new Scanner(System.in);
		DatagramPacket packet = null;
		do {
			System.out.println("目标端口:");
			int port = Integer.valueOf(new Scanner(System.in).nextInt());
			
			System.out.print("发送:");
			msg = cin.nextLine();
			byte []data = msg.getBytes();
			try {
				
				//创建数据包
				 packet = new DatagramPacket(
						data,
						data.length,
						InetAddress.getByName("127.0.0.1"),
						9000
						);
				 
				 //发送
				 socket.send(packet);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} while (!msg.equalsIgnoreCase("bye"));
		System.out.println("发送结束");
	}

}

  

接收端程序:

package cn.csuft.poorguy.chat;

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

public class ReceiverTask implements Runnable {

	DatagramSocket socket = null;
	public ReceiverTask() {
		// TODO Auto-generated constructor stub
	}
	
	public ReceiverTask(DatagramSocket socket) {
		this.socket = socket;
	}
	@Override
	public void run() {
		String msg = null;
		byte []buf = new byte[1024*8];
		do {
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			try {
				socket.receive(packet);
				
				byte []data = packet.getData();
				msg = new String(data,0,packet.getLength());
				System.out.println("收到: "+msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while (!msg.equalsIgnoreCase("bye"));
		System.out.println("接收端关闭");
	}

}

  

 

posted @ 2019-02-18 10:38  Bug研发工程师  阅读(312)  评论(0编辑  收藏  举报