21
ECE 4110 – Internetwork Programming Client-Server Model

ECE 4110 – Internetwork Programming Client-Server Model

Embed Size (px)

Citation preview

Page 1: ECE 4110 – Internetwork Programming Client-Server Model

ECE 4110 – Internetwork Programming

Client-Server Model

Page 2: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna Tugcu ECE 4110 – Internetwork Programming 2

References

R. Stevens, “UNIX Network Programming,” Volume 1, 2nd Edition

R. Stevens, “TCP/IP Illustrated,” Volume 1

R. Stevens, “Advanced Programming in the UNIX Environment”

Page 3: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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

Page 4: ECE 4110 – Internetwork Programming Client-Server Model

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

Page 5: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 6: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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: Local IP Address Local TCP Port Foreign IP Address Foreign TCP Port

A socket pair uniquely identities every TCP connection on an internet. Same concept extends to UDP.

Page 7: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 8: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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

Page 9: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 10: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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

Page 11: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 12: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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

Page 13: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 14: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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().

Page 15: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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().

Page 16: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 17: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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().

Page 18: ECE 4110 – Internetwork Programming Client-Server Model

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

Page 19: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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().

Page 20: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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.

Page 21: ECE 4110 – Internetwork Programming Client-Server Model

Spring 2003Tuna 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().