Java Socket programming-Transferring Java object through socket using UDP

In the previous chapters we were discussing the fundamentals of networking in Java , with TCP as well as UDP . We discussed the example of transferring object through TCP sockets already.In this chapter we are discussing the  Transferring Java object through socket using UDP.

Transferring Java object through socket using UDP

We are discussing the concept with an example.We have a server application as well as client application.The server is a DatagramSocket object and is waiting to get a DatagramPacket.The client creates a Student object and is  sending to network as DatagramPacket. The packet should contain the ip address of the machine where our server is running.The server receives the DatagramPacket and  converting it back to the Student object. The Student class should implement the Serializable interface as in the previous case.Because here also byte stream to object conversion and object to byte stream conversion is needed.Also the serialVersionUID  of  Student.java at both sides(server and client) should be same.

Student.java

import java.io.Serializable;

public class Student implements Serializable {

public Student(int id, String name, String address1) {
this.id = id;
this.name = name;
this.addressLine = address1;
}

private static final long serialVersionUID = 1L;
private int id;
private String name;
private String addressLine;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddressLine() {
return addressLine;
}

public void setAddressLine(String addressLine) {
this.addressLine = addressLine;
}

public String toString() {
return "Id = " + getId() + " Name = " + getName() + " Address = " + getAddressLine();
}
}

Now lets see the server side code.

UDPSocketServer.java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPSocketServer {
DatagramSocket socket = null;

public UDPSocketServer() {

}

public void createAndListenSocket() {
try {
socket = new DatagramSocket(9876);
byte[] incomingData = new byte[1024];

while (true) {
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
socket.receive(incomingPacket);
byte[] data = incomingPacket.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
try {
Student student = (Student) is.readObject();
System.out.println("Student object received = "+student);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
InetAddress IPAddress = incomingPacket.getAddress();
int port = incomingPacket.getPort();
String reply = "Thank you for the message";
byte[] replyBytea = reply.getBytes();
DatagramPacket replyPacket =
new DatagramPacket(replyBytea, replyBytea.length, IPAddress, port);
socket.send(replyPacket);
Thread.sleep(2000);
System.exit(0);
}

} catch (SocketException e) {
e.printStackTrace();
} catch (IOException i) {
i.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
UDPSocketServer server = new UDPSocketServer();
server.createAndListenSocket();
}
}

Now see the client code.

UDPSocketClient.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.*;

public class UDPSocketClient {
DatagramSocket Socket;

public UDPSocketClient() {

}

public void createAndListenSocket() {
try {

Socket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] incomingData = new byte[1024];
Student student = new Student(1, "Bijoy", "Kerala");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(student);
byte[] data = outputStream.toByteArray();
DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, 9876);
Socket.send(sendPacket);
System.out.println("Message sent from client");
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
Socket.receive(incomingPacket);
String response = new String(incomingPacket.getData());
System.out.println("Response from server:" + response);
Thread.sleep(2000);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
UDPSocketClient client = new UDPSocketClient();
client.createAndListenSocket();
}
}

Output

 Use appropriate ip address while constructing datagrampacket object. Start server  first and then client.

Output of UDPSocketClient.java

Message sent from client

Response from server:Thank you for the message

Output of UDPSocketServer.java

Student object received = Id = 1 Name = Bijoy Address = Kerala