Sockets Chapter 10 from the book Interprocess Communications
Sockets Chapter 10 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January 13, 2003 1
COMMUNICATION BASICS Topics 2 Network Addresses Domains Protocol Families Socket Types IPC Using Socketpair The Connection-Oriented Paradigm The Connectionless Paradigm
COMMUNICATION BASICS Network Addresses Every host on a network has two unique addresses: Ø The first one is a 48 -bit media access control (MAC) address that is assigned to its network interface card (NIC). (Figure 10. 1) 3
COMMUNICATION BASICS linux$ /sbin/ifconfig -a eth 0 Link encap: Ethernet HWaddr 00: B 0: D 0: AB: 7 C: 96 <-- 1 BROADCAST MULTICAST MTU: 1500 Metric: 1 | RX packets: 0 errors: 0 dropped: 0 overruns: 0 frame: 0 | TX packets: 0 errors: 0 dropped: 0 overruns: 0 carrier: 0 | collisions: 0 txqueuelen: 100 | Interrupt: 16 Base address: 0 xecc 0 Memory: e 08 fc 000 -e 08 fcc 40| | eth 1 Link encap: Ethernet HWaddr 00: 02: B 3: 35: 9 E: 21 <-- 1 inet addr: 137. 49. 6. 1 Bcast: 137. 49. 255 Mask: 255. 0. 0 UP BROADCAST RUNNING MULTICAST MTU: 1500 Metric: 1 RX packets: 471693034 errors: 2 dropped: 0 overruns: 27398 frame: 2 4 Figure 10. 1. Displaying the MAC addresses on a Linux host.
COMMUNICATION BASICS TX packets: 2147483647 errors: 0 dropped: 0 overruns: 0 carrier: 0 collisions: 0 txqueuelen: 100 Interrupt: 26 Base address: 0 xd 4 c 0 Memory: e 08 fe 000 -e 08 fec 40 lo ink encap: Local Loopback inet addr: 127. 0. 0. 1 Mask: 255. 0. 0. 0 UP LOOPBACK RUNNING MTU: 16436 Metric: 1 RX packets: 95939986 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 95939986 errors: 0 dropped: 0 overruns: 0 carrier: 0 collisions: 0 txqueuelen: 0 (1)This host has two network cards. As shown, only eth 1 is active 5 Figure 10. 1. Displaying the MAC addresses on a Linux host.
COMMUNICATION BASICS Network Addresses Ø The second one is a 32 -bit Internet (IP) address subdivided into » Network portion (netid) » Local portion (hostid) Ø Using the leading bits of the netid value, networks can be divided into five classes, A through E. Ø First three classes are: A-C » In a Class A network bit 0 is 0 » In a Class B the first two bits are 10 » In a Class C the first three bits are 110 6
COMMUNICATION BASICS Network Addresses Ø Figure 10. 2 shows that the range of hostids is directly related to the class of the network. Figure 10. 2. Network class numbering scheme. 7
COMMUNICATION BASICS Domains A dotted IP address can be mapped into a more easily understood symbolic notation using the Domain Name System (DNS). All Internet addresses are divided into a set of high-level Ø Organizational domains Ø Geographical domains 8
COMMUNICATION BASICS Domains When we create the socket, we specify its communication domain. The two types of socket communication domains are: Ø UNIX domain » In this domain, when sockets are created, they have actual file (path) names. These sockets can be used only with processes that reside on the same host. UNIX domain sockets are sometimes used as the first step in the development of socket-based communications because, due to their locality, they are somewhat easier to debug. 9
COMMUNICATION BASICS Domains Ø Internet domain » These sockets allow unrelated processes on different hosts to communicate. 10
COMMUNICATION BASICS Protocol Families A set of rules and conventions is called a protocol. Protocols are designed in layers. The layering of protocols facilitates a certain degree of isolation. Figure 10. 3 shows the standard seven layer OSI model. 11
COMMUNICATION BASICS Layer Functionality Application Provides processes access to interprocess facilities. Presentation Responsible for data conversion—text compression and reformatting, encryption. Session Addresses the synchronization of process dialog— establishes, manages, and terminates connections. Transport Responsible for maintaining an agreed-upon level of data exchange. Determines type of service, flow control, error recovery, and so on. Network Concerned with the routing of data from one network to another—establishing, maintaining, and terminating connections. Data Link Insures error-free transmission of data. Physical Addresses physical connections and transmission of raw data stream. 12 Figure 10. 3. The ISO/OSI layer cake.
COMMUNICATION BASICS Protocol Families A grouping of layers, usually equal to transport and network layers of the OSI model that deals with data format , addressing, flow control and error handling is called a protocol family. There a number of protocol families: Ø Ø 13 SNA— IBM's Systems Network Architecture UUCP— UNIX-to-UNIX copy XNS— Xerox Network System NETBIOS— IBM's Network Basic Input/Output System Ø TCP/IP— DARPA (Defense Advanced Research Projects Agency) Internet
COMMUNICATION BASICS Protocol Families Our discussion centers on TCP and UDP of the TCP/IP protocol family, Internet domain (PF_INET) and UNIX domain (PF_UNIX) that is used in creation of sockets In general TCP/IP composed of: 14 Ø TCP— Transmission Control Protocol. TCP is reliable, full duplex and connection-oriented. Data is transmitted as a byte stream. Ø IP— Internet Protocol. Provides delivery of packets. TCP, UDP and ICMP usually call IP. Ø ARP/RARP— Address/Reverse Address Resolution Protocol. These protocols are used to resolve Internet/hardware addressing. Ø UDP— User Datagram Protocol. UDP is non-reliable, full duplex, and connectionless. Data is transmitted as a series of packets. Ø ICMP— Internet Control Message Protocol. Used for error handling and flow control.
COMMUNICATION BASICS Socket Types Communicated data can be in a Ø Stream format Data is transmitted as a sequence of bytes Ø Datagram format » Datagrams are small, discrete packets that, contain header information, data, and trailer information (error correction, etc. ). Socket type will determine how communications will be carried on between the processes using the socket. Sockets must be of the same type to communicate. 15
COMMUNICATION BASICS Socket Types There are two basic socket types: Ø Stream sockets » These sockets are reliable. When these sockets are used, data is delivered in order, in the same sequence in which it was sent. There is no duplication of data, and some form of error checking and flow control is usually present. Stream sockets allow bidirectional (full duplex) communication. Stream sockets are connection-oriented. That is, the two processes using the socket create a logical connection (a virtual circuit). Information concerning the connection is established prior to the transmission of data and is maintained by each end of the connection during the communication. Data is transmitted as a stream of bytes. In a very limited fashion, these sockets also permit the user to place a higher priority urgent message ahead of the data in the current stream. 16
COMMUNICATION BASICS Socket Types Ø Datagram sockets » Datagram sockets are potentially unreliable. Thus, with these sockets, received data may be out of order. Datagram sockets support bidirectional communications but are considered connectionless. There is no logical connection between the sending and receiving processes. Each datagram is sent and processed independently. Individual datagrams may take different routes to the same destination. With connectionless service, there is no flow control. Error control, when specified, is minimal. Datagram packets are normally small and fixed in size. 17
IPC Using Socketpair The socketpair call is used to create the pair of sockets and is only implemented for the PF_UNIX (PF_LOCAL) family and is used for the same host communications. Pipes also are used for the same host but they are limited because the file descriptors permit unidirectional communication. The socketpair function creates two file descriptors for two connected sockets on the same computer. These file descriptors permit two-way communication between related 18 processes.
IPC Using Socketpair Include File(s) <sys/socket. h> Summary int socketpair( int d, int type, int protocol, int sv[2] ); Return Manual Section 2 Success Failure Sets errno 0 and two open socket descriptors -1 Yes Table 10. 1. Summary of the socketpair System Call 19
IPC Using Socketpair socketpair takes four arguments: Ø The first argument is an integer value that specifies the domain. » In general, the domains for socket-based calls should be specified as one of the protocol family-defined constants found in the header file <bits/socket. h>. (Table 10. 2) 20
IPC Using Socketpair Constant 21 Value Reference PF_UNSPEC 0 Unspecified PF_LOCAL 1 Local to host (pipes and file-domain). PF_UNIX PF_LOCAL Old BSD name for PF_LOCAL. PF_FILE PF_LOCAL Another nonstandard name for PF_LOCAL. PF_INET 2 IP protocol family. PF_AX 25 3 Amateur Radio AX. 25. PF_IPX 4 Novell Internet Protocol. PF_APPLETALK 5 Appletalk DDP. PF_NETROM 6 Amateur radio Net. ROM. PF_BRIDGE 7 Multiprotocol bridge. PF_ATMPVC 8 ATM PVCs. Table 10. 2. Protocol Family Constants.
IPC Using Socketpair Constant Reference PF_X 25 9 Reserved for X. 25 project. PF_INET 6 10 IP version 6. PF_ROSE 11 Amateur Radio X. 25 PLP. . PF_DECnet 12 Reserved for DECnet project. . PF_NETBEUI 13 Reserved for 802. 2 LLC project. . PF_SECURITY 14 Security callback pseudo AF. . PF_KEY 15 PF_KEY key management API. PF_NETLINK 16 PF_ROUTE 22 Value PF_NETLINK Alias to emulate 4. 4 BSD. . PF_PACKET 17 Packet family. . PF_ASH 18 Ash. . Table 10. 2. Protocol Family Constants.
IPC Using Socketpair Constant 23 Value Reference PF_ECONET 19 Acorn Econet. PF_ATMSVC 20 ATM SVCs. PF_SNA 22 Linux SNA Project PF_IRDA 23 IRDA sockets. PF_PPPOX 24 PPPo. X sockets. PF_MAX 32 For now. Table 10. 2. Protocol Family Constants.
IPC Using Socketpair Ø The second argument for the socketpair call is type that indicates the socket type: » The defined constant SOCK_STREAM to indicate a stream socket. » The defined constant SOCK_DGRAM to indicate a datagram-based socket. 24
IPC Using Socketpair Ø The third argument is protocol that is used to indicate the protocol within the specified family. » In most cases, this argument is set to 0, which indicates to the system that it should select the protocol. ª UDP for connectionless sockets ª TCP for connection-oriented 25
IPC Using Socketpair Ø The fourth argument is sv that is the base address of an integer array that references the two socket descriptors. » Descriptors are created if the call is successful. » Each descriptor is bidirectional and is available for both reading and writing. » If the socketpair call fails, it returns a − 1 and sets errno. (Table 10. 3. ) 26
IPC Using Socketpair # Constant perror Message Explanation 14 EFAULT Bad address sv references an illegal address. 24 EMFILE Too many open files This process has reached the limit for open file descriptors. 93 EPROTONOSUPPORT Protocol not supported Requested protocol not supported on this system. 95 EOPNOTSUPPORT Operation not supported Specified protocol does not support socket pairs. 97 EAFNOSUPPORT Address family not supported by protocol Cannot use the indicated address family with specified protocol family. Table 10. 3. socketpair Error Messages. . 27
IPC Using Socketpair Figure 10. 5 shows in the socketpair. c program example, how the created sockets communicate and exchange information between the parent and child processes. To see how socketpair. c example program works, compile socketpair. c with –lsocket optin on jupiter and see the result. Can you modify it to be able to run on elara? Instead of socketpair, the more general system call (i. e. , socket) is uses for interprocess communications. 28
IPC Using Socketpair 29 Figure 10. 5. The socketpair before and after the process forks.
The Connection-Oriented Paradigm Socket types are: Ø connection-oriented (type SOCK_STREAM) Figure 10. 6. Ø connectionless (type SOCK_DGRAM) 30
The Connection-Oriented Paradigm 31 Figure 10. 6. A connection-oriented client–server communication sequence.
The Connection-Oriented Paradigm Figure 10. 6 shows Ø The process initiating the connection is the client process and the process receiving the connection is the server. Ø Both the client and server processes use the socket call to create a new instance of a socket. Ø The socket will act as a queuing point for data exchange. (Table 10. 4) 32
The Connection-Oriented Paradigm Include File(s) <sys/types. h> <sys/socket. h> Summary Int socket( int domaint, int type, int protocol) ; Return Manual Section Success 0 and an open socket descriptor. 2 Failure Sets errno -1 Yes Table 10. 4. Summary of the socket System Call. 33
The Connection-Oriented Paradigm The socket system call takes three arguments: Ø The first argument is an integer value that specifies the protocol family. (Table 10. 5) 34
The Connection-Oriented Paradigm Constant Protocol Family PF_APPLETALK Appletalk PF_ATMPVC Access to raw ATM PVCs PF_AX 25 Amateur radio AX. 25 protocol PF_INET IPv 4 Internet protocols PF_INET 6 IPv 6 Internet protocols PF_IPX - Novell protocols PF_NETLINK Kernel user interface device PF_PACKET Low-level packet interface PF_UNIX, PF_LOCAL Local communication PF_X 25 ITU-T X. 25 / ISO-8208 protocol x 25(7) Table 10. 5. Supported Protocol Families. 35
The Connection-Oriented Paradigm Ø The second argument for the socket system call is type that indicates the socket type: » The defined constant SOCK_STREAM to indicate a stream socket. » The defined constant SOCK_DGRAM to indicate a datagram-based socket. 36
The Connection-Oriented Paradigm Ø The third argument is protocol that is used to indicate the protocol within the specified family. » In most cases, this argument is set to 0, which indicates to the system that it should select the protocol. » If the socket call is successful, it will return an integer value that can be used to reference the socket descriptor. » If the call fails, it returns a − 1 and sets errno. (Table 10. 6. ) 37
The Connection-Oriented Paradigm # Constant Perror Message Explanation When creating a socket, insufficient memory available. 12 ENOMEM Cannot allocate memory 13 EACCES Permission denied Cannot create a socket of the specified type/protocol. 22 EINVAL Invalid argument Unknown protocol or protocol family is not available. 23 ENFILE Too many open files in system 24 EMFILE Too many open files 93 EPROTONOSUPPORT Protocol not supported Requested protocol not supported 105 ENOBUFS 38 Insufficient kernel (system) memory for socket allocation. This process has reached the limit for open file descriptors. on this system or within this domain. No buffer space available Socket cannot be created until resources are freed. Table 10. 6. socket Error Messages.
The Connection-Oriented Paradigm Initially, when the socket is created, it is unbound (i. e. , there is no name or address/port number pair associated with the socket). If the process creating the socket is to act as a server, the socket must be bound. (Table 10. 7) 39
The Connection-Oriented Paradigm Include File(s) <sys/types. h> <sys/socket. h> Summary int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); Return Manual Section 2 Success Failure Sets errno 0 -1 Yes Table 10. 7. Summary of the bind System Call 40
The Connection-Oriented Paradigm The bind system call takes two arguments: Ø The first argument is an integer value that has been returned from a previous successful socket call. Ø The second argument is a real gem. The short explanation is the argument references a generic address structure of the type: /* Structure describing a generic socket address. */ struct sockaddr { __SOCKADDR_COMMON (sa_); /* Common data: adr family and length. */ char sa_data[14]; /* Address data. */ }; 41
The Connection-Oriented Paradigm If bind is successful, it returns a 0; otherwise, it returns a − 1 and sets the value of errno. (Table 10. 8) 42
The Connection-Oriented Paradigm # Constant Perror Message Explanation 2 ENOENT No such file or directory Component of the path for the file name entry does not exist. 9 EBADF Bad file descriptor sockfd reference is invalid. 12 ENOMEM Cannot allocate memory Insufficient memory. 13 EACCES Permission denied • Cannot create a socket of the specified type/ protocol. • Search access denied for part of the path specified by name. 14 EFAULT Bad address my_addr references address outside user's space. 20 ENOTDIR Not a directory Part of the path of my_addr is not a directory. 22 EINVAL Invalid argument • addrlen is invalid. • sockfd already bound to an address. 43 Table 10. 8. bind Error Messages.
The Connection-Oriented Paradigm # Constant Perror Message Explanation 30 EROFS Read-only file system File would reside on a read-only file system. 36 ENAMETOOLONG File name too long my_addr name is too long. 40 ELOOP Too many levels of symbolic links 63 ENOSR Out of streams resources Insufficient STREAMS resources for specified operation. 88 ENOTSOCK Socket operation on nonsocket sockfd is a file descriptor, not a socket descriptor 98 EADDRINUSE Address already in use Specified address already in use. 99 EADDRNOTAVAIL Can't assign request address 44 Too many symbolic links in my_addr. The specified address is not available on the local system. Table 10. 8. bind Error Messages.
The Connection-Oriented Paradigm The listen system call creates a queue for incoming connection requests. If the queue is full and the protocol does not support retransmission, the client process generating the request will receive the error ECONNREFUSED from the server. If the protocol does support retransmission, the request is ignored, so a subsequent retry can succeed. This call only applies to sockets of type SOCK_STREAM or SOCK_SEQPACKET. (Table 10. 9) 45
The Connection-Oriented Paradigm Include File(s) <sys/types. h> <sys/socket. h> Summary int listen(int s, int backlog); Return Manual Section 2 Success Failure Sets errno 0 -1 Yes Table 10. 9. Summary of the listen System Call. 46
The Connection-Oriented Paradigm The listen system call takes two arguments: Ø The first argument is a valid integer socket descriptor. Ø The second argument, backlog, denotes the maximum size of the queue. Should the listen call fail, it sets errno. (Table 10. 10) 47
The Connection-Oriented Paradigm # Constant Perror Message Explanation s reference is invalid. 9 EBADF Bad file descriptor 88 ENOTSOCK Socket operation on non-socket s is a file descriptor, not a socket descriptor. 95 EOPNOTSUPP Operation not supported Socket type (such as SOCK_DGRAM) does not support listen operation. Table 10. listen Error Messages. 48
The Connection-Oriented Paradigm When the server process is ready to accept a connection from a client process, accept system call is called. (Table 10. 11. ) 49
The Connection-Oriented Paradigm Include File(s) <sys/types. h> <sys/socket. h> Summary int accept( int s, struct sockaddr *addr, socklen_t *addrlen ); Return Manual Section 2 Success Failure Sets errno Positive integer new socket descriptor value -1 Yes Table 10. 11. Summary of the accept System Call. 50
The Connection-Oriented Paradigm The accept system call takes three arguments: Ø The first argument is a valid integer socket descriptor. Ø The second argument, *addr, is a pointer to a generic sockaddr structure. Ø The third argument, *addrlen, initially contains a reference to the length, in bytes, of the previous sockaddr structure. Should the accept call fail, it sets errno. (Table 10. 12) 51
The Connection-Oriented Paradigm # Constant Perror Message Explanation 1 EPERM Operation not permitted Firewall software prohibits connection. 4 EINTR Interrupted system call A signal was received during the accept process. 9 EBADF Bad file descriptor The socket reference is invalid. 11 EWOULDBLOCK, EAGAIN Resource temporarily unavailable The socket is set to non-blocking, and no connections are pending 12 ENOMEM Cannot allocate memory 14 EFAULT Bad address Reference for addr is not writeable. 19 ENODEV No such device Specified protocol family/type not found in the netconfig file. 52 Insufficient memory to perform operation. Table 10. 12. accept Error Messages.
The Connection-Oriented Paradigm # Constant Perror Message Explanation 22 EINVAL Invalid argument passed to accept call 24 EMFILE Too many files open Process has exceeded the maximum number of files open. 63 ENOSR Out of streams resources 71 EPROTO Protocol error An error in protocol has occurred. 85 ERESTART Interrupted system call should be restarted accept call must be restarted. 88 ENOTSOCK Socket operation on non-socket 93 EPROTONOSUPPORT Protocol not supported 53 Insufficient STREAMS resources for specified operation. The socket is a file descriptor, not a socket descriptor. Invalid protocol specified. Table 10. 12. accept Error Messages.
The Connection-Oriented Paradigm # Constant Perror Message Explanation 94 ESOCKTNOSUPPORT Socket type not supported Invalid socket type specified. 95 EOPNOTSUPP Operation not supported is not of type SOCK_STREAM. 103 ECONNABORTED Software caused connection Connection aborted. abort 105 ENOBUFS No buffer space available Insufficient memory to perform operation. 110 ETIMEDOUT Connection timed out Unable to establish connection within specified time limit. 54 Table 10. 12. accept Error Messages.
The Connection-Oriented Paradigm In the connection-oriented setting, the client process initiates the connection with the server process with the connect system call. (Table 10. 13. ) 55
The Connection-Oriented Paradigm Include File(s) <sys/types. h> <sys/socket. h> Summary int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); Success Failure Return Manual Section 0 2 -1 Table 10. 13. Summary of the connect System Call. 56 Sets errno Yes
The Connection-Oriented Paradigm The connect system call takes three arguments: Ø The first argument is a valid integer socket descriptor. Ø The second argument, *serv_addr, is handled differently depending upon whether the referenced socket is connection-oriented or connectionless. » In the connection-oriented setting, *serv_addr references the address of the socket with which the client wants to communicate (i. e. , the serving process's address). » For a connectionless socket, *serv_addr references the address to which the datagrams are to be sent. 57
The Connection-Oriented Paradigm Ø The third argument, addrlen, conveys the size of the *serv_addr reference. Ø Should the connect call fail, it sets errno. (Table 10. 14) 58
The Connection-Oriented Paradigm # Constant Perror Message Explanation 1 EPERM Operation not permitted • Attempt to broadcast without having broadcast flag set. • Request failed due to firewall. 4 EINTR Interrupted system call A signal was received during the connect process. 9 EBADF Bad file descriptor The socket reference is invalid. 11 EAGAIN Resource temporarily unavailable No more free local ports. 13 EACCES Permission denied 14 EFAULT Bad address Address referenced by *serv_addr is outside the user's address space. 22 EINVAL Invalid argument namelength is not correct for address referenced by *serv_addr. 59 Table 10. 14. connect Error Messages.
The Connection-Oriented Paradigm # Constant Perror Message Explanation 63 ENOSR Out of streams resources Insufficient STREAMS resources for specified operation. 88 ENOTSOCK Socket operation on nonsocket The socket is a file descriptor, not a socket descriptor. 91 EPROTOTYPE Protocol wrong type for socket 97 EAFNOSUPPORT Address family not supported by protocol family Address referenced by *serv_addr cannot be used with this socket. 98 EADDRINUSE Local address referenced by *serv_addr already in use. 99 EADDRNOTAVAIL Cannot assign requested address 101 ENETUNREACH 60 Address already in use Network is unreachable Conflicting protocols, socketfd versus the *serv_addr reference. Address referenced by *serv_addr not available on remote system. Cannot reach specified system. Table 10. 14. connect Error Messages.
The Connection-Oriented Paradigm # Constant Perror Message 106 EISCONN Transport endpoint is already connected 110 ETIMEDOUT Connection timed out Explanation sockfd already connected. Could not establish a connection within time limits. 111 ECONNREFUSED Connection refused Connect attempt rejected; socket already connected. 114 EALREADY Operation already in progress Socket is non-blocking, and no previous connection completed. 115 EINPROGRESS Operation now in progress 61 Socket set as non-blocking, and connection cannot be established immediately. Table 10. 14. connect Error Messages.
The Connectionless Paradigm The sequence of events for connectionless client–server communication has some common elements with connection-oriented communication. Both the client and server still generate sockets using the socket call. The server and, most often, the client, bind their sockets to an address. Both the server and client send and receive datagram packets to and from a specified address. The server process sends its packets to the client address, and the client sends its packets to the server address. (Figure 10. 14) 62
The Connectionless Paradigm 63 Figure 10. 14. A connectionless client–server communication sequence.
The Connectionless Paradigm The sendto call is one of several alternate ways to write data to a socket. (Table 10. 19) 64
The Connectionless Paradigm Include File(s) <netinet/in. h> Summary int send (int s, const void *msg, size_t len, int flags); int sendto (int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); int sendmsg(int s, const struct msghdr *msg, int flags); Return Manual Section Success Number of bytes sent. 65 2 Failure Sets errno -1 Yes Table 10. 19. Summary of the send, sendto, and sendmsg System Calls.
The Connectionless Paradigm Flag MSG_OOB Meaning Message out of band. At present this flag is valid only for Internet streambased sockets. Specifying MSG_OOB allows the process to send urgent data. The receiving process can choose to ignore the message. MSG_DONTROUTE Bypass routing tables and attempt to send message in one hop. This is often used for diagnostics purposes. MSG_DONTWAIT Adopt non-blocking operation for operations that block return EAGAIN. MSG_NOSIGNAL On stream-based socket do not send a SIGPIPE error when one end of connection is broken. MSG_CONFIRM With SOCK_DGRAM and SOCK_RAW sockets, in Linux 2. 3+, notify the link layer of successful reply from the other side. 66 Table 10. 20. Flags for the send, sendto, and sendmsg Calls.
The Connectionless Paradigm # Constant Perror Message Explanation A signal was received by the process before data was sent. 4 EINTR Interrupted system call 9 EBADF Bad file descriptor The socket reference is invalid. 11 EWOULDBLOCK, EAGAIN Resource temporarily unavailable The socket is set to non-blocking, and no connections are pending. 12 ENOMEM Cannot allocate memory 14 EFAULT Bad address 67 Insufficient memory to perform operation. Address referenced by *serv_addr is outside the user's address space. Table 10. 21. send, sendto, and sendmsg Error Messages.
The Connectionless Paradigm # Constant Perror Message Explanation 22 EINVAL Invalid argument tolen argument contains an incorrect value. 32 EPIPE Broken pipe Local end of a connection-oriented socket is closed. 88 ENOTSOCK Socket operation on nonsocket The socket argument is a file descriptor, not a socket descriptor. 90 EMSGSIZE Message too long Socket type requires message to be sent to be atomic (all sent at once) and the message to send is too long. 105 ENOBUFS No buffer space available Output queue is full. 68 Table 10. 21. send, sendto, and sendmsg Error Messages.
The Connectionless Paradigm Other than read, there are three system calls comparable to send for receiving data from a socket descriptor. These calls are recv, recvfrom, and recvmsg. Unless otherwise specified (such as with fcntl), these calls will block if no message has arrived at the socket. (Table 10. 22) 69
The Connectionless Paradigm Include File(s) <netinet/in. h> Summary int recv(int s, void *buf, size_t len, int flags); int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); int recvmsg(int s, struct msghdr *msg, int flags); Return Manual Section Success Number of bytes received 2 Failure Sets errno -1 Yes Table 10. 22. Summary of the recv, recvfrom, and recvmsg System Calls. 70
The Connectionless Paradigm Flag Meaning MSG_ERRQUEUE Receive error messages from the error message queue. The details of how to implement error message retrieval is beyond the scope of this text (see the manual page on recv for specifics). MSG_NOSIGNAL With a stream socket, do not raise a SIGPIPE error when the other end of the socket disappears. MSG_OOB Message out of band. At present this flag is valid only for Internet streambased sockets. Specifying MSG_OOB allows the process to read urgent out-of-band data. MSG_PEEK Look at the current data but do not consume it. Subsequent read-receive type calls will retrieve the same peeked-at data. MSG_TRUNC With datagram socket, return the real length of the message even if it exceeds specified amount. MSG_WAITALL Wait until the full request for data has been satisfied. 71 Table 10. 23. Flags for the recv, recvfrom, and recvmsg Calls.
The Connectionless Paradigm # Constant Perror Message Explanation A signal was received by process before data was received. 4 EINTR Interrupted system call 9 EBADF Bad file descriptor 11 EAGAIN Resource temporarily unavailable 14 EFAULT Bad address Argument references a location outside user address space. 22 EINVAL Invalid argument An argument contains incorrect value. 88 ENOTSOCK Socket operation on non-socket The socket argument is a file descriptor, not a socket descriptor. 107 ENOTCONN Transport endpoint is not connected A connection-oriented socket has not been connected. 111 ECONNREFUSED Connection refused 72 The socket reference is invalid. • The socket is set to non-blocking, and no connections are pending. • Timer expired before data was received. Remote host has refused the connection request. Table 10. 24. recv, recvfrom, and recvmsg Error Messages
- Slides: 72