Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Transmission Control Protocol (TCP). TCP: Overview RFCs: 793, 1122, 1323, 2018, 2581 r reliable, in-order byte steam: m no “message boundaries” r send.
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
תקשורת באינטרנט Tutorial 8. 2 n Socket programming u What is socket ? u Sockets architecture u Types of Sockets u The Socket system calls u Data Transfer.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
TCP/IP Sockets in C: Practical Guide for Programmers Michael J. Donahoo Kenneth L. Calvert.
Introduction to Project 1 Web Client and Server Jan 2006.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Yu-Chi Lai Lecture 3 Network Programming CS 640: Computer Networking.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Sirak Kaewjamnong Computer Network Systems
1 Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming.
Chapter 2 Applications and Layered Architectures Sockets.
The Pocket Guide to TCP/IP Sockets: C Version Michael J. Donahoo Kenneth L. Calvert.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Berkeley Sockets. Client-server model Sockets interface: Address structure, byte ordering, port no Socket primitives: socket, bind,listen… Example code.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
The Pocket Guide to TCP/IP Sockets: C Version
Network Programming CSC- 341
TCP/IP Sockets in C: Practical Guide for Programmers
Chapter4 Elementary TCP Socket
Socket Programming in C
Transport layer API: Socket Programming
Socket Programming in C
Network Programming CSC- 341
TCP/IP Sockets in C: Practical Guide for Programmers
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Sockets in C: Practical Guide for Programmers
Socket 程式設計.
TCP/IP Socket Programming in C
Yu-Chi Lai Lecture 3 Network Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
TCP/IP Sockets in C: Practical Guide for Programmers
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4

Computer Networks TCP/IP Sockets 2 IPv4 Socket Address Structure The Internet socket address structure is named sockaddr_in and is defined by including header. struct in_addr { in_addr_t s_addr /* 32-bit IP address */ }; /* network byte ordered */ struct sockaddr_in { uint8_t sin_len; /* length of structure (16) */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* 16-bit TCP or UDP port number */ /* network byte ordered */ struct in_addr sin_addr; /* 32-bit IPv4 address */ /* network byte ordered */ char sin_zero[8]; /* unused */ };

Computer Networks TCP/IP Sockets 3 Application 1 Socket socket interface user kernel Application 2 user kernel Underlying communication Protocols Communications network Socket socket interface Figure 2.16 Copyright ©2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks The Socket Interface

Computer Networks TCP/IP Sockets 4 Figure 2.17 Leon-Garcia & Widjaja: Communication Networks Copyright ©2000 The McGraw Hill Companies TCP Socket Calls

Computer Networks TCP/IP Sockets 5 socket() bind() sendto() close() socket() bind() recvfrom() sendto() close() blocks until server receives data from client data Server Client recvfrom() Figure 2.18 Leon-Garcia & Widjaja: Communication Networks Copyright ©2000 The McGraw Hill Companies Not needed UDP Socket Calls

Computer Networks TCP/IP Sockets 6 System Calls for Elementary TCP Sockets #include family: specifies the protocol family {AF_INET for TCP/IP} type: indicates communications semantics SOCK_STREAM stream socket TCP SOCK_DGRAM datagram socket UDP SOCK_RAW raw socket protocol: set to 0 except for raw sockets returns on success: socket descriptor {a small nonnegative integer} on error: -1 Example: if (( sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_sys (“socket call error”); socket Function socket int socket ( int family, int type, int protocol );

Computer Networks TCP/IP Sockets 7 sockfd: a socket descriptor returned by the socket function *servaddr: a pointer to a socket address structure addrlen: the size of the socket address structure The socket address structure must contain the IP address and the port number for the connection wanted. In TCP connect initiates a three-way handshake. connect returns only when the connection is established or when an error occurs. returns on success: 0 on error: -1 Example: if ( connect (sd, (struct sockaddr *) &servaddr, sizeof (servaddr)) != 0) err_sys(“connect call error”); connect Function int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

Computer Networks TCP/IP Sockets 8 Figure 2.17 Leon-Garcia & Widjaja: Communication Networks Copyright ©2000 The McGraw Hill Companies TCP Socket Calls

Computer Networks TCP/IP Sockets 9 bind assigns a local protocol address to a socket. protocol address: a 32 bit IPv4 address and a 16 bit TCP or UDP port number. sockfd: a socket descriptor returned by the socket function. *myaddr: a pointer to a protocol-specific address. addrlen: the size of the socket address structure. Servers bind their “well-known port” when they start. returns on success: 0 on error: -1 Example: if (bind (sd, (struct sockaddr *) &servaddr, sizeof (servaddr)) != 0) errsys (“bind call error”); bind Function int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);

Computer Networks TCP/IP Sockets 10 listen is called only by a TCP server and performs two actions: 1.Converts an unconnected socket (sockfd) into a passive socket. 2.Specifies the maximum number of connections (backlog) that the kernel should queue for this socket. listen is normally called before the accept function. returns on success: 0 on error: -1 Example: if (listen (sd, 2) != 0) errsys (“listen call error”); listen Function int listen (int sockfd, int backlog);

Computer Networks TCP/IP Sockets 11 accept is called by the TCP server to return the next completed connection from the front of the completed connection queue. sockfd: This is the same socket descriptor as in listen call. *cliaddr: used to return the protocol address of the connected peer process (i.e., the client process). *addrlen: {this is a value-result argument} before the accept call: We set the integer value pointed to by *addrlen to the size of the socket address structure pointed to by *cliaddr; on return from the accept call: This integer value contains the actual number of bytes stored in the socket address structure. returns on success: a new socket descriptor on error: -1 accept Function int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

Computer Networks TCP/IP Sockets 12 For accept the first argument sockfd is the listening socket and the returned value is the connected socket. The server will have one connected socket for each client connection accepted. When the server is finished with a client, the connected socket must be closed. Example: sfd = accept (sd, NULL, NULL); if (sfd == -1) err_sys (“accept error”); accept Function (cont.) int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); connected socket

Computer Networks TCP/IP Sockets 13 close marks the socket as closed and returns to the process immediately. sockfd: This socket descriptor is no longer useable. Note – TCP will try to send any data already queued to the other end before the normal connection termination sequence. Returns on success: 0 on error: -1 Example: close (sfd); close Function int close (int sockfd);

Computer Networks TCP/IP Sockets 14 #include /* for printf() and fprintf() */ #include /* for socket(), bind(), and connect() */ #include /* for sockaddr_in and inet_ntoa() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #define MAXPENDING 5 /* Maximum outstanding connection requests */ void DieWithError(char *errorMessage); /* Error handling function */ void HandleTCPClient(int clntSocket); /* TCP client handling function */ TCP Echo Server D&C

Computer Networks TCP/IP Sockets 15 int main(int argc, char *argv[]) { int servSock; /*Socket descriptor for server */ int clntSock; /* Socket descriptor for client */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned short echoServPort; /* Server port */ unsigned int clntLen; /* Length of client address data structure */ if (argc != 2) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ /* Create socket for incoming connections */ if ((servSock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); D&C

Computer Networks TCP/IP Sockets 16 /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ echoServAddr.sin_port = htons(echoServPort); /* Local port */ /* Bind to the local address */ if ( bind (servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed"); /* Mark the socket so it will listen for incoming connections */ if ( listen (servSock, MAXPENDING) < 0) DieWithError("listen() failed"); TCP Echo Server D&C

Computer Networks TCP/IP Sockets 17 for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ clntLen = sizeof(echoClntAddr); /* Wait for a client to connect */ if ((clntSock = accept (servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) DieWithError("accept() failed"); /* clntSock is connected to a client! */ printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); HandleTCPClient(clntSock); } /* NOT REACHED */ } TCP Echo Server D&C

Computer Networks TCP/IP Sockets 18 #include /* for printf() and fprintf() */ #include /* for socket(), connect(), send(), and recv() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() and exit() */ #include /* for memset() */ #include /* for close() */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void DieWithError(char *errorMessage); /* Error handling function */ TCP Echo Client D&C

Computer Networks TCP/IP Sockets 19 int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ unsigned short echoServPort; /* Echo server port */ char *servIP; /* Server IP address (dotted quad) */ char *echoString; /* String to send to echo server */ char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */ unsigned int echoStringLen; /* Length of string to echo */ int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes read */ if ((argc 4)) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s [ ]\n", argv[0]); exit(1); } D&C

Computer Networks TCP/IP Sockets 20 servIP = argv[1]; /* First arg: server IP address (dotted quad) */ echoString = argv[2]; /* Second arg: string to echo */ if (argc == 4) echoServPort = atoi(argv[3]); /* Use given port, if any */ else echoServPort = 7; /* 7 is the well-known port for the echo service */ /* Create a reliable, stream socket using TCP */ if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ D&C

Computer Networks TCP/IP Sockets 21 /* Establish the connection to the echo server */ if ( connect (sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("connect() failed"); echoStringLen = strlen(echoString); /* Determine input length */ /* Send the string to the server */ if (send (sock, echoString, echoStringLen, 0) != echoStringLen) DieWithError("send() sent a different number of bytes than expected"); /* Receive the same string back from the server */ totalBytesRcvd = 0; printf("Received: "); /* Setup to print the echoed string */ TCP Echo Client D&C

Computer Networks TCP/IP Sockets 22 while (totalBytesRcvd < echoStringLen) { /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */ if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed or connection closed prematurely"); totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */ echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */ printf("%s", echoBuffer); /* Print the echo buffer */ } printf("\n"); /* Print a final linefeed */ close (sock); exit(0); } TCP Echo Client D&C