SlideShare une entreprise Scribd logo
1  sur  32
Socket Programming
Mostak Ahmed
Gauhati University
2
What’s a Socket?
• A socket is a communication
mechanism that allows
client/systems to be developed
either locally or across networks.
Ethernet
Client ServerClient
3
Connection-Oriented
Protocol
serversocket()
bind()
listen()
accept()
socket()
connect()
write()
read()write()
read()
connection
establishment
data (request)
data(reply)
client
blocks until
connection from client
4
Connectionless Protocol
socket()
bind()
recvfrom()
sendto()
socket()
sendto()
recvfrom()
Server
Client
blocks until data
received from a client data(request)
data(reply)
process request
5
The Steps in Establishing
a Socket on Server
• Create a socket with the socket()
system call
• Bind the socket to an address using
the bind() system call. For a server
socket, an address consists of a
port number on the host machine
• Listen for connections with listen()
system call
• Accept a connection with the
accept() system call. This call
6
The Steps in Establishing
a Socket on Client
• Create a socket with the socket()
system call
• Connect the socket to the address
of the server using the connect()
system call
• Send and receive data. There are a
number of ways to do this, but the
simplest is to use the read() and
write() or send() and recv()system
calls
7
Kinds of Socket
• Traditional BSD families
• TCP/IP (AF_INET,Internet)
• UNIX (AF_UNIX)
• XNS (AF_NS)
• APPLETALK,DECNET,IPX
8
Socket Address
A sockadd_in is a structure
containing an internet address. This
structure must defined in
<netinet/in.h>
struct sockadd_in
{ short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
9
Socket System Call
int socket (int family, int type, int
protocol);
int socket (int domain, int type, int
protocol);
• Domain parameter specifies a
communication domain;this selects the
protocol family which will be used for
communication
• Type specifies the communication
semantics
10
Start Server Code(1)
• #include <sys/types.h>
This header file contains definitions
of a number of data types used in
system calls. These types are used
in the next two include files.
• #include <sys/socket.h>
The header file socket.h includes a
number of definitions of structures
needed for sockets.
• #include <netinet/in.h>
The header file in.h contains
11
Start Server Code(2)
int main( )
{
int sockfd, newsockfd, portno, clilen;
• sockfd and newsockfd are file
descriptors, i.e. array subscripts
into the file descriptor table. These
two variables store the values
returned by the socket system call
and the accept system call.
• portno stores the port number on
which the server accepts
connections.
12
Start Server Code(3)
char buffer[256];
• The server reads characters from
the socket connection into this
buffer
struct sockaddr_in serv_addr, cli_addr;
• The variable serv_addr will contain
the address of server
• The variable cli_addr will contain the
address of client
• To create socket and return a
socket descriptor that can be used
for accessing the socket
sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd < 0) error(“Error opening Socket”);
AF_INET is family of IPv4 Internet Protocol
13
Start Server Code(4)
serv_addr.sin_family = AF_INET;
• The variable serv_addr is a structure
of type struct sockaddr_in. This
structure has four fields. The first
field is short sin_family, which
contains a code for the address
family. It should always be set to
the symbolic constant AF_INET.
serv_addr.sin_port = htons(portno);
• The second field of serv_addr is
unsigned short sin_port , which
contain the port number. However,
instead of simply copying the port
number to this field, it is necessary
to convert this to network byte
14
Byte Ordering
• different byte ordering in varoius
platform
– little endian : Intel 80x86 , DEC VAX ,
DEC PDP-11
– big endian : IBM 370 , Motorola
68000 , Pyramid
high-order byte low-order byte
high-order byte low-order byte
addr A
addr A
addr A+1
addr A+1
little endian
big endian
15
Byte Ordering Routines
Network Byte Order is big endian
u_long htonl (u_long hostlong);
//convert host-to-network,long int
u_short htons(u_short hostshort);
//convert host-to network,short int
u_long ntohl (u_long netlong); //
convert network-to-host,long int
u_short ntohs (u_short netshort); //
convert network-to-host,short integer
16
Start Server Code(5)
serv_addr.sin_addr.s_addr = INADDR_ANY;
• The third field of sockaddr_in is a
structure of type struct in_addr
which contains only a single field
unsigned long s_addr. This field
contains the IP address of the host.
For server code, this will always be
the IP address of the machine on
which the server is running, and
there is a symbolic constant
INADDR_ANY which gets this
17
Start Server Code(6)
if (bind(sockfd, (struct sockaddr *)
&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
• The bind() system call binds a
socket to an address, in this case
the address of the current host and
port number on which the server
will run.
• bind() takes three arguments,
– the socket file descriptor
– the address to which is bound
– the size of the address to which it is
bound.
• The second argument is a pointer to
a structure of type sockaddr, but
what is passed in is a structure of
type sockaddr_in, and so this must
18
Start Server Code(7)
listen(sockfd,5);
• The listen system call allows the
process to listen on the socket for
connections.
– The first argument is the socket file
descriptor
– The second is the size of the backlog
queue: the number of connections that
can be waiting while the process is
handling a particular connection.The
maximum size permitted by most
systems.
19
Start Server Code(8)
clilen = sizeof(struct sockaddr_in);
newsockfd = accept(sockfd, (struct
sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on
accept");
• The accept() system call causes the
process to block until a client
connects to the server.
• It wakes up the process when a
connection from a client has been
successfully established.
• It returns a new file descriptor, and
all communication on this
connection should be done using
the new file descriptor.
20
Start Server Code(9)
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from
socket");
printf("Here is the message: %sn",buffer);
• This code reads from the socket,the
read call uses the new file
descriptor, the one returned by
accept(), not the original file
descriptor returned by socket().
21
Start Server Code(10)
n=write(newsockfd,"I got your
message",18);
if (n < 0) error("ERROR writing to socket");
• Once a connection has been
established, both ends can both
read and write to the connection.
• Naturally, everything written by the
client will be read by the server, and
everything written by the server will
be read by the client.
• This code simply writes a short
message to the client. The last
22
Start Server Code(11)
close (sockfd);
close(newsockfd)
}
• The close() system call closes a
socket descriptor of sockfd and
newsockfd and terminates program
23
Start Client Code(1)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
• The header files are the same as for
the server
24
Start Client Code(2)
int main(int argc, char *argv[])
{
int sockfd, portno,n;
struct sockaddr_in serv_addr;
char buffer[256];
• All of this code is the same as that
in the server
if (argc < 2) {
fprintf(stderr,"usage %s IP address and
portn", argv[0]);
exit(0); }
• The user needs to pass in the port
number on which the server will
accept connections as an argument.
25
Start Client Code(3)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
• The socket() system call creates a new
socket. It takes three arguments
– 1st
argument is family:AF_INET is
family of IPv4 Internet Protocol
– 2nd
argument is type:SOCK_STREAM
is type of connection-orient(TCP),
SOCK_DGRAM is type of
connectionless (UDP)
– 3rd
argument is the protocol: If this
argument is zero, the operating
system will choose the most
26
Start Client Code(4)
serv_addr.sin_family = AF_INET;
• The variable serv_addr is a structure
of type struct sockaddr_in. This
structure has four fields. The first
field is short sin_family, which
contains a code for the address
family. It should always be set to the
symbolic constant AF_INET
serv_addr.sin_port = htons(portno);
• serv_addr is unsigned short
sin_port , which contain the port
number. However, instead of simply
copying the port number to this field,
27
Start Client Code(5)
inet_pton(AF_INET,
argv[1],&serv_addr.sin_addr);
• This function converts the character
string from first argument, Server
address into a network address
structure in the AF address
if
(connect(sockfd,&serv_addr,sizeof(serv_ad
dr)) < 0)
error("ERROR connecting");
• The connect function is called by the
client to establish a connection to the
server. It takes three arguments
– The socket file descriptor,
– The address of the host to which it
28
Start Client Code(6)
n=write(sockfd,“This is my message",18);
if (n < 0) error("ERROR writing to socket");
• Once a connection has been
established, both ends can both
read and write to the connection.
• Naturally, everything written by the
client will be read by the server, and
everything written by the server will
be read by the client.
• This code simply writes a short
message to the client. The last
29
Start Client Code(7)
n = read(sockfd,buffer,255);
if (n < 0) error("ERROR reading from
socket");
printf("Here is the message: %sn",buffer);
• This code reads from the socket
close(sockfd);
• The close() system call closes a
socket descriptor of sockfd
30
Enhancements to The
Server Code
To allow the server to handle
multiple simultaneous
connections
1. Put the accept statement and the
following code in an infinite loop.
2. After a connection is established,
call fork() to create a new process.
3. The child process will close sockfd
and call dostuff(), passing the new
socket file descriptor as an
argument. When the two processes
31
Enhancements to The
Server Code
while (1) {
newsockfd = accept(sockfd, (struct
sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd);
dostuff(newsockfd);
exit(0); }
else close(newsockfd);
32
Enhancements to The
Server Code
void do_stuff (int sock)
{
int n; char buffer[256];
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from
socket");
printf("Here is the message:
%sn",buffer);
n = write(sock,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
}

Contenu connexe

Tendances

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
elliando dias
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
elliando dias
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
Sasi Kala
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
elliando dias
 

Tendances (20)

Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Networking lab
Networking labNetworking lab
Networking lab
 
Sockets
SocketsSockets
Sockets
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Programming TCP/IP with Sockets
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Sockets
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
 
Java sockets
Java socketsJava sockets
Java sockets
 
Networks lab manual ecp62
Networks lab manual ecp62Networks lab manual ecp62
Networks lab manual ecp62
 
Sockets
SocketsSockets
Sockets
 

Similaire à Socket Programming

Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
shanmuga priya
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
secunderbadtirumalgi
 

Similaire à Socket Programming (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Sockets
Sockets Sockets
Sockets
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
A.java
A.javaA.java
A.java
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
lab04.pdf
lab04.pdflab04.pdf
lab04.pdf
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
 
Remote Procedure Call
Remote Procedure CallRemote Procedure Call
Remote Procedure Call
 
Os 2
Os 2Os 2
Os 2
 
Np unit2
Np unit2Np unit2
Np unit2
 
Socket
SocketSocket
Socket
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
03 sockets
03 sockets03 sockets
03 sockets
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
 
Java 1
Java 1Java 1
Java 1
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYAPYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
 

Dernier

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Dernier (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 

Socket Programming

  • 2. 2 What’s a Socket? • A socket is a communication mechanism that allows client/systems to be developed either locally or across networks. Ethernet Client ServerClient
  • 5. 5 The Steps in Establishing a Socket on Server • Create a socket with the socket() system call • Bind the socket to an address using the bind() system call. For a server socket, an address consists of a port number on the host machine • Listen for connections with listen() system call • Accept a connection with the accept() system call. This call
  • 6. 6 The Steps in Establishing a Socket on Client • Create a socket with the socket() system call • Connect the socket to the address of the server using the connect() system call • Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() or send() and recv()system calls
  • 7. 7 Kinds of Socket • Traditional BSD families • TCP/IP (AF_INET,Internet) • UNIX (AF_UNIX) • XNS (AF_NS) • APPLETALK,DECNET,IPX
  • 8. 8 Socket Address A sockadd_in is a structure containing an internet address. This structure must defined in <netinet/in.h> struct sockadd_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; };
  • 9. 9 Socket System Call int socket (int family, int type, int protocol); int socket (int domain, int type, int protocol); • Domain parameter specifies a communication domain;this selects the protocol family which will be used for communication • Type specifies the communication semantics
  • 10. 10 Start Server Code(1) • #include <sys/types.h> This header file contains definitions of a number of data types used in system calls. These types are used in the next two include files. • #include <sys/socket.h> The header file socket.h includes a number of definitions of structures needed for sockets. • #include <netinet/in.h> The header file in.h contains
  • 11. 11 Start Server Code(2) int main( ) { int sockfd, newsockfd, portno, clilen; • sockfd and newsockfd are file descriptors, i.e. array subscripts into the file descriptor table. These two variables store the values returned by the socket system call and the accept system call. • portno stores the port number on which the server accepts connections.
  • 12. 12 Start Server Code(3) char buffer[256]; • The server reads characters from the socket connection into this buffer struct sockaddr_in serv_addr, cli_addr; • The variable serv_addr will contain the address of server • The variable cli_addr will contain the address of client • To create socket and return a socket descriptor that can be used for accessing the socket sockfd = socket(AF_INET,SOCK_STREAM,0); if (sockfd < 0) error(“Error opening Socket”); AF_INET is family of IPv4 Internet Protocol
  • 13. 13 Start Server Code(4) serv_addr.sin_family = AF_INET; • The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET. serv_addr.sin_port = htons(portno); • The second field of serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field, it is necessary to convert this to network byte
  • 14. 14 Byte Ordering • different byte ordering in varoius platform – little endian : Intel 80x86 , DEC VAX , DEC PDP-11 – big endian : IBM 370 , Motorola 68000 , Pyramid high-order byte low-order byte high-order byte low-order byte addr A addr A addr A+1 addr A+1 little endian big endian
  • 15. 15 Byte Ordering Routines Network Byte Order is big endian u_long htonl (u_long hostlong); //convert host-to-network,long int u_short htons(u_short hostshort); //convert host-to network,short int u_long ntohl (u_long netlong); // convert network-to-host,long int u_short ntohs (u_short netshort); // convert network-to-host,short integer
  • 16. 16 Start Server Code(5) serv_addr.sin_addr.s_addr = INADDR_ANY; • The third field of sockaddr_in is a structure of type struct in_addr which contains only a single field unsigned long s_addr. This field contains the IP address of the host. For server code, this will always be the IP address of the machine on which the server is running, and there is a symbolic constant INADDR_ANY which gets this
  • 17. 17 Start Server Code(6) if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); • The bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run. • bind() takes three arguments, – the socket file descriptor – the address to which is bound – the size of the address to which it is bound. • The second argument is a pointer to a structure of type sockaddr, but what is passed in is a structure of type sockaddr_in, and so this must
  • 18. 18 Start Server Code(7) listen(sockfd,5); • The listen system call allows the process to listen on the socket for connections. – The first argument is the socket file descriptor – The second is the size of the backlog queue: the number of connections that can be waiting while the process is handling a particular connection.The maximum size permitted by most systems.
  • 19. 19 Start Server Code(8) clilen = sizeof(struct sockaddr_in); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); • The accept() system call causes the process to block until a client connects to the server. • It wakes up the process when a connection from a client has been successfully established. • It returns a new file descriptor, and all communication on this connection should be done using the new file descriptor.
  • 20. 20 Start Server Code(9) n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %sn",buffer); • This code reads from the socket,the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned by socket().
  • 21. 21 Start Server Code(10) n=write(newsockfd,"I got your message",18); if (n < 0) error("ERROR writing to socket"); • Once a connection has been established, both ends can both read and write to the connection. • Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. • This code simply writes a short message to the client. The last
  • 22. 22 Start Server Code(11) close (sockfd); close(newsockfd) } • The close() system call closes a socket descriptor of sockfd and newsockfd and terminates program
  • 23. 23 Start Client Code(1) #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> • The header files are the same as for the server
  • 24. 24 Start Client Code(2) int main(int argc, char *argv[]) { int sockfd, portno,n; struct sockaddr_in serv_addr; char buffer[256]; • All of this code is the same as that in the server if (argc < 2) { fprintf(stderr,"usage %s IP address and portn", argv[0]); exit(0); } • The user needs to pass in the port number on which the server will accept connections as an argument.
  • 25. 25 Start Client Code(3) sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); • The socket() system call creates a new socket. It takes three arguments – 1st argument is family:AF_INET is family of IPv4 Internet Protocol – 2nd argument is type:SOCK_STREAM is type of connection-orient(TCP), SOCK_DGRAM is type of connectionless (UDP) – 3rd argument is the protocol: If this argument is zero, the operating system will choose the most
  • 26. 26 Start Client Code(4) serv_addr.sin_family = AF_INET; • The variable serv_addr is a structure of type struct sockaddr_in. This structure has four fields. The first field is short sin_family, which contains a code for the address family. It should always be set to the symbolic constant AF_INET serv_addr.sin_port = htons(portno); • serv_addr is unsigned short sin_port , which contain the port number. However, instead of simply copying the port number to this field,
  • 27. 27 Start Client Code(5) inet_pton(AF_INET, argv[1],&serv_addr.sin_addr); • This function converts the character string from first argument, Server address into a network address structure in the AF address if (connect(sockfd,&serv_addr,sizeof(serv_ad dr)) < 0) error("ERROR connecting"); • The connect function is called by the client to establish a connection to the server. It takes three arguments – The socket file descriptor, – The address of the host to which it
  • 28. 28 Start Client Code(6) n=write(sockfd,“This is my message",18); if (n < 0) error("ERROR writing to socket"); • Once a connection has been established, both ends can both read and write to the connection. • Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. • This code simply writes a short message to the client. The last
  • 29. 29 Start Client Code(7) n = read(sockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %sn",buffer); • This code reads from the socket close(sockfd); • The close() system call closes a socket descriptor of sockfd
  • 30. 30 Enhancements to The Server Code To allow the server to handle multiple simultaneous connections 1. Put the accept statement and the following code in an infinite loop. 2. After a connection is established, call fork() to create a new process. 3. The child process will close sockfd and call dostuff(), passing the new socket file descriptor as an argument. When the two processes
  • 31. 31 Enhancements to The Server Code while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); pid = fork(); if (pid < 0) error("ERROR on fork"); if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); } else close(newsockfd);
  • 32. 32 Enhancements to The Server Code void do_stuff (int sock) { int n; char buffer[256]; n = read(sock,buffer,255); if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %sn",buffer); n = write(sock,"I got your message",18); if (n < 0) error("ERROR writing to socket"); }