Communication Networks Recitation 1 Netcomm 2006 Recitation 1
- Slides: 22
Communication Networks Recitation 1 Netcomm 2006 - Recitation 1: Sockets 1
Administrative • David Raz – Schreiber M 21, (640)6455 – Email: davidraz@post. tau. ac. il – website: http: //www. cs. tau. ac. il/~davidraz/courses/c omnet 06/ – Grader: Hadas Zur zurhadas@post. tau. ac. il Netcomm 2006 - Recitation 1: Sockets 2
TCP/IP Socket Programming Netcomm 2006 - Recitation 1: Sockets 3
What is a socket? • An interface between application and the network • The application can send/receive data to/from the network -- communicate Application Network API Protocol A Protocol B Protocol C Netcomm 2006 - Recitation 1: Sockets 4
A Socket-eye view of the Internet medellin. cs. columbia. edu (128. 59. 21. 14) newworld. cs. umass. edu (128. 119. 245. 93) cluster. cs. columbia. edu (128. 59. 21. 14, 128. 59. 16. 7, 128. 59. 16. 5, 128. 59. 16. 4) • Each host machine has an IP address Netcomm 2006 - Recitation 1: Sockets 5
Ports • Each host has 65, 536 ports • Some ports are reserved for specific apps Port 0 Port 1 Port 65535 – 20, 21: FTP – 23: Telnet – 80: HTTP Netcomm 2006 - Recitation 1: Sockets 6
Address Pair • An address is an IP+port • A socket provides an interface to an IP: port pair Local IP: 111. 22. 3. 4 Local Port: 2249 Remote IP: 123. 45. 6. 78 Remote Port: 3726 Netcomm 2006 - Recitation 1: Sockets 7
Functions needed: • Specify local and remote communication endpoints • Initiate a connection • Send and receive data • Terminate a connection Netcomm 2006 - Recitation 1: Sockets 8
Socket Creation in C: socket() • int s = socket(domain, type, protocol); – s: socket descriptor (an integer, like a file-handle) – domain: integer, communication domain • e. g. , PF_INET (IPv 4 protocol) – typically used – type: communication type • SOCK_STREAM: reliable, 2 -way, connection-based service • SOCK_DGRAM: unreliable, connectionless, – protocol: specifies protocol (see file /etc/protocols for a list of options) - usually set to 0 Netcomm 2006 - Recitation 1: Sockets 9
Two essential types of sockets • SOCK_STREAM – a. k. a. TCP – reliable delivery – in-order guaranteed – connection-oriented – bidirectional App 3 2 1 • SOCK_DGRAM – a. k. a. UDP – unreliable delivery – no order guarantees – no notion of “connection” – can send or receive D 1 App socket Dest. 3 2 1 D 2 socket D 3 Netcomm 2006 - Recitation 1: Sockets 11
The bind() function • associates and (can exclusively) reserves a port for use by the socket • Done by the server • 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 Netcomm 2006 - Recitation 1: Sockets 12
The struct sockaddr • The Internet-specific: • The generic: struct sockaddr { u_short sa_family; char sa_data[14]; }; – sa_family • specifies which address family is being used • determines how the remaining 14 bytes are used 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 Netcomm 2006 - Recitation 1: Sockets 13
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 128 Big-Endian machine 119 40 12 Little-Endian machine 128 Netcomm 2006 - Recitation 1: Sockets 119 12. 40. 119. 128 40 12 14
Network Byte Order • All values stored in a sockaddr_in must be in network byte order. • Whenever the source of the address isn’t the network, use htonl and htons Netcomm 2006 - Recitation 1: Sockets 15
Example int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ } struct sockaddr_in myaddr; myaddr. sin_family = AF_INET; myaddr. sin_port = htons( 80 ); myaddr. sin_addr = htonl( INADDR_ANY ); bind(sock, &myaddr, sizeof(myaddr)); Netcomm 2006 - Recitation 1: Sockets 16
Connection Setup (SOCK_STREAM) • A 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” – both can send & receive data – either can terminate the connection Netcomm 2006 - Recitation 1: Sockets 17
Connection setup cont’d • Passive participant – step 1: listen (for incoming requests) – step 3: accept (a request) – step 4: data transfer • The accepted connection is on a new socket • The old socket continues to listen • Active participant – step 2: request & establish connection – step 4: data transfer Passive Participant a-sock-1 l-sock a-sock-2 socket Active 1 Active 2 Netcomm 2006 - Recitation 1: Sockets 18
Connection est. : listen() & accept() • Called by passive participant • 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, &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 Netcomm 2006 - Recitation 1: Sockets 19
Connection est. : connect() • Called by active participant • 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) Netcomm 2006 - Recitation 1: Sockets 20
Server example int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ } struct sockaddr_in myaddr; myaddr. sin_family = AF_INET; myaddr. sin_port = htons( 80 ); myaddr. sin_addr = htonl( INADDR_ANY ); bind(sock, &myaddr, sizeof(myaddr)); Netcomm 2006 - Recitation 1: Sockets 21
Server example listen(sock, 5); int new_sock; struct sockaddr_in their_addr; sin_size = sizeof(struct sockaddr_in); new_sock = accept(sock, (struct sockaddr *)&their_addr, &sin_size); Netcomm 2006 - Recitation 1: Sockets 22
Client example int sock; sock = socket(PF_INET, SOCK_STREAM, 0); if (sock<0) { /* ERROR */ } struct sockaddr_in dest_addr; dest_addr. sin_family = AF_INET; dest_addr. sin_port = htons( 80 ); dest_addr. sin_addr = inet_addr(” 128. 2. 5. 10“); connect)sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr; (( Netcomm 2006 - Recitation 1: Sockets 23
- Differentiate between virtual circuit and datagram network
- Backbone networks in computer networks
- Active recitation
- Quood 8
- Impromptu speaking business
- Poem recitation objectives
- Tips for reciting poetry
- What is meant by etiquette of recitation of the holy quran
- Xvvvxv
- Example of process-oriented assessment in math
- 100belaud
- Iec 61850 communication networks and systems in substations
- Game theory in wireless and communication networks
- Introduction to communication networks
- Networks of communication and exchange
- A communication processor that connects dissimilar networks
- Industrial communication networks
- Communication networks
- Parallel serial port
- What is oral communication and written communication
- Parallel and serial communication difference
- Types of nonverbal communication chronemics
- Definition of oral communication