1 / 6

read() recv()

Server (connection-oriented protocol). socket(). bind(). listen(). accept(). Client. blocks until connection from client. socket(). connection establishment. connect(). data (request). read() recv(). write() send(). process request. data (reply). write() send().

Download Presentation

read() recv()

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. Server (connection-oriented protocol) socket() bind() listen() accept() Client blocks until connection from client socket() connection establishment connect() data (request) read() recv() write() send() process request data (reply) write() send() read() recv() Socket system calls for connection-oriented protocol

  2. Server (connectionless protocol) Socket system calls for connectionless protocol socket() bind() Client recvfrom() socket() blocks until data received from client bind() data (request) sendto() process request sendto() data (reply) recvfrom() Not necessary in UDP!!

  3. Socket system call • int sockfd = socket (int family, int type, int protocol • Family: AF_UNIX, AF_INET; Type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW struct sockaddr { unsigned short sa_family; //specifies the address type char sa_data[14]; //specifies the address value }; struct sockaddr_in { short sin_family; unsigned short sin_port; 16 bit struct in_addr sin_addr; 32 bit { unsigned long s_addr; } char sin_zero[8]; };

  4. Network Byte Order Functions Example: struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(9999); sin.sin_addr.s_addr = inet_addr(“130.233.224.13”); inet_aton(“130.233.224.13”, &sin.sin_addr); unsigned short htons(unsigned short); unsigned short ntohs(unsigned short); unsigned long htonl(unsigned long); unsigned long ntohl(unsigned long);

  5. Server system calls • int bind(int sockfd, struct sockaddr *myaddr, int addrlen) • int listen(int socket, int qlength) • !newsockfd = accept(int sockfd, void* peer, int *addrlen) • int getpeername(int sockfd, struct sockaddr *addr, int *addrlen); • struct hostent *gethostbyaddr(const char peer_addr, int len, int type); • ! (char*) socaddr_in.sin_addr, name in hostent->h_name

  6. Client system calls • int connect(int sockfd, struct sockaddr* servaddr, int addrlen) • struct hostent *gethostbyname(const char *name); • ! addr.sin_addr = *((struct in_addr *) hostent->h_addr); • write or read(fd, buff_ptr, num_bytes) • int send(int s, const char *msg, int len, int flags) • int recv(int s, char *buf, int len, int flags) • flags: MSG_PEEK, MSG_OOB etc. • close(int socket), shutdown(int socket, int how)

More Related