Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 218 times |
Download: | 2 times |
1Netcomm 2006 - Recitation 1: Sockets
Communication NetworksCommunication Networks
Recitation 1Recitation 1
2Netcomm 2006 - Recitation 1: Sockets
AdministrativeAdministrative
• David RazDavid Raz– Schreiber M21, (640)6455 Schreiber M21, (640)6455 – Email: Email: [email protected]@post.tau.ac.il– website: website:
http://www.cs.tau.ac.il/~davidraz/courses/chttp://www.cs.tau.ac.il/~davidraz/courses/comnet06/omnet06/
– Grader: Grader: Hadas Zur [email protected]
3Netcomm 2006 - Recitation 1: Sockets
TCP/IP Socket ProgrammingTCP/IP Socket Programming
4Netcomm 2006 - Recitation 1: Sockets
What is a socket?What is a socket?
• An interface between application and An interface between application and the networkthe network
• The application can send/receive data The application can send/receive data to/from the network -- communicateto/from the network -- communicate
ApplicationApplication
Network APINetwork API
Protocol AProtocol A Protocol BProtocol B Protocol CProtocol C
5Netcomm 2006 - Recitation 1: Sockets
A Socket-eye view of the InternetA 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)
6Netcomm 2006 - Recitation 1: Sockets
PortsPortsPort 0
Port 1
Port 65535
• Each host has Each host has 65,536 ports65,536 ports
• Some ports are Some ports are reserved for specific reserved for specific appsapps– 20,21: FTP20,21: FTP– 23: Telnet23: Telnet– 80: HTTP80: HTTP
7Netcomm 2006 - Recitation 1: Sockets
Address PairAddress Pair
• An address is an IP+portAn address is an IP+port
• A socket provides an interface to an A socket provides an interface to an IP:port pairIP:port pair
Remote IP: 123.45.6.78Remote IP: 123.45.6.78Remote Port: 3726Remote Port: 3726
Remote IP: 123.45.6.78Remote IP: 123.45.6.78Remote Port: 3726Remote Port: 3726
Local IP: 111.22.3.4Local IP: 111.22.3.4Local Port: 2249Local Port: 2249
Local IP: 111.22.3.4Local IP: 111.22.3.4Local Port: 2249Local Port: 2249
8Netcomm 2006 - Recitation 1: Sockets
Functions needed:Functions needed:
• Specify local and remote Specify local and remote communication endpointscommunication endpoints
• Initiate a connectionInitiate a connection
• Send and receive dataSend and receive data
• Terminate a connectionTerminate a connection
9Netcomm 2006 - Recitation 1: Sockets
Socket Creation in C: socket()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)s: socket descriptor (an integer, like a file-handle)– domain: integer, communication domaindomain: integer, communication domain
• e.g., e.g., PF_INETPF_INET (IPv4 protocol) – typically used (IPv4 protocol) – typically used
– typetype: communication type: communication type• SOCK_STREAMSOCK_STREAM: reliable, 2-way, connection-based : reliable, 2-way, connection-based
serviceservice• SOCK_DGRAMSOCK_DGRAM: unreliable, connectionless,: unreliable, connectionless,
– protocolprotocol: specifies protocol (see file /etc/protocols : specifies protocol (see file /etc/protocols for a list of options) - usually set to 0for a list of options) - usually set to 0
11Netcomm 2006 - Recitation 1: Sockets
Two essential types of socketsTwo essential types of sockets• SOCK_STREAMSOCK_STREAM
– a.k.a. TCPa.k.a. TCP– reliable deliveryreliable delivery– in-order guaranteedin-order guaranteed– connection-orientedconnection-oriented– bidirectionalbidirectional
• SOCK_DGRAMSOCK_DGRAM– a.k.a. UDPa.k.a. UDP– unreliable deliveryunreliable delivery– no order guaranteesno order guarantees– no notion of “connection” no notion of “connection” – can send or receivecan send or receive
App
socket3 2 1
Dest.
App
socket3 2 1
D1
D3
D2
12Netcomm 2006 - Recitation 1: Sockets
The bind() functionThe bind() function
• associates and (can exclusively) reserves a associates and (can exclusively) reserves a port for use by the socketport for use by the socket
• Done by the serverDone by the server• int status = bind(sockid, &addrport, size);int status = bind(sockid, &addrport, size);
– statusstatus: error status, = -1 if bind failed: error status, = -1 if bind failed– sockidsockid: integer, socket descriptor: integer, socket descriptor– addrportaddrport: struct sockaddr, the (IP) address and : struct sockaddr, the (IP) address and
port of the machine. (address usually set to port of the machine. (address usually set to INADDR_ANY – chooses a local address)INADDR_ANY – chooses a local address)
– sizesize: the size (in bytes) of the addrport structure: the size (in bytes) of the addrport structure
13Netcomm 2006 - Recitation 1: Sockets
The struct sockaddrThe struct sockaddr• The generic:The generic:
struct sockaddr {struct sockaddr {u_short sa_family;u_short sa_family;char sa_data[14];char sa_data[14];
};};
– sa_family sa_family • specifies which specifies which
address family is address family is being usedbeing used
• determines how the determines how the remaining 14 bytes remaining 14 bytes are usedare used
• The Internet-specific:The Internet-specific:struct sockaddr_in {struct sockaddr_in {
short sin_family;short sin_family;u_short sin_port;u_short sin_port;struct in_addr sin_addr;struct in_addr sin_addr;char sin_zero[8];char sin_zero[8];
};};– sin_familysin_family = AF_INET = AF_INET– sin_portsin_port: port # (0-65535): port # (0-65535)– sin_addrsin_addr: IP-address: IP-address– sin_zerosin_zero: unused: unused
14Netcomm 2006 - Recitation 1: Sockets
Address and port byte-orderingAddress and port byte-ordering Problem:
different machines / OS’s use different word orderings• little-endian: lower bytes first• big-endian: higher bytes first
these machines may communicate with one another over the network
128.119.40.12
128
119
40 12
12.40.119.128
128
119
40 12
Big-Endianmachine Little-Endian
machine
15Netcomm 2006 - Recitation 1: Sockets
Network Byte OrderNetwork Byte Order
• All values stored in a All values stored in a sockaddr_insockaddr_in must be in network byte order.must be in network byte order.
• Whenever the source of the address Whenever the source of the address isn’t the network, use htonl and htonsisn’t the network, use htonl and htons
16Netcomm 2006 - Recitation 1: Sockets
ExampleExample
int sock;int sock;sock = socket(PF_INET, SOCK_STREAM, 0);sock = socket(PF_INET, SOCK_STREAM, 0);if (sock<0) { /* ERROR */ }if (sock<0) { /* ERROR */ }
struct sockaddr_in myaddr;struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;myaddr.sin_family = AF_INET;myaddr.sin_port = htons( 80 );myaddr.sin_port = htons( 80 );myaddr.sin_addr = htonl( INADDR_ANY );myaddr.sin_addr = htonl( INADDR_ANY );
bind(sock, &myaddr, sizeof(myaddr));bind(sock, &myaddr, sizeof(myaddr));
17Netcomm 2006 - Recitation 1: Sockets
Connection Setup Connection Setup (SOCK_STREAM)(SOCK_STREAM)
• A connection occurs between two kinds of A connection occurs between two kinds of participantsparticipants– passive: waits for an active participant to request passive: waits for an active participant to request
connectionconnection– active: initiates connection request to passive sideactive: initiates connection request to passive side
• Once connection is established, passive and Once connection is established, passive and active participants are “similar”active participants are “similar”– both can send & receive databoth can send & receive data– either can terminate the connectioneither can terminate the connection
18Netcomm 2006 - Recitation 1: Sockets
Connection setup cont’dConnection setup cont’d• Passive participantPassive participant
– step 1: step 1: listenlisten (for (for incoming requests)incoming requests)
– step 3: step 3: acceptaccept (a (a request)request)
– step 4: data transferstep 4: data transfer
• The accepted The accepted connection is on a connection is on a new socketnew socket
• The old socket The old socket continues to listencontinues to listen
• Active participantActive participant– step 2: request & step 2: request &
establish connectionestablish connection– step 4: data transferstep 4: data transfer
Passive Participant
l-socka-sock-1 a-sock-2
Active 1
socket
Active 2
socket
19Netcomm 2006 - Recitation 1: Sockets
Connection est.: listen() & Connection est.: listen() & accept()accept()
• Called by passive participantCalled by passive participant• int status = listen(sock, queuelen);int status = listen(sock, queuelen);
– statusstatus: 0 if listening, -1 if error : 0 if listening, -1 if error – socksock: integer, socket descriptor: integer, socket descriptor– queuelenqueuelen: integer, # of active participants that can : integer, # of active participants that can
“wait” for a connection“wait” for a connection
• int s = accept(sock, &name, &namelen);int s = accept(sock, &name, &namelen);– ss: integer, the new socket (used for data-transfer): integer, the new socket (used for data-transfer)– socksock: integer, the orig. socket (being listened on): integer, the orig. socket (being listened on)– namename: struct sockaddr, address of the active : struct sockaddr, address of the active
participantparticipant– namelennamelen: sizeof(name): value/result parameter: sizeof(name): value/result parameter
20Netcomm 2006 - Recitation 1: Sockets
Connection est.: connect()Connection est.: connect()
• Called by active participantCalled by active participant
• int status = connect(sock, &name, namelen);int status = connect(sock, &name, namelen);– statusstatus: 0 if successful connect, -1 otherwise: 0 if successful connect, -1 otherwise– socksock: integer, socket to be used in connection: integer, socket to be used in connection– namename: struct sockaddr: address of passive : struct sockaddr: address of passive
participantparticipant– namelennamelen: integer, sizeof(name): integer, sizeof(name)
21Netcomm 2006 - Recitation 1: Sockets
Server exampleServer example
int sock;int sock;sock = socket(PF_INET, SOCK_STREAM, 0);sock = socket(PF_INET, SOCK_STREAM, 0);if (sock<0) { /* ERROR */ }if (sock<0) { /* ERROR */ }
struct sockaddr_in myaddr;struct sockaddr_in myaddr;
myaddr.sin_family = AF_INET;myaddr.sin_family = AF_INET;myaddr.sin_port = htons( 80 );myaddr.sin_port = htons( 80 );myaddr.sin_addr = htonl( INADDR_ANY );myaddr.sin_addr = htonl( INADDR_ANY );
bind(sock, &myaddr, sizeof(myaddr));bind(sock, &myaddr, sizeof(myaddr));
22Netcomm 2006 - Recitation 1: Sockets
Server exampleServer example
listen(sock, 5);listen(sock, 5);int new_sock;int new_sock;struct sockaddr_in their_addr;struct sockaddr_in their_addr;sin_size = sizeof(struct sockaddr_in);sin_size = sizeof(struct sockaddr_in);new_sock = accept(sock, (struct sockaddr new_sock = accept(sock, (struct sockaddr
*)&their_addr, &sin_size);*)&their_addr, &sin_size);
23Netcomm 2006 - Recitation 1: Sockets
Client exampleClient example
int sock;int sock;sock = socket(PF_INET, SOCK_STREAM, 0);sock = socket(PF_INET, SOCK_STREAM, 0);if (sock<0) { /* ERROR */ }if (sock<0) { /* ERROR */ }
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;dest_addr.sin_family = AF_INET;dest_addr.sin_port = htons( 80 );dest_addr.sin_port = htons( 80 );dest_addr.sin_addr = dest_addr.sin_addr = inet_addrinet_addr((“128.2.5.10”“128.2.5.10”));;
connectconnect((sock, sock, ((struct sockaddr struct sockaddr *)*)&dest_addr, &dest_addr, sizeofsizeof((struct sockaddrstruct sockaddr))));;