230 likes | 331 Views
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.
E N D
Advanced Sockets API-II Vinayak Jagtap vinayak@ncst.ernet.in
Socket Functions for UDP client/server Server Client socket() socket() bind() sendto() Data (request) recvfrom() Process request Data (reply) sendto() recvfrom() close()
Application details • Applications will have to do for themselves • Acknowledgement of packets • Flow control • Sequencing
#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
#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
Simple Echo client/server using UDP fgets sendto recvfrom UDP Client UDP Server sendto fputs recvfrom
Features of the UDP server • Echo function never terminates. • Iterative Server
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
connect() Advantages: • Is more efficient (due to avoidance of multiple internal connects) • Asynchronous errors (e.g., ICMP errors) can be detected.
Connected UDP Socket Application connected peer read write UDP UDP Datagram from other IP
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.
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
Timeouts • SIGALARM
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
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. */ }
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 */ };
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
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
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*/ };
UNIX Domain Protocols • Family is AF_LOCAL/AF_UNIX • A pathname is chosen in the address information
UNIX Domain Protocols Types of sockets provided: • Stream sockets • Datagram sockets • Raw sockets (poorly documented)
References • Unix Network Programming, Volume I • W. Richard Stevens