Вы находитесь на странице: 1из 39

Socket Programming

Computer Networks Lab

Course Instructor : Dr. Sasthi C. Ghosh


T.A : Durgesh Singh

Indian Statistical Institute - Kolkata

15 mars 2019
Introduction to Sockets

I What is a Socket and where does it come into picture ?


I They are used as interface in network applications or processes
to make them interact with each other.
I An Abstraction for two endpoints (same machine/on network)
to communicate with each other.
I Sockets are provided as an Application Program Interface
(API) :- APIs depend upon the Operating system and the
programming language being used.
One such example is Berkely sockets developed for UNIX
systems.
I We will work with internet communication protocols
(TCP/UDP).
I Sockets are identified as : IP-address+ process address (Port)
and Protocol (TCP/UDP etc.)
Introduction to Sockets
Application

Sockets

Ports(TCP/UDP)

Transport layer
TCP/UDP
Network

Internet Layer Physical Layer

Figure – Sockets as APIs


Example :
process, protocol, port number
———————————————-
I FTP, TCP, 21
Telnet, TCP, 23
DNS-lookup, UDP, 53.
I These are reserved Ports (system ports) used by server
(1-1023) also called well known ports.
I We will usually see TCP and UDP transport layer protocols
(which you will read in detail in class).
I TCP : Reliable Byte-stream Service , flow control , connection
oriented.
I UDP : Best effort data gram service , no acknowledgment , no
retransmission , connectionless.
I If host machine 1 IP address : 192.168.25.28, process is FTP,
associated port number : 21 and usually TCP protocol is used.
Socket is defined as 192.168.25.28 :21 with TCP.
Examples Contd.

I If host machine 2 IP address : 48.71.21.12, process is FTP,


associated port number : 21 and usually TCP protocol is used.
Socket is defined as 48.71.21.12 :21 with TCP.
I We will develop a concurrent echo server in c that will take
request from client and display its message.
I We need to know some basic commands.
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
I X receive
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
I X receive
I X close
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
I X receive
I X close
I close X
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
I X receive
I X close
I close X
I waits X
Client-Server Architecture
Example : website surfing.
Server(youtube) Client(Browser)
I created X
I waits X
I ” created
I ” <—request
I connected ”
I reply—> X
I X receive
I X close
I close X
I waits X
I ” X
Association

I connection : communication link between two processes.


I association is a term for 5 tuple that completely specifies those
two processes that make up connection.
I {protocol, local-addr, local-process, foreign-addr,
foreign-process}
I e.g., {tcp, 192.168.42.39, 1500, 192.168.25.28, 21}
I half-association : is either {protocol, local-addr, local-process}
or {protocol, foreign-addr, foreign-process} which specify each
half of a connection.
I We also called half-association as socket or transport address.
Hence socket pair corresponds to association.
Let’s Look into the Basic
System Calls
Basic System Calls
Server Client

socket() socket()

bind()

listen()

connect()
accept()

send()
recv()

send() recv()

close()
close()

Figure – Client-Server Communication using TCP


Basic System Calls

I socket() : Creates a new communication end point.


I bind() : Bind local address to the socket.
I listen() : Willingness to accept connections.
I accept() : Block caller until a connection request arrives.
I connect() : Actively attempts to establish a connection.
I send() : Send data over the connection.
I recv() : Receive data over the connection.
I close() : Release the connection
Basic System Calls & Association Tuple matrix

5-tuple → protocol local address, foreign address,


Environment ↓ local process foreign process
conn. oriented Server socket() bind() listen(), accept()
conn. oriented Client socket() connect()
conn. less Server socket() bind() recvfrom()
conn. less Client socket() bind() sendto()
Some useful Routines

I Byte Ordering routines : htons(), htonl(), ntohl(), ntohs()


I Byte Operations : bcopy(), bzero()
I Address Conversion Routines : inet_addr(), inet_ntoa()
Listing 1 – Echo Server program (server)

1 #i n c l u d e < s t d i o . h>
2 ...
3
4 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
5 {
6 i n t s o c k i d , new_sock_id , c l i _ l e n , c h i l d _ p i d ;
7
8
9 struct sockaddr_in cli_addr , serv_addr ;
10
11 // o p e n i n g a TCP s o c k e t ( i n t e r n e t stream s o c k e t ) ∗∗∗∗
12
13 // b i n d i n g the local address
14
15 // s t a r t listening for connections
16
17 // w a i t for c o n n e c t i o n from client
18
19 // A c c e p t t h e call from client and r e p l y the request .
20
21 return 0;
22 }
Listing 2 – Echo Server program (server)
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <s y s / s o c k e t . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 ...
5
6 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
7 {
8 i n t s o c k i d , new_sock_id , c l i _ l e n , c h i l d _ p i d ;
9
10
11 s t r u c t sockaddr_in cli_addr , serv_addr ;
12 // pname=a r g v [ 0 ] ;
13
14 // o p e n i n g a TCP s o c k e t ( i n t e r n e t s t r e a m s o c k e t )
15 i f ( ( s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ) < 0 )
16 {
17 p e r r o r ( " cannot o p e i n s o c k e t \n" ) ;
18 exit (1);
19 }
20
21 p r i n t f (" socket c r e a t e d \n" ) ;
22
23 // b i n d i n g the local address ∗∗∗∗
24
25 // s t a r t listening for connections
26
27
28 // w a i t for c o n n e c t i o n from client
29
30 // A c c e p t t h e call from client and r e p l y the request .
31
32
33 return 0;
34 }
Listing 3 – Echo Server program (server)
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <s y s / s o c k e t . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 ...
5
6 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
7 {
8 i n t s o c k i d , new_sock_id , c l i _ l e n , c h i l d _ p i d ;
9
10
11 s t r u c t sockaddr_in cli_addr , serv_addr ;
12 // pname=a r g v [ 0 ] ;
13
14 // o p e n i n g a TCP s o c k e t ( i n t e r n e t s t r e a m s o c k e t )
15 i f ( ( s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ) < 0 )
16 {
17 p e r r o r ( " cannot o p e i n s o c k e t \n" ) ;
18 exit (1);
19 }
20
21 p r i n t f (" socket c r e a t e d \n" ) ;
22
23 b z e r o ( ( c h a r ∗ ) &s e r v _ a d d r , s i z e o f ( s e r v _ a d d r ) ) ;
24 s e r v _ a d d r . s i n _ f a m i l y = AF_INET ;
25 s e r v _ a d d r . s i n _ a d d r . s_addr = h t o n l (INADDR_ANY ) ;
26 s e r v _ a d d r . s i n _ p o r t = h t o n s ( SERV_PORT ) ;
27
28 // b i n d i n g t h e l o c a l a d d r e s s
29 i f ( b i n d ( s o c k i d , ( s t r u c t s o c k a d d r ∗)& s e r v _ a d d r , s i z e o f ( s e r v _ a d d r )) <0)
30 {
31 perror (" bind () f a i l e d " ) ;
32 exit (1);
33 }
34
35
36 p r i n t f ( " b i n d i n g done . . . l i s t e n i n g s t a r t e d \n" ) ;
37
38
39 // s t a r t listening for c o n n e c t i o n s ∗∗∗∗
40
41
42 // w a i t for c o n n e c t i o n from client
43
44 // A c c e p t t h e call from client and r e p l y the request .
45
46
47 return 0;
48 }
Listing 4 – Echo Server program (server)
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <s y s / s o c k e t . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 ...
5 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
6 {
7 i n t s o c k i d , new_sock_id , c l i _ l e n , child_pid ;
8
9
10 s t r u c t sockaddr_in cli_addr , serv_addr ;
11 // pname=a r g v [ 0 ] ;
12
13 // o p e n i n g a TCP s o c k e t ( i n t e r n e t s t r e a m s o c k e t )
14 i f ( ( s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ) < 0 )
15 {
16 p e r r o r ( " cannot o p e i n s o c k e t \n" ) ;
17 exit (1);
18 }
19
20 p r i n t f (" socket c r e a t e d \n" ) ;
21
22 b z e r o ( ( c h a r ∗ ) &s e r v _ a d d r , s i z e o f ( s e r v _ a d d r ) ) ;
23 s e r v _ a d d r . s i n _ f a m i l y = AF_INET ;
24 s e r v _ a d d r . s i n _ a d d r . s_addr = h t o n l (INADDR_ANY ) ;
25 s e r v _ a d d r . s i n _ p o r t = h t o n s ( SERV_PORT ) ;
26
27 // b i n d i n g t h e l o c a l a d d r e s s
28 i f ( b i n d ( s o c k i d , ( s t r u c t s o c k a d d r ∗)& s e r v _ a d d r , s i z e o f ( s e r v _ a d d r )) <0)
29 {
30 perror (" bind () f a i l e d " ) ;
31 exit (1);
32 }
33 p r i n t f ( " b i n d i n g done . . . l i s t e n i n g s t a r t e d \ n " ) ;
34
35 l i s t e n ( sockid , 5);
36
37 // w a i t for c o n n e c t i o n from client
38
39 // A c c e p t t h e call from client and r e p l y the request .
40
41
42 return 0;
43 }
Listing 5 – Echo Server program (server)
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e <s y s / s o c k e t . h>
3 #i n c l u d e <s y s / t y p e s . h>
4 #i n c l u d e < s t d l i b . h>
5 #i n c l u d e < n e t i n e t / i n . h>
6 #i n c l u d e <a r p a / i n e t . h>
7 #i n c l u d e < s t r i n g . h>
8 #i n c l u d e < u n i s t d . h>
9
10 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
11 {
12 i n t s o c k i d , new_sock_id , c l i _ l e n , c h i l d _ p i d ;
13
14
15 s t r u c t sockaddr_in cli_addr , serv_addr ;
16 // pname=a r g v [ 0 ] ;
17
18 // o p e n i n g a TCP s o c k e t ( i n t e r n e t s t r e a m s o c k e t )
19 i f ( ( s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ) < 0 )
20 {
21 p e r r o r ( " cannot o p e i n s o c k e t \n" ) ;
22 exit (1);
23 }
24
25 p r i n t f (" socket c r e a t e d \n" ) ;
26
27 b z e r o ( ( c h a r ∗ ) &s e r v _ a d d r , s i z e o f ( s e r v _ a d d r ) ) ;
28 s e r v _ a d d r . s i n _ f a m i l y = AF_INET ;
29 s e r v _ a d d r . s i n _ a d d r . s_addr = h t o n l (INADDR_ANY ) ;
30 s e r v _ a d d r . s i n _ p o r t = h t o n s ( SERV_PORT ) ;
31
32 // b i n d i n g t h e l o c a l a d d r e s s
33 i f ( b i n d ( s o c k i d , ( s t r u c t s o c k a d d r ∗)& s e r v _ a d d r , s i z e o f ( s e r v _ a d d r )) <0)
34 {
35 perror (" bind () f a i l e d " ) ;
36 exit (1);
37 }
38 p r i n t f ( " b i n d i n g done . . . l i s t e n i n g s t a r t e d \n" ) ;
39
40 l i s t e n ( sockid , 5);
41
42 c h a r c [ 1 0 0 ] , c_w [ 1 0 0 ] ;
43 i n t rc ,w;
44 // w a i t f o r c o n n e c t i o n f r o m c l i e n t
45 c l i _ l e n= s i z e o f ( c l i _ a d d r ) ;
46 new_sock_id=a c c e p t ( s o c k i d , ( s t r u c t s o c k a d d r ∗)& c l i _ a d d r , &c l i _ l e n ) ;
47 i f ( new_sock_id <0){
48 perror (" accept e r r o r " ) ;
49 exit (1);
50 }
51 r c=r e a d ( new_sock_id , c , s i z e o f ( c ) ) ;
52 w=w r i t e ( new_sock_id , c , s i z e o f ( c ) ) ;
53 close ( sockid );
54 p r i n t f ( " c a l l c l o s e d \n" ) ;
55 return 0;
56 }
socket() system call

int sockid=socket(int family, int type, int protocol)


I sockid : socket descriptor (like a file-handle)
I family : Integer, communication domain
I PF_INET, IPv4 protocols, internet address (Typically used).
I PF_UNIX, local communication, file addresses.
I type : communication type
I SOCK_STREAM : connection oriented, reliable, 2-way.
I SOCK_DGRAM : connectionless, un-reliable
I protocol :specifies protocol
IPPROTO_TCP, IPPROTO_UDP.
usually set to 0 (use default protocol).
I sockid upon failure returns -1.
bind()

I socket() command do not specify addresses, it only create


communication interfaces.
I bind() will be used for this task which associates and reserves
a port for a socket.
int status = bind(int sockid, struct sockaddr *addrport, int
size) ;
I sockid : as described earlier, a socket descriptor.
I addrport : C structure which stores IP-address and port
number of machine.
I size : Size in bytes of addrport structure.
I Important to note that addrport is a pointer to a protocol
specific address (TCP/IP, UNIX, XNS etc.)
Listing 6 – Data Structures
1 // S o c k e t API d e f i n e s a g e n e r i c d a t a t y p e f o r a d d r e s s e s :
2 s t r u c t sockaddr {
3 u n s i g n e d s h o r t s a _ f a m i l y ; /∗ A d d r e s s f a m i l y ( e . g . AF_INET ) ∗/
4 c h a r s a _ d a t a [ 1 4 ] ; /∗ F a m i l y − s p e c i f i c a d d r e s s i n f o r m a t i o n ∗/
5 }
6 // P a r t i c u l a r f o r m o f t h e s o c k a d d r u s e d f o r TCP/ I P a d d r e s s e s :
7 s t r u c t in_addr {
8 u n s i g n e d l o n g s_addr ; /∗ I n t e r n e t a d d r e s s ( 3 2 b i t s ) ∗/
9 }
10 s t r u c t sockaddr_in {
11 u n s i g n e d s h o r t s i n _ f a m i l y ; /∗ I n t e r n e t p r o t o c o l ( AF_INET ) ∗/
12 u n s i g n e d s h o r t s i n _ p o r t ; /∗ A d d r e s s p o r t ( 1 6 b i t s ) ∗/
13 s t r u c t i n _ a d d r s i n _ a d d r ; /∗ I n t e r n e t a d d r e s s ( 3 2 b i t s ) ∗/
14 char sin_zero [ 8 ] ; /∗ n o t u s e d ∗/
15 }
16
17 /∗NOTE t h a t s o c k a d d r _ i n c a n be c a s t e d t o a s o c k a d d r ∗/

Listing 7 – How to use bind()


1 int sockid ;
2 s t r u c t sockaddr_in addrport ;
3 s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ;
4 a d d r p o r t . s i n _ f a m i l y = AF_INET ;
5 addrport . sin_port = htons (5100);
6 a d d r p o r t . s i n _ a d d r . s_addr = h t o n l (INADDR_ANY ) ;
7 i f ( b i n d ( s o c k i d , ( s t r u c t s o c k a d d r ∗ ) &a d d r p o r t , s i z e o f ( a d d r p o r t ) ) < 0)
8 { // r e p o r t e r r o r
9 }
Listing 8 – connect()
1 /∗ The c l i e n t e s t a b l i s h e s a c o n n e c t i o n w i t h t h e s e r v e r by calling
2 c o n n e c t ( ) ∗/
3 i n t s t a t u s = c o n n e c t ( s o c k i d , &f o r e i g n A d d r , a d d r l e n ) ;
4
5 // s o c k i d : i n t e g e r , s o c k e t t o be u s e d i n c o n n e c t i o n
6 // f o r e i g n A d d r : s t r u c t sockaddr : address of the p a s s i v e participant ( server ).
7 // a d d r l e n : i n t e g e r , s i z e o f ( name )
8 // s t a t u s : 0 i f s u c c e s s f u l c o n n e c t , −1 o t h e r w i s e
9
10 // c o n n e c t ( ) is blocking

Listing 9 – accept()
1 /∗ S e r v e r g e t s a s o c k e t f o r an i n c o m i n g c l i e n t c o n n e c t i o n by calling a c c e p t ( ) ∗/
2 i n t s o c k 1 = a c c e p t ( s o c k i d , &c l i e n t A d d r , &a d d r L e n ) ;
3
4 // s o c k 1 : integer , t h e new s o c k e t ( u s e d for d a t a−t r a n s f e r )
5
6 // s o c k i d : integer , the original socket ( being l i s t e n e d on )
7
8 // c l i e n t A d d r : s t r u c t sockaddr type , a d d r e s s of the active
9 // p a r t i c i p a n t ( f i l l e d i n upon r e t u r n ) .
10
11 // a d d r L e n : sizeof ( clientAddr ): value / r e s u l t parameter
12
13
14 /∗ a c c e p t ( ) i s b l o c k i n g ( w a i t s f o r c o n n e c t i o n b e f o r e b l o c k i n g )
15 I t d e q u e u e s t h e n e x t c o n n e c t i o n on t h e q u e u e f o r s o c k e t ( s o c k i d ) .
16 ∗/
Listing 10 – Exchanging data with stream socket
1 int c o u n t 1 = s e n d ( s o c k i d , msg , msgLen , flags );
2
3 int count2 = r ecv ( sockid , recvBuf , bufLen , flags );
4
5
6 /∗ C a l l s are blocking ( returns only after data is sent / received
7 )
8 ∗/

Listing 11 – Echo Server example using stream socket (client side


code)
1
2 #i n c l u d e < s t d i o . h>
3 #i n c l u d e <s y s / s o c k e t . h>
4 #i n c l u d e <s y s / t y p e s . h>
5 #i n c l u d e < s t d l i b . h>
6 #i n c l u d e < n e t i n e t / i n . h>
7 #i n c l u d e <a r p a / i n e t . h>
8 //#i n c l u d e " unp . h "
9 #i n c l u d e < s t r i n g . h>
10 #i n c l u d e < u n i s t d . h>
11 #d e f i n e SERV_PORT 6000
12 #d e f i n e SERV_HOST_ADDR " 1 2 7 . 0 . 0 . 1 " /∗ h o s t address for s e r v e r ∗/
13 #d e f i n e MAXLINE 512
14
15
16 i n t main ( i n t a r g c , c h a r c o n s t ∗ a r g v [ ] )
17 {
18 int sockid ;
19 c h a r c [ 1 0 0 ] , c_w [ 1 0 0 ] ;
20 i n t rc ,w;
21
22 s t r u c t sockaddr_in serv_addr ;
23 // pname=a r g v [ 0 ] ;
24
25 // s e r v e r a d d r e s s
26 b z e r o ( ( c h a r ∗ ) &s e r v _ a d d r , s i z e o f ( s e r v _ a d d r ) ) ;
27 s e r v _ a d d r . s i n _ f a m i l y = AF_INET ;
28 s e r v _ a d d r . s i n _ a d d r . s_addr = i n e t _ a d d r (SERV_HOST_ADDR ) ;
29 s e r v _ a d d r . s i n _ p o r t = h t o n s ( SERV_PORT ) ;
30
31
32 // o p e n i n g a TCP s o c k e t ( i n t e r n e t s t r e a m s o c k e t )
33 i f ( ( s o c k i d = s o c k e t ( AF_INET , SOCK_STREAM, 0 ) ) < 0 )
34 {
35 p e r r o r ( " cannot o p e i n s o c k e t \n" ) ;
36 exit (1);
37 }
38 printf (" sockid s u c c e s s \n" ) ;
39 // c o n n e c t t o s e r v e r
40 i f ( c o n n e c t ( s o c k i d , ( s t r u c t s o c k a d d r ∗)& s e r v _ a d d r , s i z e o f ( s e r v _ a d d r )) <0){
41 p e r r o r (" connect () e r r o r " ) ;
42 exit (1);
43 }
44 p r i n t f ( " c o n n e c t i o n s u c c e s s \n" ) ;
45 // str_cli ( stdin , sockid );
46 fgets (c ,100 , stdin ) ;
47 w=w r i t e ( s o c k i d , c , s i z e o f ( c ) ) ;
48 r c=r e a d ( s o c k i d , c_w , s i z e o f (c_w ) ) ;
49 f p u t s ( c_w , s t d o u t ) ;
50 close ( sockid );
51
52 }
Next Class

I More into Echo server and data structures used.


I Utility programs used in Echo server.
I Basics of Chat program.
References

Unix Network Programming by W. Richard Stevens


Thank You

Вам также может понравиться