Download presentation
Presentation is loading. Please wait.
Published byJack Spencer Modified over 9 years ago
1
ECE 4110 – Internetwork Programming Client-Server Model
2
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 2 References R. Stevens, “UNIX Network Programming,” Volume 1, 2 nd Edition R. Stevens, “TCP/IP Illustrated,” Volume 1 R. Stevens, “Advanced Programming in the UNIX Environment”
3
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 3 Introduction Example: The server process is the telnet daemon. The client processes are telnet clients. Typically, the server process runs with root permissions, and each client process runs with the permissions of the corresponding user. TCP/UDP and IP protocols are part of the kernel. Server Client #1 Client #2 Client #3
4
4 TCP Client-Server Model socket() connect() send() recv() close() socket() bind() listen() accept() recv() send() recv() close() process request blocks until connection from client connection establishment data (request) data (reply) end-of-file notification TCP Server TCP Client
5
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 5 Examples Under the directory /Stevens/unpv12e, libraries and source files written by R. Stevens are installed. Stevens defines wrapper functions for connect(), listen(), etc., to perform function call with error checking. The wrapper functions have the initial in upper-case. Under intro directory, you will find daytimetcpsrv.c and daytimetcpcli.c. Examine these two files. This example is similar to what you did in Lab 2.
6
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 6 TCP Socket Pair Each TCP connection is associated with a 4- tuple that defines the two ends of the connection: LocalIPAddress LocalTCPPort ForeignIPAddress ForeignTCPPort A socket pair uniquely identities every TCP connection on an internet. Same concept extends to UDP.
7
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 7 TCP Socket Programming Make sure the server is running and bound to the server port before client attempts to connect. Refer to manual pages and Unix Network Programming book for a thorough discussion of the following functions. We will discuss only the features relevant to us for the moment.
8
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 8 TCP Socket Programming (cont’d) socket() function: int socket(int domain, int type, int protocol); Returns a socket descriptor, sockfd, which is non-negative unless there is an error. domain: Specifies the protocol family. Possible values: AF_INET: IPv4 protocols (for TCP and UDP) type: Type of socket. Possible values: SOCK_STREAM: Stream socket (for TCP) SOCK_DGRAM: Datagram socket (for UDP) protocol: Particular protocol to be used with socket. Possible values: 0: for TCP and UDP
9
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 9 TCP Socket Programming (cont’d) bind() function: int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); Returns 0 for success, -1 for error. sockfd: Socket descriptor returned by socket(). my_addr: Structure that contains information about local protocol- specific address. IPv4: my_addr is of type struct sockaddr_in rather than sockaddr. Therefore, a casting is required for my_addr. Set my_addr as follows: my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port_no); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); addrlen: Size of my_addr If port number is zero, kernel selects an ephemeral port.
10
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 10 TCP Socket Programming (cont’d) listen() function: int listen(int sockfd, int backlog); Converts unconnected socked into a passive socket, i.e., kernel should accept incoming connection requests. Returns 0 for success, -1 for error. sockfd: Socket descriptor returned by socket(). backlog: Maximum length the queue of pending connections may grow to. Possible value: 1
11
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 11 TCP Socket Programming (cont’d) accept() function: int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); Creates a new connected request when a request arrives at the listening socket sockfd. Returns the file descriptor for the new socket if successful, -1 for error. sockfd: Socket descriptor returned by socket(). addr: Address structure for the client. The exact structure of addr is determined by the socket’s family. addrlen: Length of the addr structure.
12
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 12 TCP Socket Programming (cont’d) connect() function: int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); Returns 0 for success, -1 for error. sockfd: Socket descriptor returned by socket(). serv_addr: Structure that contains destination IP address and port number. Set serv_addr as follows: servaddr.sin_family = AF_INET; servaddr.sin_port = htons(port_no); inet_pton(AF_INET, server_ip_address, &servaddr.sin_addr); addrlen: Size of sockaddr
13
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 13 TCP Socket Programming (cont’d) close() function: int close(int sockfd); Closes sockfd and releases any locks. Do not access sockfd after close(). Returns 0 for success, -1 for error.
14
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 14 TCP Socket Programming (cont’d) recv() function: int recv(int sockfd, void *buf, size_t len, int flags); Returns the number of bytes received if successful, -1 for error. sockfd: Socket descriptor returned by socket(). buf: Buffer into which received data is written. len: Maximum number of bytes to be written to buf. flags: Multiple options OR’ed. Usually 0. Use MSG_DONTWAIT if you want non-blocking I/O. read() can also be used instead of recv().
15
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 15 TCP Socket Programming (cont’d) send() function: int send(int sockfd, const void *msg, size_t len, int flags); Returns the number of bytes sent if successful, -1 for error. sockfd: Socket descriptor returned by socket(). msg: Message that will be sent. len: Number of bytes to be sent. flags: Multiple options OR’ed. Usually 0. Use MSG_DONTWAIT if you want non-blocking I/O. write() can also be used instead of send().
16
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 16 TCP Socket Programming (cont’d) setsockopt(), getsockopt() functions: int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); Returns 0 for success, -1 for error. sockfd: Socket descriptor returned by socket(). level: Level at which the option resides. Possible value: IPPROTO_TCP optname: Option to be set. Possible value: TCP_NODELAY optval, optlen: Used to access option.
17
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 17 UDP Socket Programming UDP is connectionless. So, On the server side, listen() and accept() are not used. On the client side, connect() is not used typically. Instead of send() and recv(), use sendto() and recvfrom().
18
18 UDP Client-Server Model connect() sendto() recvfrom() close() socket() bind() recvfrom() sendto() process request blocks until datagram receiver from a client Only for connected UDP sockets data (reply) UDP Server UDP Client
19
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 19 UDP Socket Programming (cont’d) recvfrom() function: int recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); Same as recv(). In addition, from: Address structure for the source of the message. fromlen: Size of from. If from is NULL, same as recv().
20
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 20 UDP Socket Programming (cont’d) sendto() function: int send(int sockfd, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); Same as send(). In addition, to: Address structure for the destination. tolen: Size of to.
21
Spring 2003 Tuna Tugcu ECE 4110 – Internetwork Programming 21 UDP Socket Programming (cont’d) UDP client may optionally use connect() to tell the kernel IP address and port number of destination. This is not a real connect operation. If connected UDP socket is being used, sendto() and recvfrom() cannot be used. Use send() and recv().
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.