|
Datagram Communication
Part:
1
2
3
(Continued from previous part...)
The following diagram shows the steps involved in sending a datagram
from one program to another program using the methods provided by
DatagramSocket and DatagramPacket classes:
Sender Program Receiver Program
Internet Address #a Internet Address #b
Step Available Port #p Available Port #q
1 ds = new DatagramSocket(#b+#q)
2 dp = new DatagramPacket()
3 ds.receive(dp)
4 ds = new DatagramSocket() (waiting)
5 dp = new DataPacket() (waiting)
6 dp.setAddress() (waiting)
7 dp.setPort() (waiting)
8 dp.setData() (waiting)
9 ds.send(dp) (receiving datagram)
10 (dp is ready)
Datagram Socket CLient And Server Programs
The following program, called DatagramServer, is a simple datagram socket
application, which acts a server listening on given port number for datagram
packets to arrive:
/**
* DatagramServer.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.io.*;
import java.net.*;
public class DatagramServer {
public static void main(String[] args) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
try {
// binding to the default address and port 7777
DatagramSocket ds = new DatagramSocket(7777);
printDatagramSocketInfo(ds);
while (true) {
ds.receive(dp);
printDatagramSocketInfo(ds);
printDatagramPacketInfo(dp);
// no need to call buf = dp.getData();
// reversing the characters, assuming one-type characters
int n = dp.getLength();
for (int i=0; i<n/2; i++) {
byte t = buf[i];
buf[i] = buf[n-1-i];
buf[n-1-i] = t;
}
ds.send(dp);
}
} catch (IOException e) {
System.err.println(e.toString());
}
}
private static void printDatagramSocketInfo(DatagramSocket s) {
System.out.println("Datagram Socket Info:");
if (s.isConnected()) {
System.out.println(" Connected to a remote system.");
System.out.println(" Remote address = "
+s.getInetAddress().toString());
System.out.println(" Remote port = "
+s.getPort());
} else {
System.out.println(" Not connected to a remote system.");
}
System.out.println(" Local socket address = "
+s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+s.getLocalAddress().toString());
System.out.println(" Local port = "
+s.getLocalPort());
}
private static void printDatagramPacketInfo(DatagramPacket d) {
System.out.println("Datagram Packet Info:");
System.out.println(" Remote socket address = "
+d.getSocketAddress().toString());
System.out.println(" Remote address = "
+d.getAddress().toString());
System.out.println(" Remote port = "
+d.getPort());
System.out.println(" Data length = "
+d.getLength());
}
}
Whenever DatagramServer receives a datagram packet, it takes the data,
reverses the data, and sends the data back to where it comes from.
Run DatagramServer in a command window, you will see the following output:
Datagram Socket Info:
Not connected to a remote system.
Local socket address = 0.0.0.0/0.0.0.0:7777
Local address = 0.0.0.0/0.0.0.0
Local port = 7777
The server is ready to receive packets at port 7777.
The following program, called DatagraClient, acts as a client program to
DatagramServer. It repeatedly sends a datagram packet to DatagramServer,
and waits a return datagram packet:
(Continued on next part...)
Part:
1
2
3
|