In the following program, we will demonstrate, how data can be transferred between client and server using Transport Layer Protocol - UDP. This code is self-explanatory. Hence, first, you will learn to develop a server-side application. Then, you will learn to develop client-side applications.
We know that UDP is a connectionless protocol, hence at the time of testing you may not get a response from the server in many times.
EchoServer.java
import java.net.*;
import java.io.*;
public class EchoServer {
public static final int SERVICE_PORT = 7;
public static final int BUFSIZE = 4096;
private DatagramSocket socket;
public EchoServer() {
try {
socket = new DatagramSocket( SERVICE_PORT );
System.out.println ("Server active on port " +
socket.getLocalPort() );
} catch (Exception e) {
System.err.println ("Unable to bind port");
}
}
public void serviceClients() {
byte[] buffer = new byte[BUFSIZE];
for (;;) {
try {
DatagramPacket packet = new DatagramPacket( buffer, BUFSIZE );
socket.receive(packet);
System.out.println ("Packet received from " + packet.getAddress() + ":" + packet.getPort() +" of length " + packet.getLength() );
socket.send(packet);
} catch (IOException ioe) {
System.err.println ("Error : " + ioe);
}
}
}
public static void main(String args[]) {
EchoServer server = new EchoServer();
server.serviceClients();
}
}
EchoClient.java
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class EchoClient {
public static final int SERVICE_PORT = 7;
public static final int BUFSIZE = 256;
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("Syntax - java EchoClient hostname");
return;
}
String hostname = args[0];
InetAddress addr = null;
try {
addr = InetAddress.getByName(hostname);
} catch (UnknownHostException uhe) {
System.err.println("Unable to resolve host");
return;
}
try {
DatagramSocket socket = new DatagramSocket();
socket.setSoTimeout(2 * 1000);
for (int i = 1; i <= 10; i++) {
String message = "Packet number " + i;
char[] cArray = message.toCharArray();
byte[] sendbuf = new byte[cArray.length];
for (int offset = 0; offset < cArray.length; offset++) {
sendbuf[offset] = (byte) cArray[offset];
}
DatagramPacket sendPacket = new DatagramPacket(sendbuf, cArray.length, addr, SERVICE_PORT);
System.out.println("Sending packet to " + hostname);
socket.send(sendPacket);
System.out.print("Waiting for packet.... ");
byte[] recbuf = new byte[BUFSIZE];
DatagramPacket receivePacket = new DatagramPacket(recbuf, BUFSIZE);
boolean timeout = false;
try {
socket.receive(receivePacket);
} catch (InterruptedIOException ioe) {
timeout = true;
}
if (!timeout) {
System.out.println("packet received!");
System.out.println("Details : " + receivePacket.getAddress());
ByteArrayInputStream bin = new ByteArrayInputStream(receivePacket.getData(), 0,
receivePacket.getLength());
BufferedReader reader = new BufferedReader(new InputStreamReader(bin));
for (;;) {
String line = reader.readLine();
if (line == null)
break;
else
System.out.println(line);
}
} else {
System.out.println("packet lost!");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
}
}
} catch (IOException ioe) {
System.err.println("Socket error " + ioe);
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.