Socket is a logical entity in a computer
system which provides communication mechanism between two computers using TCP (transmission
control protocol) or UDP (user data-gram protocol), but in this blog we will be
studying communication over TCP. Socket is combination of IP address and port.
The java.net package contains classes that provide all of low level
communication for you. To create socket there is Socket class available in
java.net package and the java.net.ServerSocket class provides a mechanism for
server program to listen for client and establish connection between them.
Steps
to establish communication using socket over TCP:
1.Server initiate
Server socket object indicating on which port communication will take place i.e.
server decides port on which communication takes place.
2.The server invokes
the accept() method of the ServerSocket class. This method waits until client
connects to server on the given port.
3. Then client
initiate socket object specifying IP address or name of server and port number
on which communication will takes place. Communication will takes place only
when server is listening to same port.
4.After this it can
be possible to communicate on a socket.
After following above steps communication
can be done using I/O streams. TCP is a two way communication protocol. Both
client and server has input and output stream. Client output stream is
connected to input stream and Client output stream is connected to server
output stream. There are various socket primitives available in ServerSocket
class.
Socket
primitives:-
There are a lot of primitives available but
we will be discussing only two which is required to establish simple
communication over socket.
public ServerSocket(int port):
This primitive in server decides on which
port communication will take place. We can say that it binds the port address
by creating socket. Sometime this primitives throws I/O exceptions.
public
Socket accept():
This primitive in server continuously
waiting for client to get connect on the specified port in above primitives.
This method also throws I/O exceptions.
Example
:-
For client:
import java.net.*;
import java.io.*;
public class socketclient {
public static void main(String[] args) {
Socket
s=null;
try
{
s
= new Socket("localhost",30000);
}
catch(UnknownHostException
uhe)
{
uhe.printStackTrace();
}
catch(IOException
ioe)
{
ioe.printStackTrace();
}
System.out.println("Connected
Successfully");
BufferedReader
in = null;
PrintWriter
out = null;
try
{
in=new BufferedReader(new
InputStreamReader(s.getInputStream()));
out=new PrintWriter(new
OutputStreamWriter(s.getOutputStream()));
System.out.println("Start Communication
now -->>");
while(true)
{
BufferedReaderreader=newBufferedReader(newinputStreamReader(System.in));
String name=reader.readLine();
out.println(name);
out.flush();
System.out.println("Server
says:->>"+in.readLine());
if(in.readLine().equals("exited") | in.readLine().equals(null))
{
s.close();
break;
}
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
For server:
import java.net.*;
import java.io.*;
public class socketserver {
public
static void main(String[] args) {
ServerSocket
ss=null;
Socket
server = null;
int
port=30000;
try
{
ss=new
ServerSocket(port);
}
catch(IOException
ioe)
{
ioe.printStackTrace();
}
System.out.println("listening
0n "+ port);
try
{
server=ss.accept();
}
catch(IOException
ioe)
{
ioe.printStackTrace();
}
BufferedReader
in = null;
PrintWriter out = null;
try
{
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
while(true)
{
String clientCommand = in.readLine();
if(clientCommand.equals("exit"))
{
System.out.println("Connection
is disconnected from the user Messege :EXIT");
out.println("exited");out.flush();
server.close();
break;
}
System.out.println("Client Says :-->>" + clientCommand);
System.out.println("Server Says:-->>");
BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in));
String name=reader.readLine();
out.println(name);
out.flush();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
No comments:
Post a Comment