欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UDP Socket编程--客户端和服务端双向通信

程序员文章站 2024-02-01 19:53:10
...

最近写了个UDP通信,能够实现客户端和服务端的双向通信,和大家分享!
因为UDP是无连接通信,叫客户端和服务端是不准确的,应该叫发送端和接收端,因为我的代码能实现双向的通信,在接收信息的同时还能够发送信息,所以我叫做一端和另一端!

一端:

package test.udp;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * @author 
 * @version guchao
 * CreateTime:
 * Description:真正意义的UDP聊天,每端都可以不停地向另一端发送消息,也在不停地接收消息
 */
public class TestUDPServer2 {

	public static void main(String[] args) throws IOException {
		new TestUDPServer2().go();
	}
	
	private void go() {
		//服务端启动2个线程,1个发送,1个接收
		ServerSendThread send = new ServerSendThread();
		new Thread(send).start();
		
		ServerRecvThread recv = new ServerRecvThread();
		new Thread(recv).start();
	}

//	服务端发送线程
	class ServerSendThread implements Runnable{

		@Override
		public void run() {
			try {
				DatagramSocket ds = new DatagramSocket();
				String str = "";
				byte[] buf = null;
				while (true) {
					System.out.println("请输入>");
					BufferedReader br = new BufferedReader(
							new InputStreamReader(System.in));
					str = br.readLine();
					if ("bye".equals(str))
						break;
					buf = str.getBytes();

					DatagramPacket dp = new DatagramPacket(buf, buf.length,
							new InetSocketAddress("127.0.0.1", 5679));
					ds.send(dp);
					buf = null;
				}
				ds.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
//	服务端接收线程
	class ServerRecvThread implements Runnable{

		@Override
		public void run() {
			byte[] buf = new byte[1024];
			
			//接收端的端口需要指定,不然发送端不知道向哪个端口发送数据包
			DatagramSocket ds;
			try {
				ds = new DatagramSocket(5678);
			
				String message = "";
				while(true){
					DatagramPacket dp = new DatagramPacket(buf, buf.length);
					ds.receive(dp);
					
					message = new String(buf,0,dp.getLength());
					System.out.println("接收到Client端的信息为:"+message);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
}

 

另一端:

package test.udp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * @author
 * @version guchao CreateTime:2010-11-29 上午08:57:52 Description:发送端
 */
public class TestUDPClient2 {
	public static void main(String[] args) throws IOException {
		new TestUDPClient2().go();
	}

	private void go() {
		ClientSendThread send = new ClientSendThread();
		new Thread(send).start();

		ClientRecvThread recv = new ClientRecvThread();
		new Thread(recv).start();
	}

	class ClientSendThread implements Runnable {

		@Override
		public void run() {
			try {
				DatagramSocket ds = new DatagramSocket();
				String str = "";
				byte[] buf = null;
				while (true) {
					System.out.println("请输入>");
					BufferedReader br = new BufferedReader(
							new InputStreamReader(System.in));
					str = br.readLine();
					if ("bye".equals(str))
						break;
					buf = str.getBytes();

//					System.out.println("-----buf.length-------" + buf.length);
					DatagramPacket dp = new DatagramPacket(buf, buf.length,
							new InetSocketAddress("127.0.0.1", 5678));
					ds.send(dp);
					buf = null;
				}
				ds.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	class ClientRecvThread implements Runnable {

		@Override
		public void run() {
			byte[] buf = new byte[1024];
			
			//接收端的端口需要指定,不然发送端不知道向哪个端口发送数据包
			DatagramSocket ds;
			try {
				ds = new DatagramSocket(5679);
			
				String message = "";
				DatagramPacket dp = null;
				while(true){
					dp = new DatagramPacket(buf, buf.length);
					ds.receive(dp);
					
					message = new String(buf,0,dp.getLength());
					System.out.println("接收到Server端信息为:"+message);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

 

 

相关标签: 网络编程 UDP