Presentation is loading. Please wait.

Presentation is loading. Please wait.

回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.

Similar presentations


Presentation on theme: "回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need."— Presentation transcript:

1 回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need to know anything about the client u even that it exists n The client should always know something about the server u at least where it is located Client process Server process 1. Client sends request 2. Server handles request 3. Server sends response 4. Client handles response Resource Note: clients and servers are processes running on hosts (can be the same or different hosts).

2 回到第一頁 Servers n Servers are long-running processes (daemons). u Created at boot-time (typically) by the init process (process 1) u Run continuously until the machine is turned off. n Each server waits for requests to arrive on a well-known port associated with a particular service. u Port 7: echo server u Port 23: telnet server u Port 25: mail server u Port 80: HTTP server n Other applications should choose between 1024 and 65535 See /etc/services for a comprehensive list of the services available on a Linux machine.

3 回到第一頁 Clients n Examples of client programs  Web browsers, ftp, telnet, ssh n How does a client find the server? u The IP address in the server socket address identifies the host u The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. u Examples of well known ports F Port 7: Echo server F Port 23: Telnet server F Port 25: Mail server F Port 80: Web server

4 回到第一頁 Sockets n What is a socket? u To the kernel, a socket is an endpoint of communication. u To an application, a socket is a file descriptor that lets the application read/write from/to the network. F Remember: All Unix I/O devices, including networks, are modeled as files. n Clients and servers communicate with each by reading from and writing to socket descriptors. n The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.

5 回到第一頁 Linux Socket n The socket interface is an extension of pipes. Sockets are used to communicate across networks.

6 回到第一頁 Client / Server Session ClientServer socket bind listen read writeread write Connection request read close EOF open_listenfd acceptconnect open_clientfd Overview

7 回到第一頁 Creating a Socket Domains include:

8 回到第一頁 Step 1 – Setup Socket Both client and server need to setup the socket – int socket(int domain, int type, int protocol); domain – AF_INET -- IPv4 (AF_INET6 for IPv6)‏ type – SOCK_STREAM -- TCP – SOCK_DGRAM -- UDP protocol – 0 For example, – int sockfd = socket(AF_INET, SOCK_STREAM, 0);

9 回到第一頁 Step 2 (Server) - Binding Only server need to bind – int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); sockfd – file descriptor socket() returned my_addr – struct sockaddr_in for IPv4 struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490)‏ struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton()‏ };

10 回到第一頁 struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ }; Struct sockaddr_un{ sa_family_t sun_family; /*AF_UNIX */ char sun_path[] ; /* pathname */ }; Struct in_addr{ unsigned long int s_addr; };

11 回到第一頁 Step 3 (Server) - Listen Now we can listen – int listen(int sockfd, int backlog); sockfd – again, file descriptor socket() returned backlog – number of pending connections to queue For example, – listen(sockfd, 5);

12 回到第一頁 Step 4 (Server) - Accept Server must explicitly accept incoming connections – int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)‏ sockfd – again... file descriptor socket() returned addr – pointer to store client address, (struct sockaddr_in *) cast to (struct sockaddr *)‏ addrlen – pointer to store the returned size of addr, should be sizeof(*addr)‏ For example – int isock=accept(sockfd, (struct sockaddr_in *) &caddr, &clen);

13 回到第一頁 What about client? Client need not bind, listen, and accept All client need to do is to connect – int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen); For example, – connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));

14 回到第一頁 Network programming n socket(family, type, protocol) n bind(fd, uaddr, addrlen) n listen(fd, backlog) n accept(fd, sockaddr, skaddrlen)

15 回到第一頁 socket(family, type, protocol) n u sys_socket(...) u get an empty inode for socket structure u allocate a sock structure u return the file descriptor of the socket if success

16 回到第一頁 bind(fd, uaddr, addrlen) n To make a socket available for use by other processes, a server program needs to give the socket a name. n For AF_INET sockets, this means associating the socket with an IP port number. n n sys_bind(...) u bind a name to a socket u move the socket address to kernel space u address & port checking

17 回到第一頁 listen(fd, backlog) n To accept incoming connections on a socket, a server program must create a queue to store pending requests. n u sys_listen(...) u allow the protocol to do anything necessary u move a socket into listening state

18 回到第一頁 accept(fd, sockaddr, skaddrlen) n u sys_accept(...) u attempt to create a new socket u set up the link with the client u wake up the client u return the new fd

19 回到第一頁 connect(fd, uaddr, addrlen) n u sys_connect(...) u attempt to connect to a socket with the server address u move the address to kernel space

20 回到第一頁 Closing a Socket n You can terminate a socket connection at the server and client by calling close.

21 回到第一頁 Socket Programming Clients n Network Byte Ordering u Network is big-endian, host may be big- or little-endian u Functions work on 16-bit (short) and 32-bit (long) values u htons() / htonl() : convert host byte order to network byte order u ntohs() / ntohl(): convert network byte order to host byte order u Use these to convert network addresses, ports, … n Structure Casts u You will see a lot of ‘structure casts’ struct sockaddr_in serveraddr; /* fill in serveraddr with an address */ … /* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); …

22 回到第一頁 Network Information

23 回到第一頁 Similarly, information concerning services and associated port numbers is availble through some service information functions.

24 回到第一頁 The address list needs to be cast to the appropriate address type and converted from network ordering to a printable string, using the inet_ntoa conversion.

25 回到第一頁 What is select()? Monitor multiple descriptors How does it work? – Setup sets of sockets to monitor – select(): blocking until something happens – “Something” could be Incoming connection: accept()‏ Clients sending data: read()‏ Pending data to send: write()‏ Timeout

26 回到第一頁 What is FD_SET()? #include #include void FD_ZERO(fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); void FD_SET(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset); FD_ZERO initializes an fd_set to the empty set, FD_SET and FD_CLR set and clear elements of the set fdsetcorresponding to the file descriptor passed as fd FD_ISSET returns non-zero if the file descriptor referred to by fd is an element of the fd_set pointed to by fdset.

27 回到第一頁 prevent indefinite blocking The select function can also use a timeout value to prevent indefinite blocking. The timeout value is given using a struct timeval. This structure, defined in sys/time.h, has the following members: struct timeval { time_t tv_sec; /* seconds */ long tv_usec; /* microseconds */ }

28 回到第一頁 #include int main() { char buffer[128]; int result, nread; fd_set inputs, testfds; struct timeval timeout; FD_ZERO(&inputs); FD_SET(0,&inputs); while(1) { testfds = inputs; timeout.tv_sec = 2; timeout.tv_usec = 500000; result = select(FD_SETSIZE, &testfds, (fd_set *)NULL, (fd_set *)NULL, &timeout); switch(result) { case 0: printf("timeout\n"); break; case -1: perror("select"); exit(1); default: if(FD_ISSET(0,&testfds)) { ioctl(0,FIONREAD,&nread); if(nread == 0) { printf("keyboard done\n"); exit(0); } nread = read(0,buffer,nread); buffer[nread] = 0; printf("read %d from keyboard: %s", nread, buffer); } break; } } }

29 回到第一頁 ioctl Interfaces The FIONREAD: ioctl returns the number of data bytes (in all data messages queued) in the location pointed to by the arg parameter.


Download ppt "回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need."

Similar presentations


Ads by Google