Java Program To Illustrate Client Side
Java Program To Illustrate Client Side
DatagramSockets are Java’s mechanism for network communication via UDP instead of TCP. Java
provides DatagramSocket to communicate over UDP instead of TCP. It is also built on top of IP.
DatagramSockets can be used to both send and receive packets over the Internet.
One of the examples where UDP is preferred over TCP is the live coverage of TV channels. In this aspect,
we want to transmit as many frames to live audience as possible not worrying about the loss of one or
two frames. TCP being a reliable protocol add its own overhead while transmission.
Another example where UDP is preferred is online multiplayer gaming. In games like counter-strike or
call of duty, it is not necessary to relay all the information but the most important ones. It should also be
noted that most of the applications in real life uses careful blend of both UDP and TCP; transmitting the
critical data over TCP and rest of the data via UDP.
This article is a simple implementation of one-sided client-server program wherein the client sends
messages to server and server just prints it until the client sends “bye”.
Creation of DatagramSocket:- First, a datagramSocket object is created to carry the packet to the
destination and to receive it whenever the server sends any data. To create a datagramSocket following
constructors can be used:
throws SocketException
throws SocketException
Parameters:
Throws:
InetAddress inetaddress)
throws SocketException
Parameters:
Throws:
Creation of DatagramPacket: In this step, the packet for sending/receiving data via a datagramSocket is
created.
Constructor to send data: DatagramPacket(byte buf[], int length, InetAddress inetaddress, int port):-
int offset,
int length,
SocketAddress address)
Parameters:
int length)
Parameters:
throws SocketException
Parameters:
Throws:
throws SocketException
Parameters:
Throws:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
InetAddress ip = InetAddress.getLocalHost();
while (true)
buf = inp.getBytes();
// Step 2 : Create the datagramPacket for sending
// the data.
DatagramPacket DpSend =
// the data.
ds.send(DpSend);
if (inp.equals("bye"))
break;
TCP
Client-Side Programming
To connect to another machine we need a socket connection. A socket connection means the two
machines have information about each other’s network location (IP Address) and TCP port. The
java.net.Socket class represents a Socket. To open a socket:
The first argument – IP address of Server. ( 127.0.0.1 is the IP address of localhost, where code
will run on the single stand-alone machine).
The second argument – TCP Port. (Just a number representing which application to run on a
server. For example, HTTP runs on port 80. Port number can be from 0 to 65535)
Communication
To communicate over a socket connection, streams are used to both input and output the data.
In the program, the Client keeps reading input from a user and sends it to the server until “Over” is
typed.
Java Implementation
Java
import java.net.*;
import java.io.*;
// establish a connection
try
System.out.println("Connected");
catch(UnknownHostException u)
System.out.println(u);
catch(IOException i)
System.out.println(i);
while (!line.equals("Over"))
try
line = input.readLine();
out.writeUTF(line);
catch(IOException i)
System.out.println(i);
}
}
try
input.close();
out.close();
socket.close();
catch(IOException i)
System.out.println(i);
Server Programming
A ServerSocket which waits for the client requests (when a client makes a new Socket())
A plain old Socket socket to use for communication with the client.
Communication
getOutputStream() method is used to send the output through the socket.
Close the Connection
After finishing, it is important to close the connection by closing t
Java
import java.net.*;
import java.io.*;
try
System.out.println("Server started");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
while (!line.equals("Over"))
try
line = in.readUTF();
System.out.println(line);
catch(IOException i)
System.out.println(i);
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
System.out.println(i);
Important Points
Server application makes a ServerSocket on a specific port which is 5000. This starts our Server
listening for client requests coming in for port 5000.
socket = server.accept()
The accept() method blocks(just sits there) until a client connects to the server.
Then we take input from the socket using getInputStream() method. Our Server keeps receiving
messages until the Client sends “Over”.
After we’re done we close the connection by closing the socket and the input stream.
To run the Client and Server application on your machine, compile both of them. Then first run
the server application and then run the Client application.