1 / 34

Client/Server Model (1)

Socket Programming contents: - Client-Server Architecture - Socket Interface - Application Process/Transport Layer. 1. Client/Server Model (1). Connection Setup. Wait Connection. Send Request /Receive Result. Process Request. Release Connection. Send Result and Loop. Client.

Download Presentation

Client/Server Model (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programmingcontents:- Client-Server Architecture- Socket Interface- Application Process/Transport Layer 1

  2. Client/Server Model (1) Connection Setup Wait Connection Send Request /Receive Result Process Request Release Connection Send Result and Loop Client Server Network Connection 2

  3. Client/Server Model (2) • Server • Iterative Server • Concurrent Server • Client • active open • send the message to server • receive the message from server • release communication channel 3

  4. Structure of Iterative Server #include <..> main() { int sockfd, newsockfd; if((sockfd = socket(..) < 0) error(); if(bind(sockfd, ...) <0) error(); if(listen(sockfd, 5) < 0) error(); for( ; ; ) { newsockfd = accept(sockfd, ..); if(newsockfd < 0) error(); dostuff(newsockfd); close(newsockfd); } } Server Client Client Client 4

  5. Iterative Connection (TCP) 5

  6. Iterative Connectionless(UDP) 6

  7. Structure of Concurrent Server Master Slave Slave Slave Slave Slave Slave 7

  8. Concurrent, Connection-oriented (fork) #include <..> main() { int sockfd, newsockfd; if((sockfd = socket(..) < 0) error(); if(bind(sockfd, ...) <0) error(); if(listen(sockfd, 5) < 0) error(); for( ; ; ) { newsockfd = accept(sockfd, ..); if(newsockfd < 0) error(); if( fork() == 0) /* child */ { close(sockfd); dostuff(newsockfd); close(newsockfd); exit(0); } /* end if */ close(newsockfd); /* parent */ } /* end for */ } /* end main */ 8

  9. Concurrent, Connection-oriented (thread) 9

  10. Concurrent, Connectionless (UDP) 10

  11. Network Programming API Application Program System V TLI Berkeley Sockets OS Network Kernel 11

  12. Socket 12

  13. Socket Interface 13

  14. Connection Oriented Process socket() Server Client bind() listen() socket() accept() Connection setup connect() blocking until connection read() write() Request(data) process request Reply(data) write() read() 14

  15. Connectionless Process Server socket() Client bind() socket() recvfrom() bind() blocking until connection Request(data) sendto() process request Reply(data) sendto() recvfrom () 15

  16. BSD Socket Library 16

  17. Transport Protocol Port • Port Number • 0 - no use • 1-255 : Well-known port number • 256-1023 : reserved port number • 1024-4999: client port number • 5000-65535: user defined port number 17

  18. Association • Five information set • Data Elements • protocol Identifier • Source internet address • Source port number • Destination internet address • Destination port number 18

  19. Socket Programming • Socket Type • Stream socket: connection oriented (TCP) • Datagram socket: connectionless (UDP) • Raw socket: IP and ICMP • Socket address • Family: 16 bit integer • port: 16 bit integer • address: 32 bit internet address 19

  20. Socket Data Structure • struct sockaddr_in { • short sin_family; /* AF_INET*/ • u_short sin_port; /* 16 bit port • number-network byte ordered */ • struct in_addr sin_addr; /* 32 bit internet • address- network byte ordered */ • char sin_zero[8]; /* unused */ • }; 20

  21. Socket Address Data Type • Unsigned Integer Data Type • u_char • u_short • u_long • Internet address data structure(32 bit) • struct in_addr { • u-long s_addr; • }; 21

  22. hostent structure • struct hostent { • char *h_name; /* host name */ • char **h_aliases; /* host allias */ • int h_addrtype; /* host address type */ • int h_length; /* address length */ • char **h_addr_list; /* address list */ • } 22

  23. gethostbyname( ) : find hostent structure by given the name struct hostent *gethostbyname(char *name) success: pointer to structure, failure: 0 example: struct hostent * host_info ; host_info = gethostbyname(“www.woosong.ac.kr”); 23

  24. bzero(memset), bcopy(memcpy) • bzero():fill the buffer having the length with 0(ASCII Null) • void bzero(char *buffer, int length) • void *memset(void *s, int c, size_t n) • bcopy() : copy nbytes from src to dest • void bcopy(char *src, char *dest, int nbytes) • void memcpy(void *s1, const void *s2, size_t n) copy from s2 to S1 24

  25. Support function • Network Byte Order • Big-Endian Host (IBM,Motorola - TCP/IP) • 192 10 32 1 • 1100 0000 0000 1010 0010 0000 0000 0001 • Little-Endian (DEC-VAX,Intel) • 1 32 10 192 • 0000 0001 0010 0000 0000 1010 1100 0000 25

  26. Integer octet order function • #include <sys/types.h> • #include <netinet/in.h> • htons : host order short network order(16 bit) u_short htons(u_short num) • htonl : host order (32 bit) network order u_long htonl(u_long num) • ntohs : network order host short(16 bit) u-short ntohs (u_short num) • ntohl : network order host long(32 bit) u_long = ntohl (u_long num) 26

  27. Address translation function • #include <sys/types.h> <sys/socket.h> • <netinet/in.h> <arpa/inet.h> • inet_addr : ASCII dot-decimal(A.B.C.D) -> 32 bit IP address(binary) • unsigned long inet_addr (const char *addr) • failure: -1 • inet_ntoa : 32 bit IP address-> ASCII dotted-decimal (A.B.C.D) • ascii_addr = char *inet_ntoa (struct in_addr in) • failure: -1 27

  28. Relationship of function • Host storage order networked byte order • (binary IP address) • (htons,htonl ntohs, ntohl) • ASCII Dotted-Decimal order networked byte order • ( inet_addr, inet_ntoa) 28

  29. System Call (1) (1) socket() int socket (int domain, int type, int protocol) domain : AF_UNIX : same domain in c/s AF_INET : different domain of c/s type : SOCK_STREAM : stream(TCP) SOCK_DGRAM : datagram(UDP) protocol: 0(right) Header File(AF_INET) : <netinet/in.h> < arpa/inet.h> <netdb.h> success: socket descriptor failure: -1 29

  30. System Call (2) (2) bind() : assign the address int bind (int fd, struct sockaddr * address, int addressLen) (3) listen() : wait for the connection from client int listen (int fd, int queueLength) queueLength: maximum number of waiting sockets (in general 5) success: 0 failure: -1 30

  31. System Call (3) (4) accept() : accept the connection from client int accept (int fd, struct sockaddr * address, int addressLen) (5) connect(): connect to server int connect (int fd, struct sockaddr * address, int addressLen) success: 0 failure: -1 31

  32. System Call (4) File Header : <stdio.h> (4) write() : send the packet int write (int fd, char *buf, int nbytes) (5) read() : receivethe packet int read (int fd, char *buf, int nbytes) success: number of bytes failure: -1 32

  33. System Call (5) File Header : <sys/types.h> <sys/socket.h> (6) send(), sendto() : int send (int fd, char *buf, int nbytes, int flags) int sendto (int fd, char *buf, int nbytes, int flags, struct sockaddr * to, int addressLen) (7) recv(), recvfrom() : int recv (int fd, char *buf, , int nbytes, int flags) int recvfrom (int fd, char *buf, int nbytes, int flags, struct sockaddr * from, int addressLen) 33

  34. Other Programming • PC Winsock Programming • Java Network Programming 34

More Related