1. 程式人生 > >Java——UDP的簡單傳輸

Java——UDP的簡單傳輸

網路程式設計:用來實現網路互連的不同計算機上執行的程式可以進行資料交換
     1.UDP傳輸,執行時,先啟動接收方(即先執行接收方),後執行傳送方
         (1)傳送Send
             1)建立DatagramSocket,隨機埠號
             2)建立DatagramPacket,指定資料,長度,地址,埠
             3)使用DatagramSocket傳送DatagramPacket
             4)關閉DatagramSocket
         (2)接受Receive
             1)建立DatagramSocket,指定埠號
             2)建立DatagramPacket,指定陣列,長度
             3)使用DatagramSocket接收DatagramPacket
             4)關閉DatagramSocket
             5)從DatagramPacket中獲取資料
         (3)接收方獲取ip和埠號
             String ip=packet,getAddress().getHostAddress();
             int port=packet.getPort();
     2.UDP傳輸的優化

     3.UDP傳輸多執行緒:傳送和接收在一個視窗完成

傳送端程式碼

package pra_23;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class J_42 {

	/**
	 * @param args
	 * @throws SocketException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws Exception {
		//傳送端
		String str="qqqqqq";
		DatagramSocket socket=new DatagramSocket();
		DatagramPacket dp=
				new DatagramPacket(str.getBytes(),str.getBytes().length, InetAddress.getByName("127.0.0.1"),6666);
		socket.send(dp);			//把資料發出去
		socket.close();				//關閉
		//傳送端的優化
		Scanner sc=new Scanner(System.in);
		DatagramSocket socket2=new DatagramSocket();
		while(true){
			String line=sc.nextLine();
			if("quit".equals(line)){
				break;
			}
			DatagramPacket packet2=
					new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("127.0.0.1"), 6666);
			socket2.send(packet2);
			}
		socket2.close();
	}

}

接收端程式碼

package pra_23;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class J_43 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		//接收端
		DatagramSocket ds=new DatagramSocket(6666);
		DatagramPacket dp=new DatagramPacket(new byte[1024], 1024);
		ds.receive(dp);
		byte[] arr=dp.getData();
		int len=dp.getLength();
		System.out.println(new String(arr,0,len));
		ds.close();
		
		//接收端優化
		DatagramSocket ds2=new DatagramSocket(6666);
		DatagramPacket dp2=new DatagramPacket(new byte[1024], 1024);
		while(true){
			ds2.receive(dp2);
			byte[] arr2=dp2.getData();
			int len2=dp2.getLength();
			String ip=dp2.getAddress().getHostAddress();
			int port=dp2.getPort();
			System.out.println(ip+":"+port+":"+new String(arr2,0,len2));
		}
	}

}

 

UDP傳輸多執行緒

package pra_23;

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

public class J_44 {
	public static void main(String[] args) throws Exception{
		new Receive().start();
		
		new Send().start();
	}
}
class Receive extends Thread{
	public void run(){
		try {
			DatagramSocket ds2=new DatagramSocket(6666);
			DatagramPacket dp2=new DatagramPacket(new byte[1024], 1024);
			while(true){
				ds2.receive(dp2);
				byte[] arr2=dp2.getData();
				int len2=dp2.getLength();
				String ip=dp2.getAddress().getHostAddress();
				int port=dp2.getPort();
				System.out.println(ip+":"+port+":"+new String(arr2,0,len2));
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}
class Send extends Thread{
	public void run(){
		try {
			Scanner sc=new Scanner(System.in);
			DatagramSocket socket2=new DatagramSocket();
			while(true){
				String line=sc.nextLine();
				if("quit".equals(line)){
					break;
				}
				DatagramPacket packet2=
						new DatagramPacket(line.getBytes(),line.getBytes().length,InetAddress.getByName("127.0.0.1"), 6666);
				socket2.send(packet2);
				}
			socket2.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
}