Introduction To Socket Programming: Practical - 02
Introduction To Socket Programming: Practical - 02
Introduction To Socket Programming: Practical - 02
Practical – 02
INTRODUCTION TO SOCKET PROGRAMMING
What is Socket?
A socket is a software object that acts as an end point establishing a bidirectional network
communication link between a server-side and a client-side program.A socket is one endpoint of a
two-way communication link between two programs running on the network. A socket is bound to
a port number so that the TCP layer can identify the application that data is destined to be sent to.
What is Socket Programming?
Socket programming is a way of connecting two nodes on a network to communicate with each
other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the
other to form a connection. Server forms the listener socket while client reaches out to the server.
On the client-side: The client knows the hostname of the machine on which the server is running
and the port number on which the server is listening. To make a connection request, the client tries
to rendezvous with the server on the server's machine and port. The client also needs to identify
itself to the server so it binds to a local port number that it will use during this connection. This is
usually assigned by the system.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new
socket bound to the same local port and also has its remote endpoint set to the address and port of
the client. It needs a new socket so that it can continue to listen to the original socket for
connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can
use the socket to communicate with the server.
The client and server can now communicate by writing to or reading from their sockets
Why Socket Programming
Socket programs are used to communicate between various processes usually running on different
systems. It is mostly used to create a client-server environment. This post provides the various
functions used to create the server and client program and an example program. In the example,
the client program sends a file name to the server and the server sends the contents of the file back
to the client.
The client needs to learn two basic pieces of information, which are:
1. Port number
2. IP address of server
Socket class
A socket is simply an endpoint for communications between the machines. The Socket class can
be used to create a socket.
The following figure shows a communication structure between two machines, in this figure, the
communication is done using a socket over the internet.
IT,SVIIT,Indore Page 1
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
IT,SVIIT,Indore Page 2
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
IT,SVIIT,Indore Page 3
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
Code:
IT,SVIIT,Indore Page 4
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
Client code
try
{
Socket ss = new Socket("127.0.0.1",5502);
ss.close();
}
catch(Exception e)
{
System.out.print("error"+e);
}
}
}
Server code
IT,SVIIT,Indore Page 5
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
Socket s=ss.accept();
//establishes connection
System.out.println("Establishes Connection between a Client and Server");
String str=(String)dis.readUTF();
System.out.println("Entered sting is "+str);
char [] ch = str.toCharArray();
int count=0;
for (int i = 0; i < ch.length; i++)
{
if (ch[i]!=' ');
{
count++;
}
}
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}
Output:
IT,SVIIT,Indore Page 6
Component Technology(BTIT-505) Suyash Jain(1601DMBIT01011)
Client :
Server:
IT,SVIIT,Indore Page 7