Download presentation
Presentation is loading. Please wait.
1
1 Netcomm 2005 - Sockets Communication Networks Recitation 1
2
2 Netcomm 2005 - Sockets Administrative Nir AndelmanNir Andelman –Open Space, (640)7438 –Email: andelman@cs.tau.ac.il andelman@cs.tau.ac.il –website: http://www.cs.tau.ac.il/~andelmni/courses/c omnet05/netcom.html http://www.cs.tau.ac.il/~andelmni/courses/c omnet05/netcom.html http://www.cs.tau.ac.il/~andelmni/courses/c omnet05/netcom.html –Grader: TBD
3
3 Netcomm 2005 - Sockets TCP/IP Socket Programming
4
4 Netcomm 2005 - Sockets What is a socket? An interface between application and the networkAn interface between application and the network The application can send/receive data to/from the network -- communicateThe application can send/receive data to/from the network -- communicate Application Network API Protocol A Protocol B Protocol C
5
5 Netcomm 2005 - Sockets A Socket-eye view of the Internet Each host machine has an IP addressEach host machine has an IP address medellin.cs.columbia.edu (128.59.21.14) cluster.cs.columbia.edu (128.59.21.14, 128.59.16.7, 128.59.16.5, 128.59.16.4) newworld.cs.umass.edu (128.119.245.93)
6
6 Netcomm 2005 - Sockets Ports Port 0 Port 1 Port 65535 Each host has 65,536 portsEach host has 65,536 ports Some ports are reserved for specific appsSome ports are reserved for specific apps –20,21: FTP –23: Telnet –80: HTTP r A socket provides an interface to address IP:port pair
7
7 Netcomm 2005 - Sockets Functions needed: Specify local and remote communication endpointsSpecify local and remote communication endpoints Initiate a connectionInitiate a connection Send and receive dataSend and receive data Terminate a connectionTerminate a connection
8
8 Netcomm 2005 - Sockets Socket Creation in C: socket() int s = socket(domain, type, protocol);int s = socket(domain, type, protocol); –s: socket descriptor (an integer (like a file-handle) –domain: integer, communication domain e.g., PF_INET (IPv4 protocol) – typically usede.g., PF_INET (IPv4 protocol) – typically used –type: communication type SOCK_STREAM: reliable, 2-way, connection-based serviceSOCK_STREAM: reliable, 2-way, connection-based service SOCK_DGRAM: unreliable, connectionless,SOCK_DGRAM: unreliable, connectionless, –protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0
9
9 Netcomm 2005 - Sockets Socket Descriptor Data Structure Descriptor Table 0 1 2 3 4 Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726 Family: PF_INET Service: SOCK_STREAM Local IP: 111.22.3.4 Remote IP: 123.45.6.78 Local Port: 2249 Remote Port: 3726
10
10 Netcomm 2005 - Sockets Two essential types of sockets SOCK_STREAMSOCK_STREAM –a.k.a. TCP –reliable delivery –in-order guaranteed –connection-oriented –bidirectional SOCK_DGRAMSOCK_DGRAM –a.k.a. UDP –unreliable delivery –no order guarantees –no notion of “connection” –can send or receive App socket 3 2 1 Dest. App socket 3 2 1 D1 D3 D2
11
11 Netcomm 2005 - Sockets The bind() function associates and (can exclusively) reserves a port for use by the socketassociates and (can exclusively) reserves a port for use by the socket int status = bind(sockid, &addrport, size);int status = bind(sockid, &addrport, size); –status: error status, = -1 if bind failed –sockid: integer, socket descriptor –addrport: struct sockaddr, the (IP) address and port of the machine (address usually set to INADDR_ANY – chooses a local address) –size: the size (in bytes) of the addrport structure
12
12 Netcomm 2005 - Sockets The struct sockaddr The generic:The generic: struct sockaddr { u_short sa_family; char sa_data[14]; }; –sa_family specifies which address family is being usedspecifies which address family is being used determines how the remaining 14 bytes are useddetermines how the remaining 14 bytes are used The Internet-specific:The Internet-specific: struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; –sin_family = AF_INET –sin_port: port # (0- 65535) –sin_addr: IP-address –sin_zero: unused
13
13 Netcomm 2005 - Sockets Address and port byte-ordering r Problem: m different machines / OS’s use different word orderings little-endian: lower bytes first big-endian: higher bytes first m these machines may communicate with one another over the network 128.119.40.12 1281194012 12.40.119.128 1281194012 Big-Endian machine Little-Endian machine
14
14 Netcomm 2005 - Sockets Network Byte Order All values stored in a sockaddr_in must be in network byte order.All values stored in a sockaddr_in must be in network byte order. –sin_port a TCP/IP port number. –sin_addr an IP address.
15
15 Netcomm 2005 - Sockets Connection Setup (SOCK_STREAM) A connection occurs between two kinds of participantsA connection occurs between two kinds of participants –passive: waits for an active participant to request connection –active: initiates connection request to passive side Once connection is established, passive and active participants are “similar”Once connection is established, passive and active participants are “similar” –both can send & receive data –either can terminate the connection
16
16 Netcomm 2005 - Sockets Connection setup cont’d Passive participantPassive participant –step 1: listen (for incoming requests) –step 3: accept (a request) –step 4: data transfer The accepted connection is on a new socketThe accepted connection is on a new socket The old socket continues to listenThe old socket continues to listen Active participantActive participant –step 2: request & establish connection –step 4: data transfer Passive Participant l-socka-sock-1a-sock-2 Active 1 socket Active 2 socket
17
17 Netcomm 2005 - Sockets Connection est.: listen() & accept() Called by passive participantCalled by passive participant int status = listen(sock, queuelen);int status = listen(sock, queuelen); –status: 0 if listening, -1 if error –sock: integer, socket descriptor –queuelen: integer, # of active participants that can “wait” for a connection int s = accept(sock, &name, &namelen);int s = accept(sock, &name, &namelen); –s: integer, the new socket (used for data-transfer) –sock: integer, the orig. socket (being listened on) –name: struct sockaddr, address of the active participant –namelen: sizeof(name): value/result parameter
18
18 Netcomm 2005 - Sockets Connection est.: connect() Called by active participantCalled by active participant int status = connect(sock, &name, namelen);int status = connect(sock, &name, namelen); –status: 0 if successful connect, -1 otherwise –sock: integer, socket to be used in connection –name: struct sockaddr: address of passive participant –namelen: integer, sizeof(name)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.