It is very necessary to know what is server and client before moving to the program.I just explain in short about server and client...
* server: As the name implies, it provides service of some kind. The service is provided to the client that connect to the server's host machine may be for some specific purpose.
* Though a client and its corresponding server will normally run on different machines in a real world application, it is perfectly possible for such programs to run on the same machine.
* there are two ways of connection between Server and client
1.TCP connection and 2. UDP connection
Hence we can write program for TCP also or else UDP also....
* WHY DO WE HAVE 2 WAYS???? WHICH ONE IS GOOD??
TCP- Transmission Control Protocol
UDP- User Datagram Protocol
DIFFERENCE 1: UDP doesn’t re-send a packet if it fails to arrive. TCP overhead of providing facilities such as confirmation of receipt and re-transmission of lost or corrupted packets.
DIFFERENCE 2: UDP doesn’t re-assemble packets into the correct sequence.TCP does re-assemble packets into the correct sequence.
DIFFERENCE 3: UDP is significantly faster than TCP.
For some applications, however, the relatively slow throughput speed offered by TCP was simply not feasible. Such applications included the streaming of audio and video files (i.e., the playing of those files while they were being downloaded). Such applications didn’t use TCP, because of its large overhead. Instead, they used UDP, since their major objective was to keep playing the sound/video without interruption and losing a few bytes of data was much better than waiting for re-transmission of the missing data.
* PORT
A port is a logical connection to a computer (as opposed to a physical connection) and is identified by a number in the range 1–65535.
-------------------------------------------------------------------------------------------------------------
SERVER PROGRAM IN UDP:
import java.io.*;
import java.net.*;
public class UDPServer
{
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
System.out.println("opening port \n");
try
{
datagramSocket=new DatagramSocket(PORT);
}
catch(SocketException sockEx)
{
System.out.println("unable to open ");
System.exit(1);
}
handleClient();
}
private static void handleClient()
{
try
{
String messageIn,messageOut;
int numMessages=0;
InetAddress clientAddress=null;
int clientPort;
do
{
buffer= new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
clientAddress=inPacket.getAddress();
clientPort=inPacket.getPort();
messageIn=new String(inPacket.getData(),0,inPacket.getLength());
System.out.print(clientAddress);
System.out.print(" : ");
System.out.println(messageIn);
numMessages++;
messageOut="message" + numMessages + ": " + messageIn;
outPacket=new DatagramPacket(messageOut.getBytes(),messageOut.length(),clientAddress,clientPort);
datagramSocket.send(outPacket);
}while(true);
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
System.out.println("\n Closing connection.. ");
datagramSocket.close();
}
}
}
save as: UDPServer.java
compile as: javac UDPServer.java
run as : java UDPServer
--------------------------------------------------------------------------------------------------------------
step1:
*private static final int PORT=1234;
this instruction reserves a port so that server will wait in this port to connect to the client.
*private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
teo packets will be created inPacket and outPacket one packet for sending the data and another one for receiving the data respectively. and transfer of these packets are done using a data holding socket called datagramSocket. packet holds some kind of information and socket holds the those packets.
* private static byte[] buffer;
we reserve some buffer for incoming and outgoing message.
step2:
*datagramSocket=new DatagramSocket(PORT);
this instruction will open the Socket at PORT=1234. and waits for incoming packet. if it fails to open the Socket and waits for any incoming packet then error(catch) message will be printed.
*String messageIn,messageOut;
two strings are used for incoming and outgoing message respectively( this is a two way communication).
*inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
the incoming packet will be captured in inPacket corresponding socket is declared from which data packet need to be received.
*clientAddress=inPacket.getAddress();
clientPort=inPacket.getPort();
these instructions will catch the client Address from which packet is received. we can display the client address in case of more than one clients are connected to a single server so that we can easily distinguish. Client port refers to the port no in which server is waiting(1234 in this case).
step3:
*messageIn=new String(inPacket.getData(),0,inPacket.getLength());
this will separate the data from packet and stores it in messageIn string which we already defined.
*System.out.print(clientAddress);
System.out.print(" : ");
System.out.println(messageIn);
these three instructions will print the received message along with the clientAddress from which it is received.
*outPacket=new DatagramPacket(messageOut.getBytes(),messageOut.length(),clientAddress,clientPort);
A response is created at the server side and it is sent to client using following instruction.
*datagramSocket.send(outPacket);
-----------------------------------------------------------------------------------------------------
CLIENT PROGRAM IN UDP
import java.io.*;
import java.net.*;
import java.util.*;
public class UDPClient
{
private static InetAddress host;
private static final int PORT=1234;
private static DatagramSocket datagramSocket;
private static DatagramPacket inPacket,outPacket;
private static byte[] buffer;
public static void main(String[] args)
{
try
{
host=InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("HOST ID not found.. ");
System.exit(1);
}
accessServer();
}
private static void accessServer()
{
try
{
datagramSocket=new DatagramSocket();
Scanner userEntry=new Scanner(System.in);
String message="",response="";
do
{
System.out.println("enter message :");
message=userEntry.nextLine();
if(!message.equals("***CLOSE***"))
{
outPacket=new DatagramPacket(message.getBytes(),message.length(),host,PORT);
datagramSocket.send(outPacket);
buffer=new byte[256];
inPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(inPacket);
response=new String(inPacket.getData(),0,inPacket.getLength());
System.out.println(" \n SERVER-->>" +response);
}
}while(!message.equals("***CLOSE***"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
System.out.println("\n closing connection.... ");
datagramSocket.close();
}
}
}
save as: UDPClient.java
compile as: javac UDPClient.java
run as : java UDPClient ---------------------------------------------------------------------------------------------------
InetAddress: This is used to encapsulate the IP address and domain name for that address.
No comments:
Post a Comment