1. 程式人生 > >Java實現UDP服務端與多客戶端連線

Java實現UDP服務端與多客戶端連線

學習的課程,程式比較簡單,直接上程式碼。

UDPServer

package com.imooc;

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

public class UDPServer {

	public static void main(String[] args) throws SocketException {
		// TODO Auto-generated method stub
		DatagramSocket socket= new DatagramSocket(8800);
		
		byte[] data = new byte[1024];
		DatagramPacket packet = new DatagramPacket(data, data.length);
		
		System.out.println(".....伺服器準備啟動......");
		int count = 0;
		
		while(true)
		{
			
			try {
				socket.receive(packet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			
			count++;
			
			UDPServerThread udpServerThread = new UDPServerThread(packet);
			udpServerThread.start();

			String info = new String(data, 0, packet.getLength());  
			System.out.println("我是伺服器,客戶端說:" + info);  
			System.out.println("該客戶端IP地址為:" + packet.getAddress());  
			System.out.println("客戶端連線次數:" + count);  
			
		}
			 
	}

}

UDPServerThread

package com.imooc;

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

public class UDPServerThread extends Thread {

	DatagramPacket datagramPacket = null;
	
	public UDPServerThread(DatagramPacket packet) {
		// TODO Auto-generated constructor stub
		this.datagramPacket = packet;
	}
	 @Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
			InetAddress inetAddress = datagramPacket.getAddress();
			int port2 = datagramPacket.getPort();
			byte[] data2 =	"歡迎你的到來".getBytes();
			DatagramPacket datagramPacket2 =  new DatagramPacket(data2, data2.length, inetAddress,port2);
			
			DatagramSocket socket = null;
			try {
				socket = new DatagramSocket();
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				socket.send(datagramPacket2);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			socket.close();
	}
}

UDPClient

package com.imooc;

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

public class UDPClient {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			InetAddress  inetAddress = InetAddress.getByName("localhost");
			int port = 8800;
			byte[] data = "使用者名稱: 000;密碼: 123".getBytes();
			DatagramPacket packet = new DatagramPacket(data, data.length,inetAddress,port);
			DatagramSocket socket = new DatagramSocket();
			socket.send(packet);
			
			/*
			 * 接收伺服器端的資料
			 */
			 byte[] data2 = new byte[1024];
			 DatagramPacket packet2 = new DatagramPacket(data2, data2.length);
			 socket.receive(packet2);
			 String reply = new String(data2, 0, packet2.getLength());
			 System.out.println("我是客戶端,伺服器說:"+reply);
			 socket.close();
					
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

輸出結果

.....伺服器準備啟動......
我是伺服器,客戶端說:使用者名稱: 555;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:1
我是伺服器,客戶端說:使用者名稱: 444;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:2
我是伺服器,客戶端說:使用者名稱: 333;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:3
我是伺服器,客戶端說:使用者名稱: 222;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:4
我是伺服器,客戶端說:使用者名稱: 111;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:5
我是伺服器,客戶端說:使用者名稱: 000;密碼: 123
該客戶端IP地址為:/127.0.0.1
客戶端連線次數:6