Java實現區域網內單播

穆穆裡發表於2014-03-19

前面我寫過一篇文章講java實現組播,今天我們就來看看java怎麼實現區域網內單播。

我們先來補充一下網路程式設計的兩個要點:

1. 以程式為物件來看待問題會比較簡單一點。程式是最小的程式單元,而網路程式在建立網路連線的時候會佔用一個埠。

2. 不管是單播還是組播,其實都是程式間通訊。

所以實現單播最重要的是指定IP地址和程式埠。

如果是本機測試,IP地址相同是必然的,只要傳送程式和接收程式使用不同的埠(注意在傳送時保持目的埠與接收程式的埠一致)就可以互相通訊,否則會出現IP地址被佔用的異常。

區域網內單播有兩種實現方式:一種是無連線方式,另一種是面向連線方式。

我們先來看無連線的方式:

首先我們繼承DatagramSocket類,並封裝了傳送和接收訊息的方法。

import java.net.*;
import java.io.*;

/**
 * 繼承資料包套接字類
 * 實現傳送訊息和接收訊息的方法
 * @author michael
 *
 */
public class MyDatagramSocket extends DatagramSocket {
	static final int MAX_LEN = 100;

	MyDatagramSocket() throws SocketException {
		super();
	}

	MyDatagramSocket(int port) throws SocketException {
		super(port);
	}

	public void sendMessage(InetAddress receiverHost, int receiverPort,
			String message) throws IOException {
		byte[] sendBuffer = message.getBytes();
		DatagramPacket datagram = new DatagramPacket(sendBuffer,
				sendBuffer.length, receiverHost, receiverPort);
		this.send(datagram);
	}

	public String receiveMessage() throws IOException {
		byte[] receiveBuffer = new byte[MAX_LEN];
		DatagramPacket datagram = new DatagramPacket(receiveBuffer, MAX_LEN);
		this.receive(datagram);
		String message = new String(receiveBuffer);
		return message;
	}
}

-------------------------------------------------Receiver.java---------------------------------------------------

import java.net.*;

/**
 * 採用無連線的方式實現程式間通訊
 * @author michael
 *
 */
public class Receiver {

	public static void main(String[] args) {
		try {
			InetAddress receiverHost = InetAddress.getByName("127.0.0.1");
			int myPort = 1234;// 本程式埠
			int receiverPort = 5689;// 接收程式的埠
			String message = "Hi Sender";
			MyDatagramSocket mySocket = new MyDatagramSocket(myPort);
			System.out.println(mySocket.receiveMessage());
			mySocket.sendMessage(receiverHost, receiverPort, message);
			mySocket.close();
		} catch (Exception ex) {
			System.out.println(ex);
		}
	}
}

-------------------------------------------------Sender.java---------------------------------------------------

import java.net.*;

/**
 * 採用無連線的方式實現程式間通訊
 * @author michael
 *
 */
public class Sender {

	public static void main(String[] args) {
		try {
			InetAddress receiverHost = InetAddress.getByName("127.0.0.1");
			int myPort = 5689;
			int receiverPort = 1234;
			String message = "Hello Receiver";
			MyDatagramSocket mySocket = new MyDatagramSocket(myPort);
			mySocket.sendMessage(receiverHost, receiverPort, message);
			System.out.println(mySocket.receiveMessage());
			mySocket.close();
		} catch (Exception ex) {
			
		}
	}
}

程式碼裡面我採用的是本機測試,不過我試過了聯機測試,是可以的。

下面再看面向連線的方式:

向連線的方式跟無連線的方式大體相同,只是多了一個建立連線和斷開連線的過程。

-------------------------------------------------Receiver.java---------------------------------------------------

import java.net.*;

/**
 * 採用面向連線的方式實現程式間通訊
 * @author michael
 *
 */
public class Receiver {

	public static void main(String[] args) {
		try {
			InetAddress senderHost = InetAddress.getByName("127.0.0.1");
			int senderPort = 1234;
			int myPort = 4568;
			String message = "Hi Sender";
			MyDatagramSocket mySocket = new MyDatagramSocket(myPort);
			//與對方建立連線
			mySocket.connect(senderHost, senderPort);
			System.out.println(mySocket.receiveMessage());
			mySocket.sendMessage(senderHost, senderPort, message);
                        //斷開連線
			mySocket.disconnect();
			mySocket.close();
		}
		catch (Exception ex) {
			System.out.println("An exception has occured: " + ex);
		}
	}
}
-------------------------------------------------Sender.java---------------------------------------------------
import java.net.*;

/**
 * 採用面向連線的方式實現程式間通訊
 * @author michael
 *
 */
public class Sender {

	public static void main(String[] args) {
		try {
			InetAddress receiverHost = InetAddress.getByName("127.0.0.1");
			int receiverPort = 4568;
			int myPort = 1234;
			String message = "Hello Receiver";
			MyDatagramSocket mySocket = new MyDatagramSocket(myPort);
			mySocket.connect(receiverHost, receiverPort);
			mySocket.sendMessage(receiverHost, receiverPort, message);
			System.out.println(mySocket.receiveMessage());
			mySocket.disconnect();
			mySocket.close();
		}
		catch (Exception ex) {
			System.out.println(ex);
		}
	}
}
測試跟前面一樣。

相關文章