vjexp2.1
Transcript of vjexp2.1
-
8/16/2019 vjexp2.1
1/20
EX NO 2 INTRODUCTION TO SOCKET PROGRAMMING
Aim:
To study about the basics of Socket in Network Programming
Network Programming
Network programming involves writing programs that communicate with other programs
across a computer N/W. One program is normally called the client and the other the server.
!ommon e"amples in the T!P/#P are web clients $browsers% & Web Servers 'TP clients &
server and Telnet clients & servers.
To facilitate communication between unrelated processes and to standardi(e network
programming an )P# is needed. There are two such )P#s*
+. Sockets sometimes called ,-erkeley Sockets. 0T# $0/open transport interface%
Socket
#n T!P/#P an addressable point that consists of an #P address and a T!P or 12P port
member that provides application with access to T!P/#P protocol is called Socket.
) socket is an abstraction that represents an endpoint of communication. The operations
that can be performed on a socket include control operations $such as associating a port number
with the socket initiating or accepting a connection on the socket or destroying the socket% data
transfer operations $such as writing data through the socket to some other application or reading
data from some other application through the socket% and status operations $such as finding the#P address associated with the socket%. The complete set of operations that can be performed on a
socket constitutes the Socket API $)pplication Programming #nterface%.
Str!ct!re
Structures are used in socket programming to hold information about the address. The
generic socket address structure is defined below*
struct sockaddr
3
unsigned short sa4family5 /6 address family 6/
char sa4data7+895 /6 +8 bytes of protocol address6/
:5
-
8/16/2019 vjexp2.1
2/20
IP"# Socket A$$re Str!ct!re
This structure is also called as ;#nternet socket address structure header.
struct in4addr 3
in4addr4t s4addr5 /6 ?@bit #Pv8 address network byte ordered 6/
:5
struct sockaddr4in3
unit4t sin4len5 /6 length of structure $+A byte% 6/
sa4family4t sin4family5 /6)'4#NBT6/
in4port4t sin4port5 /6 +A@bit T!P or 12P port number 6/
/6 network byte ordered 6/
struct in4addr sin4addr5 /6?@bit #pv8 address network byte ordered 6/
char sin4(ero7C95 /6 unused D initiali(e to all (eroes 6/
:5
Im%ortant &!nction'(ocket)*
This function is called by both T!P server and client process to create an empty socket.
Einclude =sys/socket.h>
int socket $int family int type int protocol%5
family* specifies the protocol family and is one of the constants below*
+ami,- Decri%tion
)'4#NBT #Pv8 protocols
)'4#NBTA #PvA protocols
)'4FO!)F 1ni" domain protocols
)'4GO1TB Gouting sockets
)'4HBI Hey sockets
type* indicates communications semantics
SO!H4STGB)J @ stream socket
SO!H42KG)J @ datagram socketSO!H4G)W @ raw socket
protocol* set to L e"cept for raw sockets.
Geturns on success* socket descriptor $a small nonnegative integer% on error* @+
2( .in$)*
-
8/16/2019 vjexp2.1
3/20
The bind function assigns a local protocol address to a socket. The protocol address is the
combination of either a ?@bit #PM8 address or a +C@bit #PMA address along with a +A@bit T!P
or 12P port number.
Einclude =sys/socket.h>
int bind$int sockfd const struct sockaddr 6myaddr socklen4t addrlen%5
sockfd* a socket descriptor returned by the socket function.
6myaddr* a pointer to a protocol@specific address.
addrlen* the si(e of the socket address structure.
Geturns on success* L on error* @+
/( connect)*
The connect function is used by a T!P client to establish a connection with a T!P server.
Einclude =sys/socket.h>
int connect$int sockfd const struct sockaddr 6servaddr socklen4t addrlen%5
sockfd* a socket descriptor returned by the socket function
6servaddr* a pointer to a socket address structure
addrlen* the si(e of the socket address structure
Geturns on success* L on error* @+
#( ,iten)*
The listen function is called only by a T!P server to converts an unconnected socket into a
passive socket indicating that kernel should accept incoming connection reuests directed to its
socket.
Einclude=sys/socket.h>int listen $int sockfd int backlog%5
sockfd* a socket descriptor returned by the socket function.
backlog* ma"imum number of connections that the kernel should ueue for this socket.
Geturns on success* L on error* @+
0( acce%t)*
The accept function is called by the T!P server to return the ne"t completed connection from the
front of the completed connection ueue.
Einclude=sys/socket.h>int accept $int sockfd struct sockaddr 6cliaddr socklen4t 6addrlen%5
sockfd* This is the same socket descriptor as in listen call.
6cliaddr* used to return the protocol address of the connected peer process
6addrlen* length of the address.
Geturns on success* a new $connected%socket descriptor on error*@+
-
8/16/2019 vjexp2.1
4/20
1( c,oe)*
The close function is used to close a socket and terminate a T!P connection.
Einclude =unistd.h>
int close $int sockfd%5
sockfd* This socket descriptor is no longer useable.
Geturns on success* L on error* @+
( rea$)*
The read function is used to receive data from the specified socket.
Einclude =unistd.h>
ssi(e4t read$int sockfd const void 6 buf si(e4t nbytes%5
sockfd* a socket descriptor returned by the socket function.
buf* buffer to store the data.
nbytes* si(e of the buffer
Geturns* number of bytes read if OHL on BO' @+ on error
3( write)*
The write function is used to send the data through the specified socket.
Einclude =unistd.h>
ssi(e4t write$int sockfd const void 6 buf si(e4t nbytes%5
sockfd* a socket descriptor returned by the socket function.
buf* buffer to store the data.
nbytes* si(e of the buffer
Geturns* number of bytes written if OHL on BO' @+ on error
4( en$to)*
This function is similar to the write function but additional arguments are reuired.
Einclude=sys/socket.h>
ssi(e4t sendto$int sockfd const void 6buff si(e4t nbyte int flag
const struct sockaddr 6to socklen4t addrlen%5
sockfd D socket descriptor
6buff D pointer to buffer to write from.
nbytes D number of bytes to write.to D socket address structure containing the protocol address of where the data is to be sent.
addrlen D si(e of the socket address structure
Geturns* number of bytes read or written if OH@+ on error
'5( rec"&rom)*
This function is similar to the read function but additional arguments are reuired.
-
8/16/2019 vjexp2.1
5/20
Einclude=sys/socket.h>
ssi(e4t recvfrom$int sockfd void 6buff si(e4t nbyte int flag
struct sockaddr 6from socklen4t 6addrlen%5
sockfd D socket descriptor
6buff D pointer to buffer to read.nbytes D number of bytes to read.
addrlen D si(e of the socket address structure
from @ socket address structure of who sent the datagram.
Geturns* number of bytes read or written if OH@+ on error
-
8/16/2019 vjexp2.1
6/20
EOF notifcation
data ( reply)
data ( request)
Connection establishment
blocks until connection rom client
process request
TCP Client
TCP Server
socket()
socket()
bind()
listen()
accept()
read()write()
read()
write()
read()
close()
close()
conect()
Socket &!nction &or connection6oriente$ comm!nication
-
8/16/2019 vjexp2.1
7/20
UDP Client
UDP Server
Blocks until datagram receied rom a client
!rocess request
"ata (reply)
"ata (request)
close()
recrom()
sendto()
sendto()
recrom()
bind()
socket()
socket()
Socket &!nction &or connection6,e comm!nication
-
8/16/2019 vjexp2.1
8/20
Program to create a ocket
/6 B"@+ Program To !reate a Socket 6/
Einclude =stdio.h> /6 NBB2B2 B)2BG '#FBS 6/
Einclude =stdlib.h>
Einclude =unistd.h>
Einclude =sys/socket.h>
Einclude=netinet/in.h>
Einclude=arpa/inet.h>
int main$%
3
int sockfd+sockfd5 /6 sokcet file descriptors 6/
sockfd+socket$)'4#NBTSO!H4STGB)JL%5 /6 socket system call 6/
sockfdsocket$P'4#NBTSO!H42KG)JL%5
-
8/16/2019 vjexp2.1
9/20
if$sockfd+@+% /6 error checking 6/
3
printf$QSocket+ not !reatedRnQ%5
:
else
3
printf$QSocket+ !reated and Rt Socket+ 'ile 2escriptor value is d RnQsockfd+%5
if$sockfd@+%
3
printf$Qsocket creation errorQ%5
:
else
3
printf$QSocket created and Rt socket descriptor value is dRnQsockfd%5
:
::
-
8/16/2019 vjexp2.1
10/20
O!t%!t:
Socket ' &i,e $ecri%tion "a,!e i /
Socket # &i,e $ecri%tion "a,!e i #
Program to 7in$ a ocket
/6 B"@ Program to -ind a Socket 6/
Einclude=stdio.h>
Einclude=sys/socket.h>
Einclude=arpa/inet.h>
Einclude=netinet/in.h>
Edefine POGTNO LLL /6 Type definition of a Port number 6/
int main$%
3
-
8/16/2019 vjexp2.1
11/20
int sockfdiPOGTNO5
struct sockaddr4in myaddr5 /6 -uiltin structure for internet address 6/
if$$sockfdsocket$)'4#NBTSO!H4STGB)JL%%@+%
3printf$QSocket !reation BrrorRnQ%5:
myaddr.sin4family)'4#NBT5 /6 Structure variable definition 6/
myaddr.sin4porthtons$POGTNO%5
myaddr.sin4addr.s4addr#N)22G4)NI5
memset$&$myaddr.sin4(ero%RLC%5 /6 fills with constant byte to remaining space 6/
/6 socket -inding process and error checking 6/
if$bind$sockfd$struct sockaddr 6%&myaddrsi(eof$struct sockaddr%%U@+%
3
printf$Q Socket is -inded at port dRnQi%5
:
else
3
printf$Q-inding BrrorRnQ%5
:
:
-
8/16/2019 vjexp2.1
12/20
OUTPUT:
SOCKET IS 7INDED AT PORT 2555
Program to im%,ement ,iten)* -tem ca,,
/6 B"@? Program to implement F#STBN system call 6/
Einclude =stdio.h> /6 These are the usual header files 6/
Einclude =sys/types.h>
-
8/16/2019 vjexp2.1
13/20
Einclude =sys/socket.h>
Einclude =netinet/in.h>
Edefine POGT ?VVL /6)ssigning Port number 6/
Edefine -)!HFOK + /6 Number of allowed connections 6/
main$%
3
int fd5 /6 file descriptors 6/
struct sockaddr4in server5 /6 servers address information 6/
struct sockaddr4in client5 /6 clients address information 6/
int sin4si(e5
int "5
if $$fdsocket$)'4#NBT SO!H4STGB)J L%% @+ %3 /6 calls socket$% 6/
printf$Qsocket$% errorRnQ%5
e"it$@+%5
:
server.sin4family )'4#NBT5
server.sin4port htons$POGT%5 /6 Gemember htons$% from Q!onversionsQ section % 6/
-
8/16/2019 vjexp2.1
14/20
server.sin4addr.s4addr #N)22G4)NI5 /6 #N)22G4)NI puts your #P address
automatically 6/
b(ero$&$server.sin4(ero%C%5 /6 (ero the rest of the structure 6/
if$bind$fd$struct sockaddr6%&serversi(eof$struct sockaddr%%@+% /6 calls bind$% 6/
3 printf$Qbind$% errorRnQ%5
e"it$@+%5
:
"listen$fd-)!HFOK% 5 /6 calls listen$% 6/
if$"@+%
3
printf$Qlisten$% errorRnQ%5
e"it$@+%5
:
else
3
printf$Server is in listening mode Rn %5
:
close$fd%5 /6 close fd 6/
:
-
8/16/2019 vjexp2.1
15/20
-
8/16/2019 vjexp2.1
16/20
OUTPUT:
Ser"er i in ,itening mo$e(
Program &or acce%t)* -tem ca,,
-
8/16/2019 vjexp2.1
17/20
/6 B"@? Program to implement )!!BPT system calls 6/
Einclude =stdio.h> /6 These are the usual header files 6/
Einclude =sys/types.h>
Einclude =sys/socket.h>
Einclude =netinet/in.h>
Edefine POGT ?VVL /6 )ssigning Port numbers 6/
Edefine -)!HFOK /6 Number of allowed connections 6/
main$%
3
int fd fd5 /6 file descriptors 6/
struct sockaddr4in server5 /6 servers address information 6/
struct sockaddr4in client5 /6 clients address information 6/
int sin4si(e5
if $$fdsocket$)'4#NBT SO!H4STGB)J L%% @+ % /6 calls socket$% 6/
3
printf$Qsocket$% errorRnQ%5
e"it$@+%5
-
8/16/2019 vjexp2.1
18/20
-
8/16/2019 vjexp2.1
19/20
3
sin4si(esi(eof$struct sockaddr4in%5
if $$fd accept$fd$struct sockaddr 6%&client&sin4si(e%%@+%3 /6 calls accept$% 6/
printf$Qaccept$% errorRnQ%5
e"it$@+%5
:
else
printf$Q Server is in accept mode Q%5
printf$QIou got a connection from sRnQinet4ntoa$client.sin4addr% %5 /6 prints clients #P 6/
close$fd%5 /6 close fd 6/
:
:
-
8/16/2019 vjexp2.1
20/20
OUTPUT:
cc("8k!mar
(9a(o!t7in$)*error
Re!,t:
Thus the basics of socket with e"ample peograms are studied.