Copyright © tutorialspoint.com
NOTE: If you are aware of Unix Sockets then you can leave introduction part What is a socket?Just another bit of computer jargon? Going back a little into networking history, it is a Berkeley UNIX mechanism of creating a virtual duplex connection between processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never ever have happened. Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it. This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications which listen on different ports. For example a net browser listens on port 80 for information. Also we can write applications which listen and send information on a specific port number. For now, let's sum up that a socket is an IP address and a port, enabling connection. To explain the socket we will take an example of Client - Server Programming. To complete a client server architecture we would have to go through the following steps Creating A Server
Creating A Client
Create a socketThe first step in establishing a network connection is creating a socket, with the sockt() function
socket creates a SOCKET. The other three arguments are integers which should have the following values for TCP/IP connections.
So socket function call will be something like this:
Bind to a socket addressThe sockets created by socket are innocent and they have not yet been polluted by a computer hostname or a port number. Next socket functionbind() fleshes out the socket with these values. Server uses bind() function to specify the port at which they will be accepting connections from the clients.
ADDRESS is a socket address which ( for TCP/IP ) is a packet string containing three elements
As the bind() is used by a server which does not need to know its own address so the argument list looks like this:
The or die clause is very important - if a server dies without outstanding connections the port won't be immediately reusable unless you use the option SO_REUSEADDR using Listening on the portIf this is a server program then it is required to issue a call to
The above call is mandatory for all the servers and here QUEUESIZE is the maximum number of outstanding connection request allowed. Generally, listen() is used in an infinite loop. As soon as one connection arrives the server deals with it and then goes back to listen for more connections. Accepting connectionsIf this is a server program then it is required to issue a call to
The accept call receive SOCKET descriptor returned by socket() function. Upon successful completion of this call, a new socket descriptor is returned. All future communication between client and server then takes place over NEW_SOCKET and SOCKET returns to what it does best : listen()ing for a new connection. If access() call fails then it returns FLASE which is defined in Socket module which we have used initially. You will often see accept() used inside a while loop as follows
Now all the calls related to server are over and let us see a call which will be required by the client Connection EstablishmentIf you are going to prepare client program then after using socket() call you would have to use another call connect() to connect to the server.
If you connect to the server successfully then you can start sending your commands to the server using SOCKET descriptor. So now lets put all the things together Script to Create a Server
Now starting above ServerTo run the server in background mode issue the following command on Unix prompt
Script to Create a Client
|
Copyright © tutorialspoint.com