Hello....
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.
First I will give you the program then i will explain: -----------------------------------------------------------------------------------------------------------------------------------------------------------------
ANYTHING YOU READ BELOW IS ABOUT SERVER MEAN WHILE FORGET ABOUT CLIENT
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
TCP Server program
import java.io.*;
import java.net.*;
public class myServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000); //instruction7 System.out.println("Server ready for chatting"); //instruction 8
Socket sock = sersock.accept( ); //instruction 9
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
sendMessage = keyRead.readLine();
pwrite.println(sendMessage); pwrite.flush();
}
}
}
-------------------------------------------------------------------------------------------
The program should be saved as filename.java(above case it should be myServer.java).
To compile- javac myServer.java
To run- java myServer
how it is done??
STEP1: importing all java packs import java.io.*;
import java.net.*;
STEP2: what id the meaning of "ServerSocket sersock = new ServerSocket(3000);" in the program??(7th instruction(or line) in above program)
It is creation of server socket Object. The ServerSocket constructor requires a port number (1024–65535, for non-reserved ones) as an argument. In this example, the server will await (‘listen for’) a connection from a client on port 3000.
STEP3: While server is waiting we know that server is ready to give service
hence we print "Server ready for chatting". which is next statement
System.out.println("Server ready for chatting");
(8th instruction or line in above program)
STEP4: what is the meaning of " Socket sock = sersock.accept( ); " in the above program?? (9th instruction or line in the above program)
It is putting the server into wait state.The server waits indefinitely (‘blocks’) for a client to connect. It does this by calling method accept of class ServerSocket, which returns a Socket object when a connection is made.
STEP5: what is the meaning of "BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in))" in the above program?? (10th instruction or line in the above program)
* In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
* It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.
* BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)) will buffer the input from the specified file(i.e System.in). Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
STEP6: what does it mean " OutputStream ostream = sock.getOutputStream() "??? (11th instruction in the above program)
This will create a socket for outgoing message which is not necessary in server side in one way communication since it is a two way communication it is required. here obviously you will get a doubt that why we need to send message from server, in order to get clear with this doubt run both the server and client programs and check. This is a two way communication(you can call it as chat). Obviously client should request service from server. This means communication always starts from client(first message must be sent from client to server) then server will reply.
STEP7: what does it mean " PrintWriter pwrite = new PrintWriter(ostream, true) "?? (12th instruction in the above program)
This is to print the message at server side if it is set to true then message will be visible at server side and if it is set to false then message will be received at server side but not be visible. This instruction is not necessary in server side in one way communication since it is a two way communication it is required.
STEP8: what does it mean " InputStream istream = sock.getInputStream(); "?? (12th instruction in the above program)
This instruction is required to accept the socket containing message which is sent from client.
STEP9: what is the meaning of " BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); "?? (13th instruction in the above program)
this is a buffer for the incoming message which holds the incoming message.
STEP10: in last step we declared two variables, one to send the message(i.e: sendMessage) and other to receive the message(i.e: receiveMessage)
readline() identifies the 'ENTER' key. if ENTER key is pressed message will be sent.
--------------------------------------------------------------------------------------------------------------------------------------------------------
END OF SERVER PART
--------------------------------------------------------------------------------------------------------------------------------------------------------
Now moving to client
TCP Client program:
import java.io.*;
import java.net.*;
public class myClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000);
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Start the chitchat, type and press Enter key");
String receiveMessage, sendMessage;
while(true) {
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage); }
}
}
}
-------------------------------------------------------------------------------
For explanation we can refer to server part. Since it is a two way communication most of the instructions will be same...
All the best...
IF U HAVE ANY DOUBTS LEAVE A COMMENT...
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.
First I will give you the program then i will explain: -----------------------------------------------------------------------------------------------------------------------------------------------------------------
ANYTHING YOU READ BELOW IS ABOUT SERVER MEAN WHILE FORGET ABOUT CLIENT
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
TCP Server program
import java.io.*;
import java.net.*;
public class myServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000); //instruction7 System.out.println("Server ready for chatting"); //instruction 8
Socket sock = sersock.accept( ); //instruction 9
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage);
sendMessage = keyRead.readLine();
pwrite.println(sendMessage); pwrite.flush();
}
}
}
-------------------------------------------------------------------------------------------
The program should be saved as filename.java(above case it should be myServer.java).
To compile- javac myServer.java
To run- java myServer
how it is done??
STEP1: importing all java packs import java.io.*;
import java.net.*;
STEP2: what id the meaning of "ServerSocket sersock = new ServerSocket(3000);" in the program??(7th instruction(or line) in above program)
It is creation of server socket Object. The ServerSocket constructor requires a port number (1024–65535, for non-reserved ones) as an argument. In this example, the server will await (‘listen for’) a connection from a client on port 3000.
STEP3: While server is waiting we know that server is ready to give service
hence we print "Server ready for chatting". which is next statement
System.out.println("Server ready for chatting");
(8th instruction or line in above program)
STEP4: what is the meaning of " Socket sock = sersock.accept( ); " in the above program?? (9th instruction or line in the above program)
It is putting the server into wait state.The server waits indefinitely (‘blocks’) for a client to connect. It does this by calling method accept of class ServerSocket, which returns a Socket object when a connection is made.
STEP5: what is the meaning of "BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in))" in the above program?? (10th instruction or line in the above program)
* In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.
* It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.
* BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in)) will buffer the input from the specified file(i.e System.in). Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.
STEP6: what does it mean " OutputStream ostream = sock.getOutputStream() "??? (11th instruction in the above program)
This will create a socket for outgoing message which is not necessary in server side in one way communication since it is a two way communication it is required. here obviously you will get a doubt that why we need to send message from server, in order to get clear with this doubt run both the server and client programs and check. This is a two way communication(you can call it as chat). Obviously client should request service from server. This means communication always starts from client(first message must be sent from client to server) then server will reply.
STEP7: what does it mean " PrintWriter pwrite = new PrintWriter(ostream, true) "?? (12th instruction in the above program)
This is to print the message at server side if it is set to true then message will be visible at server side and if it is set to false then message will be received at server side but not be visible. This instruction is not necessary in server side in one way communication since it is a two way communication it is required.
STEP8: what does it mean " InputStream istream = sock.getInputStream(); "?? (12th instruction in the above program)
This instruction is required to accept the socket containing message which is sent from client.
STEP9: what is the meaning of " BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream)); "?? (13th instruction in the above program)
this is a buffer for the incoming message which holds the incoming message.
STEP10: in last step we declared two variables, one to send the message(i.e: sendMessage) and other to receive the message(i.e: receiveMessage)
readline() identifies the 'ENTER' key. if ENTER key is pressed message will be sent.
--------------------------------------------------------------------------------------------------------------------------------------------------------
END OF SERVER PART
--------------------------------------------------------------------------------------------------------------------------------------------------------
Now moving to client
TCP Client program:
import java.io.*;
import java.net.*;
public class myClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000);
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Start the chitchat, type and press Enter key");
String receiveMessage, sendMessage;
while(true) {
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
if((receiveMessage = receiveRead.readLine()) != null)
{
System.out.println(receiveMessage); }
}
}
}
-------------------------------------------------------------------------------
For explanation we can refer to server part. Since it is a two way communication most of the instructions will be same...
All the best...
IF U HAVE ANY DOUBTS LEAVE A COMMENT...
No comments:
Post a Comment