C6. Objectives Client/sever Model
C6. Objectives Client/sever Model
C6. Objectives Client/sever Model
Objectives
Network programming Client-server model Sockets interface Socket primitives Example code for echoclient and echoserver
Client/sever model
Client asks (request) server provides (response) Typically: single server - multiple clients The server does not need to know anything about the client even that it exists The client should always know something about the server at least where it is located
1. Client sends request
Client process Server process Resource
Note: clients and servers are processes running on hosts (can be the same or different hosts).
Client
Internet
physical layer
Clients
Examples of client programs
Web browsers, ftp, telnet, ssh
Client
Client
Page 1
Servers
Servers are long-running processes (daemons).
Created at boot-time (typically) by the init process (process 1) Run continuously until the machine is turned off. What is a socket?
Sockets
To the kernel, a socket is an endpoint of communication. To an application, a socket is a file descriptor that lets the application read/write from/to the network.
Remember: All Unix I/O devices, including networks, are modeled as files.
Each server waits for requests to arrive on a wellknown port associated with a particular service.
Port 7: echo server See /etc/services for a Port 23: telnet server comprehensive list of the Port 25: mail server services available on a Linux machine. Port 80: HTTP server Other applications should choose between 1024 and 65535
Clients and servers communicate with each by reading from and writing to socket descriptors. The main distinction between regular file I/O and socket I/O is how the application opens the socket descriptors.
Socket primitives
SOCKET: int socket(int domain, int type, int protocol);
domain := AF_INET (IPv4 protocol) type := (SOCK_DGRAM or SOCK_STREAM ) protocol := 0 (IPPROTO_UDP or IPPROTO_TCP) returned: socket descriptor (sockfd), -1 is an error
Structure Casts
You will see a lot of structure casts
/* /* /* /*
address family (always AF_INET) */ port num in network byte order */ IP addr in network byte order */ pad to sizeof(struct sockaddr) */
LISTEN : int listen(int sockfd, int backlog); backlog: how many connections we want to queue ACCEPT: int accept(int sockfd, void *addr, int *addrlen); addr: here the socket-address of the caller will be written returned: a new socket descriptor (for the temporal socket) CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); //used by TCP client
parameters are same as for bind()
SEND (DGRAM-style): int sendto(int sockfd, const void * msg, int len, int flags, const struct sockaddr *to, int tolen); msg: message you want to send len: length of the message flags := 0 to: socket address of the remote process tolen: = sizeof(struct sockaddr) returned: the number of bytes actually sent RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); buf: buffer to receive the message len: length of the buffer (dont give me more!) from: socket address of the process that sent the data fromlen:= sizeof(struct sockaddr) flags := 0 returned: the number of bytes received CLOSE: close (socketfd);
SEND: int send(int sockfd, const void *msg, int len, int flags); msg: message you want to send len: length of the message flags := 0 returned: the number of bytes actually sent RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags); buf: buffer to receive the message len: length of the buffer (dont give me more!) flags := 0 returned: the number of bytes received
Page 2
Client+server: connectionless
CREATE BIND
EchoClient.c #includes
#include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */ #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #define ECHOMAX 255 /* Longest string to echo */
EchoServer.c
int main(int argc, char *argv[]) { int sock; /* Socket */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned int cliAddrLen; /* Length of incoming message */ char echoBuffer[ECHOMAX]; /* Buffer for echo string */ unsigned short echoServPort =7; /* Server port */ int recvMsgSize; /* Size of received message */ /* Create socket for sending/receiving datagrams */ sock = socket(AF_INET, SOCK_DGRAM, 0); /* 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(172.24.23.4); echoServAddr.sin_port = htons(echoServPort); /* Local port */ /* Bind to the local address */ bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr);
Page 3
for (;;) /* Run forever */ { cliAddrLen = sizeof(echoClntAddr); /* Block until receive message from a client */ recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen); printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); /* Send received datagram back to the client */ sendto(sock, echoBuffer, recvMsgSize, 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr); }
Client+server: connection-oriented
BIND SOCKET LISTEN CONNECT TCP three-way handshake ACCEPT
SEND
RECEIVE
SEND
} /* end of main () */
RECEIVE
CLOSE
Concurrent server
HTTP
Client
HTTP conversation
Server OK I would like to open a connection GET <file location> Send page or error message Display response Close connection OK
HTTP is the set of rules governing the format and content of the conversation between a Web client and server
HTTP conversation
C:\telnet aut.unitbv.ro 80 GET /aut/bti/index.html [ENTER] [ENTER] Html code here To activate local echo, use: Ctrl + ] set localecho
Email POP3
Used in conjunction with an SMTP Host
SMTP Host sends and receives e-mail for remote users, POP allows users to retreive their mail from the host. SMTP stores mail for unconnected hosts
Page 4
POP3
protocol is relatively simple
connect to port 110 of remote host
read back a response check for OK or ERR over and over again
Commands
POP3 - Commands
USER name
terminate with <crlf> identifies the user/mail drop name
PASS string
user password usually the same as the users logon password
STAT
request number of messages on server and size of mail drop
LIST
return a list <crlf> of all msgs on server
format msg size
Auth
Auth
Transaction
Update
LIST [msg_no]
request size of msg_no
format msg_no size
USER
RETR msg_no
PASS QUIT
return the message identified by msg_no
POP3 - Commands
DELE msg_no
delete msg_no from server happens in UPDATE State
POP3 - Conversation
C:\telnet mail.unitbv.ro 110 +OK UNITBV.RO POP server
NOOP
nothing except a positive reply from server
RSET
reset all deletions pending on server
list
1 144058 2 3707 3 448431
QUIT
quit session, UPDATE, enter AUTH1 State
retr 2
..
quit
Page 5