1 / 23

Advanced Sockets API-II

Advanced Sockets API-II. Vinayak Jagtap vinayak@ncst.ernet.in. UDP Sockets. Socket Functions for UDP client/server. Server. Client. socket(). socket(). bind(). sendto(). Data (request). recvfrom(). Process request. Data (reply). sendto(). recvfrom(). close(). Application details.

trung
Download Presentation

Advanced Sockets API-II

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. Advanced Sockets API-II Vinayak Jagtap vinayak@ncst.ernet.in

  2. UDP Sockets

  3. Socket Functions for UDP client/server Server Client socket() socket() bind() sendto() Data (request) recvfrom() Process request Data (reply) sendto() recvfrom() close()

  4. Application details • Applications will have to do for themselves • Acknowledgement of packets • Flow control • Sequencing

  5. #include <sys/types.h> #include <sys/socket.h> int sendto(int sockfd, char* buf, int nbytes, int flags, struct sockaddr* to, int addrlen); Return value: No. of bytes sent if OK, -1 on error Function for sending data Flags argument is either 0 or is formed by OR’ing some constants like: MSG_DONTROUTE Bypass routing MSG_DONTWAIT Enable non-blocking operation

  6. #include <sys/types.h> #include <sys/socket.h> int recvfrom(int sockfd, char* buf, int nbytes, int flags, struct sockaddr* from, int* addrlen); Return value: No. of bytes read if OK, -1 on error Function for receiving data Flags argument is either 0 or is formed by OR’ing some constants like: MSG_PEEK Peek at data present on socket MSG_DONTWAIT Enable non-blocking operation

  7. Simple Echo client/server using UDP fgets sendto recvfrom UDP Client UDP Server sendto fputs recvfrom

  8. Features of the UDP server • Echo function never terminates. • Iterative Server

  9. connect() for UDP! • Connect can be done on a udp socket to specify destination address and port • Does not do any TCP like connection • send can be used instead of sendto as destination address is known. • Receive operations will accept data only from the specified source, and Send operations can send only to the specified destination. • Disconnection can be done by calling connect() with family member (eg sin_family) set to AF_UNSPEC

  10. connect() Advantages: • Is more efficient (due to avoidance of multiple internal connects) • Asynchronous errors (e.g., ICMP errors) can be detected.

  11. Connected UDP Socket Application connected peer read write UDP UDP Datagram from other IP

  12. Getsockbyname() #include <sys/socket.h> int getsockname(int s, struct sockaddr *name, socklen_t *namelen); Getsockname returns the current name for the specified socket. On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS EBADF The argument s is not a valid descriptor. ENOTSOCK The argument s is a file, not a socket. ENOBUFS Insufficient resources available in the system to perform the operation. EFAULT The name parameter points to memory not in a valid part of the process address space.

  13. UDP • When to use? • Applications use broadcasting or multicasting • Cost of connection establishment is high compared to data transferred • When not to use? • Flow control is very important • Packet sequence has to be maintained • E.g. DNS name query, SNMP

  14. Timeouts • SIGALARM

  15. Extended I/O Functions int send(int sockfd, const void *msg, size_t len, int flags) int recv(int sockfd, const void *buff, size_t len, int flags) MSG_OOB 0x1 process out-of-band data MSG_DONTROUTE 0x4 bypass routing, use direct interface MSG_DONTWAIT 0x40 don't block MSG_NOSIGNAL 0x2000 don't raise SIGPIPE

  16. Extended I/O functions #include <sys/uio.h> int readv(int fd, const struct iovec * vector, int count); int writev(int fd, const struct iovec * vector, int count); They return number of bytes read/written on success, -1 on failure struct iovec { void* iov_base; /* Starting address. */ size_t iov_len; /* Length in bytes. */ }

  17. Extended I/O functions #include <sys/socket.h> int recvmsg(int sockfd, struct msghdr *msg, int flags); int sendmsg(int sockfd, const struct msghdr *msg, int flags); They return number of bytes read/written on success, -1 on failure struct msghdr { void * msg_name; /* protocol address */ socklen_t msg_namelen; /* size of address */ struct iovec * msg_iov; /* scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void * msg_control; /* ancillary data */ /*aligned for struct cmsghdr*/ socklen_t msg_controllen; /* ancillary data buffer len */ int msg_flags; /* flags on received message */ };

  18. UNIX Domain Protocols • A way of performing client-server communication on a single host using the sockets (or XTI) API • They are an alternative to IPC

  19. UNIX Domain Protocols Reasons to use: • It is better to use them for networked applications residing on the same host because they increase the efficiency of I/O. They are light weight in comparison with the TCP/IP stack. E.g.: X-Windows applications • Newer implementations provide the client’s credentials (user ID and group ID) to server

  20. UNIX Domain Socket Address Structure In <sys/un.h> struct sockaddr_un { sa_family_t sun_family; /* AF_LOCAL */ char sun_path[104]; /* null terminated path name*/ };

  21. UNIX Domain Protocols • Family is AF_LOCAL/AF_UNIX • A pathname is chosen in the address information

  22. UNIX Domain Protocols Types of sockets provided: • Stream sockets • Datagram sockets • Raw sockets (poorly documented)

  23. References • Unix Network Programming, Volume I • W. Richard Stevens

More Related