Socket Programming: Single server Single
Client
Socket programming is methodology
to connect two or more than two systems in the same network. To make this
happen at least two nodes are required “Server” and “Client”. It is very
important to understand the role of Server
and Client. Server is a node which accepts the request generated by client and
Client is a node which generates request.
As we have clearly stated that
Server accepts the request from Client, so it becomes automatically important
for Server to be always available to accept the request. Hence Server will
always be in
listening mode waiting for the request.
Before we start exploring the
different stages involved in the Socket programming lets understand the
terminologies used in socket programming. In this blog we are going to cover
the short description of header files used in socket programming. Later we will
cover the different stages necessary for the connection from the perspective of
Client as well as Server.
So lets begin with the description
of Header files used in Socket Programming:
1] sys/socket.h:
This particular header file is used
to define various function which helps programmer to establish communication
between two nodes in the network. Some of the functions are mentioned below.
A socket()
needs three parameters, and they are passed as integer values. Return
type of socket function is integer. Format of socket function is mentioned
below:
int socket(int namespace, int style, int protocol);
Apart from that another important
function is defined in the above mentioned header file. Next function which we
are going to describe is connect(). The connect function initiates a connection from
the socket with file descriptor socket to the socket whose address is
specified by the addr and length arguments. (This socket is
typically on another machine, and it must be already set up as a server).
Format of connect()
is mentioned below:
int connect
(int socket, struct sockaddr *addr, size_t length);
Input/Output
functions are also declared in this header
file. Most frequently used function to send message is send() and write(). For Output recv() is
used.
2] netinet/in.h:
This header file is
included to implement several functions necessary for the implementation of
socket programming. If we include netinet/in.h in our code than arpa/inet.h
will not be necessary and all the functions of arpa/inet.h will be included.
Some of the functions defined in netinet/in.h are mentioned below:
A structure with the name of sockaddr_in
is defined in the above mentioned header file. This structure helps programmer
to include several members such as IP address, AF_INET, Port Number, sin_addr,
sin_port etc.
Apart from the above structure several other functions whish are defined
are htonl(), htons(), ntohl(),
and ntohs(). Basic work of these
functions are to convert a 16 bit integer into 32 bit integer, how and where
they are implemented is mentioned below:
The htonl() and htons() functions shall return the argument value converted
from host to network byte order. The ntohl() and ntohs() functions shall return
the argument value converted from network to host byte order.
unistd.h is the C/C++ header file that is your code's entry point to
various constant, type and function declarations that comprise the POSIX
operating system API.
#include <unistd.h> gets
you, among other things: the infamous NULL pointer definition. symbolic
constants for I/O functions like SEEK_SET and R_OK.
How To Run Code In The Ubuntu?
Step:1 First you have to give your path of file.
Syntax: cd path_where file is stored
e.g. cd Downloads
If you don’t know the location of your file then check the file location
using browser download location and put it in place of “Downloads”.
Step:2 To compile the code,type following syntax,
Syntax: gcc file_name.c -o object_file
object_file is the name of your
object code for the executed file. If there will not be any error then you will
be able to see the object file in your directory.
Step:3 use ls to see if the object file is created
or not.
Syntax: ls
Step:4 To run the code, type the following syntax,
Syntax: ./object_file
How The Code Work?
Stage:1 First We have to execute the server side code, that’s why the server
becomes in “Listening Mode”.
Stage:2 Now give the server IP
address into the client code and execute it. So that is basically a request to
connect with server from client.
Stage:3 If you give the wrong server address
to client or if you don’t execute the server code first then error occurs as “server not ready”. Otherwise,
connection will be established and you can communicate with each other.
Server.c
Client.c
Contributed by,
Parth Sapra
Happy Patel
Yukta Patel
Mohit Pachani
Awesome !! Great Thing ππ
ReplyDeleteNice oneπ
ReplyDeleteIt is very useful and easy to understand...
ReplyDelete