Download presentation
Presentation is loading. Please wait.
1
Advanced Sockets API-II Vinayak Jagtap vinayak@ncst.ernet.in
2
UDP Sockets
3
Socket Functions for UDP client/server socket() bind() socket() sendto() recvfrom() close() recvfrom() sendto() Data (request) Data (reply) Client Server Process request
4
Application details Applications will have to do for themselves Acknowledgement of packets Flow control Sequencing
5
Function for sending data #include 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 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
Function for receiving data #include 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 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 UDP ClientUDP Server sendto recvfrom sendto recvfrom fgets fputs
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 UDP connected peer UDP Datagram from other IP read write
12
Getsockbyname() #include 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 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 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 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.