Java套接字編程(下)(2)
發(fā)表時(shí)間:2023-08-17 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]DatagramSocket類 DatagramSocket類在客戶端創(chuàng)建自尋址套接字與服務(wù)器端進(jìn)行通信連接,并發(fā)送和接受自尋址套接字。雖然有多個(gè)構(gòu)造函數(shù)可供選擇,但我發(fā)現(xiàn)創(chuàng)建客戶端自尋址套接字最...
DatagramSocket類
DatagramSocket類在客戶端創(chuàng)建自尋址套接字與服務(wù)器端進(jìn)行通信連接,并發(fā)送和接受自尋址套接字。雖然有多個(gè)構(gòu)造函數(shù)可供選擇,但我發(fā)現(xiàn)創(chuàng)建客戶端自尋址套接字最便利的選擇是DatagramSocket()函數(shù),而服務(wù)器端則是DatagramSocket(int port)函數(shù),如果未能創(chuàng)建自尋址套接字或綁定自尋址套接字到本地端口,那么這兩個(gè)函數(shù)都將拋出一個(gè)SocketException對象,一旦程序創(chuàng)建了DatagramSocket對象,那么程序分別調(diào)用send(DatagramPacket dgp)和 receive(DatagramPacket dgp)來發(fā)送和接收自尋址數(shù)據(jù)包,
List4顯示的DGSClient源代碼示范了如何創(chuàng)建自尋址套接字以及如何通過套接字處理發(fā)送和接收信息
Listing 4: DGSClient.java
// DGSClient.java
import java.io.*;
import java.net.*;
class DGSClient
{
public static void main (String [] args)
{
String host = "localhost";
// If user specifies a command-line argument, that argument
// represents the host name.
if (args.length == 1)
host = args [0];
DatagramSocket s = null;
try
{
// Create a datagram socket bound to an arbitrary port.
s = new DatagramSocket ();
// Create a byte array that will hold the data portion of a
// datagram packet's message. That message originates as a
// String object, which gets converted to a sequence of
// bytes when String's getBytes() method is called. The
// conversion uses the platform's default character set.
byte [] buffer;
buffer = new String ("Send me a datagram").getBytes ();
// Convert the name of the host to an InetAddress object.
// That object contains the IP address of the host and is
// used by DatagramPacket.
InetAddress ia = InetAddress.getByName (host);
// Create a DatagramPacket object that encapsulates a
// reference to the byte array and destination address
// information. The destination address consists of the
// host's IP address (as stored in the InetAddress object)
// and port number 10000 -- the port on which the server
// program listens.
DatagramPacket dgp = new DatagramPacket (buffer,
buffer.length,
ia,
10000);
// Send the datagram packet over the socket.
s.send (dgp);
// Create a byte array to hold the response from the server.
// program.
byte [] buffer2 = new byte [100];
// Create a DatagramPacket object that specifies a buffer
// to hold the server program's response, the IP address of
// the server program's computer, and port number 10000.
dgp = new DatagramPacket (buffer2,
buffer.length,
ia,
10000);
// Receive a datagram packet over the socket.
s.receive (dgp);
// Print the data returned from the server program and stored
// in the datagram packet.
System.out.println (new String (dgp.getData ()));
}
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
if (s != null)
s.close ();
}
}
}
DGSClient由創(chuàng)建一個(gè)綁定任意本地(客戶端)端口好的DatagramSocket對象開始,然后裝入帶有文本信息的數(shù)組buffer和描述服務(wù)器主機(jī)IP地址的InetAddress子類對象的引用,接下來,DGSClient創(chuàng)建了一個(gè)DatagramPacket對象,該對象加入了帶文本信息的緩沖器的引用,InetAddress子類對象的引用,以及服務(wù)端口號10000, DatagramPacket的自尋址數(shù)據(jù)包通過方法sent()發(fā)送給服務(wù)器程序,于是一個(gè)包含服務(wù)程序響應(yīng)的新的DatagramPacket對象被創(chuàng)建,receive()得到響應(yīng)的自尋址數(shù)據(jù)包,然后自尋址數(shù)據(jù)包的getData()方法返回該自尋址數(shù)據(jù)包的一個(gè)引用,最后關(guān)閉DatagramSocket。
DGSServer服務(wù)程序補(bǔ)充了DGSClient的不足,List5是DGSServer的源代碼:
Listing 5: DGSServer.java
// DGSServer.java
import java.io.*;
import java.net.*;
class DGSServer
{
public static void main (String [] args) throws IOException
{
System.out.println ("Server starting ...\n");
// Create a datagram socket bound to port 10000. Datagram
// packets sent from client programs arrive at this port.
DatagramSocket s = new DatagramSocket (10000);
// Create a byte array to hold data contents of datagram
// packet.
byte [] data = new byte [100];
// Create a DatagramPacket object that encapsulates a reference
// to the byte array and destination address information. The
// DatagramPacket object is not initialized to an address
// because it obtains that address from the client program.
DatagramPacket dgp = new DatagramPacket (data, data.length);
// Enter an infinite loop. Press Ctrl+C to terminate program.
while (true)
{
// Receive a datagram packet from the client program.
s.receive (dgp);
// Display contents of datagram packet.
System.out.println (new String (data));
// Echo datagram packet back to client program.
s.send (dgp);
}
}
}
DGSServer創(chuàng)建了一個(gè)綁定端口10000的自尋址套接字,然后創(chuàng)建一個(gè)字節(jié)數(shù)組容納自尋址信息,并創(chuàng)建自尋址包,下一步,DGSServer進(jìn)入一個(gè)無限循環(huán)中以接收自尋址數(shù)據(jù)包、顯示內(nèi)容并將響應(yīng)返回客戶端,自尋址套接沒有關(guān)閉,因?yàn)檠h(huán)是無限的。
在編譯DGSServer 和DGSClient的源代碼后,由輸入java DGSServer開始運(yùn)行DGSServer,然后在同一主機(jī)上輸入Java DGSClient開始運(yùn)行DGSClient,如果DGSServer與DGSClient運(yùn)行于不同主機(jī),在輸入時(shí)注意要在命令行加上服務(wù)程序的主機(jī)名或IP地址,如:java DGSClient www.yesky.com