Update contrib.
1 /** @file ../include/sys/socket.h
5 /** @fn accept(int s, struct sockaddr * addr, socklen_t * addrlen)
10 The argument s is a socket that has been created with socket, bound to an address with bind and is listening for connections after a listen . The accept system call extracts the first connection request on the queue
11 of pending connections and creates a new socket which it allocates a new file
12 descriptor. The new socket inherits the state of the O_NONBLOCK property from the original socket s.
14 If no pending connections are present on the queue, and the original socket
15 is not marked as non-blocking, accept blocks the caller until a connection is present. If the original
16 socket is marked non-blocking and no pending connections are present on the
17 queue, accept returns an error as described below.
19 The accepted socket may not be used to accept more connections. The original
20 socket s remains open.
22 The argument addr is a result argument that is filled-in with the address of the
23 connecting entity as it is known to the communications layer. The exact format
24 of the addr argument is determined by the domain in which the communication
25 is occurring. A null pointer may be specified for addr if the address information is not required. In this case addrlen is not used and should also be null. Otherwise, the addrlen argument is a value-result argument: It should initially contain
26 the amount of space pointed to by addr and on return will contain the actual length (in bytes) of the
27 address returned. This call is used with connection-based socket types, currently
30 It is possible to select a socket for the purposes of doing an accept by selecting it for read.
32 For certain protocols, such as ISO or DATAKIT which require an explicit confirmation, accept can be thought of as merely dequeueing the next connection
33 request and not implying confirmation. Confirmation can be implied by a normal
34 read or write on the new file descriptor, and rejection can be implied by closing
39 #include <sys/socket.h>
40 #include <netinet/in.h>
46 struct sockaddr_in addr;
47 struct sockaddr_in ss;
48 struct sockaddr_in new_socket;
50 unsigned int addr_len;
52 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
54 addr.sin_family = AF_INET;
55 addr.sin_addr.s_addr = htonl(INADDR_ANY);
56 addr.sin_port = htons(5000);
57 bind(sock_fd,(sockaddr*)&addr;,sizeof(addr));
59 newsock_fd = accept(sock_fd,(sockaddr*)&new;_socket,&addr;_len); // Code blocks here
69 @return The call returns -1 on error.
70 If it succeeds, it returns a non-negative
71 integer that is a descriptor for the accepted socket.
82 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
89 /** @fn bind(int s, const struct sockaddr *addr, socklen_t addrlen)
95 assigns the local protocol address to a socket.
96 When a socket is created
97 with socket it exists in an address family space but has no protocol address assigned.
98 The bind system call requests that addr be assigned to the socket.
102 #include <sys/socket.h>
103 #include <netinet/in.h>
108 struct sockaddr_in addr,ss;
111 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
113 addr.sin_family = AF_INET;
114 addr.sin_addr.s_addr = htonl(INADDR_ANY);
115 addr.sin_port = htons(5000);
116 bind(sock_fd,(struct sockaddr*)&addr;,sizeof(addr));
124 For maximum portability always initialise the socket address structure to
125 zero before populating it and passing it to bind.
127 @return The bind() function returns the value 0 if successful; otherwise the
128 value -1 is returned and the global variable errno is set to indicate the
140 @externallyDefinedApi
143 /** @fn connect(int s, const struct sockaddr *name, socklen_t namelen)
149 The s argument is a socket. If it is of type SOCK_DGRAM name specifies the peer with which the socket is to be associated. It
150 specifiies the address to which datagrams are to be sent and the only address
151 from which datagrams are to be received.
153 If the socket is of type SOCK_STREAM, this call attempts to make a connection to another socket.
154 The other socket is specified by name, which is an address in the communications space of the socket.
155 Each communications space interprets the name argument in its own way.
157 Generally, stream sockets may successfully connect only once and datagram sockets may use connect multiple times to change their association. Datagram sockets
158 may dissolve the association by connecting to an invalid address, such as a
163 #include <sys/socket.h>
164 #include <netinet/in.h>
168 struct sockaddr_in serv_addr;
170 serv_addr.sin_family = AF_INET;
171 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
172 serv_addr.sin_port = htons(5000);
173 sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
174 connect(sock_fd,(struct sockaddr*)&serv;_addr,sizeof(serv_addr));
180 @return The connect() function returns the value 0 if successful; otherwise it returns
181 the value -1 and the sets global variable errno to indicate the error.
191 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
194 @externallyDefinedApi
198 /** @fn getpeername(int s, struct sockaddr * name, socklen_t * namelen)
202 @return The getpeername() function returns the value 0 if successful; otherwise
203 the value -1 is returned and the global variable errno is set to indicate
206 The getpeername system call
207 returns the name of the peer connected to
208 socket s. The namelen argument should be initialized to indicate
209 the amount of space pointed to by name. On return it contains the actual size of the name
211 The name is truncated if the buffer provided is too small.
215 #include <sys/socket.h>
216 #include <netinet/in.h>
222 struct sockaddr_in addr;
223 struct sockaddr_in ss;
224 struct sockaddr_in new_socket;
226 unsigned int addr_len;
228 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
230 addr.sin_family = AF_INET;
231 addr.sin_addr.s_addr = htonl(INADDR_ANY);
232 addr.sin_port = htons(5000);
233 bind(sock_fd,(struct sockaddr*)&addr;,sizeof(addr));
235 newsock_fd = accept(sock_fd,(struct sockaddr*)&new;_socket,&addr;_len); // Code blocks here
237 // Assuming client has connected to the server.
239 getpeername(sock_fd,(struct sockaddr*)&ss;,&len;);
254 @externallyDefinedApi
258 /** @fn getsockname(int s, struct sockaddr * name, socklen_t * namelen)
262 @return The getsockname() function returns the value 0 if successful; otherwise
263 the value -1 is returned and the global variable errno is set to indicate
266 The getsockname system call
267 returns the current name for the specified socket.
268 The namelen argument should be initialized to indicate
269 the amount of space pointed to by name. On return it contains the actual size of the name
274 #include <sys/socket.h>
275 #include <netinet/in.h>
280 struct sockaddr_in addr,ss;
283 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
285 addr.sin_family = AF_INET;
286 addr.sin_addr.s_addr = htonl(INADDR_ANY);
287 addr.sin_port = htons(5000);
288 bind(sock_fd,(struct sockaddr*)&addr;,sizeof(addr));
291 getsockname(sock_fd,(struct sockaddr*)&ss;,&len;);
304 @externallyDefinedApi
307 /** @fn getsockopt(int s, int level, int optname, void * optval, socklen_t * optlen)
313 Note: This description also covers the following functions -
316 @return Upon successful completion, the value 0 is returned; otherwise the
317 value -1 is returned and the global variable errno is set to indicate the
320 The getsockopt and setsockopt system calls manipulate the options associated with a socket. Options may exist at multiple protocol levels; they are always present at the uppermost "socket" level.
321 When manipulating socket options the level at which the option resides and the name of the option must be specified. To manipulate options at the socket level, level is specified as SOL_SOCKET. To manipulate options at any other level the protocol number of the appropriate protocol controlling the option is supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP;
323 The optval and optlen arguments are used to access option values for setsockopt. For getsockopt they identify a buffer in which the value for the requested option(s) are to be returned. For getsockopt, optlen is a value-result argument, initially containing the size of the buffer pointed to by optval, and modified on return to indicate the actual size of the value returned. If no option value is to be supplied or returned, optval may be NULL.
325 The optname argument and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. The include file \#include \<sys/socket.h\>contains definitions for socket level options, described below. Options at other protocol levels vary in format and name; consult the appropriate entries in section 4 of the manual.
326 Most socket-level options utilize an int argument for optval. For setsockopt, the argument should be non-zero to enable a boolean option, or zero if the option is to be disabled. SO_LINGER uses a struct linger argument, defined in
328 #include <sys/socket.h,> which specifies the desired state of the option and the linger interval (see below). SO_SNDTIMEO and SO_RCVTIMEO use a struct timeval argument, defined in
329 #include <sys/time.h.>
330 The following options are recognized at the socket level. Except as noted, each may be examined with getsockopt and set with setsockopt. SO_DEBUG enables recording of debugging information
331 SO_REUSEADDR Allows a socket to be bound to an local address that is already in use.
332 SO_REUSEPORT enables duplicate address and port bindings
333 SO_KEEPALIVE enables keep connections alive
334 SO_DONTROUTE enables routing bypass for outgoing messages
335 SO_BROADCAST enables permission to transmit broadcast messages (enable only)
336 SO_OOBINLINE enables reception of out-of-band data in band
337 SO_SNDBUF set buffer size for output
338 SO_RCVBUF set buffer size for input
339 SO_SNDLOWAT set minimum count for output
340 SO_RCVLOWAT set minimum count for input
341 SO_SNDTIMEO set timeout value for output
342 SO_RCVTIMEO set timeout value for input
343 SO_ACCEPTFILTER set accept filter on listening socket
344 SO_TYPE get the type of the socket (get only)
345 SO_ERROR get and clear error on the socket (get only)
350 SO_DEBUG enables debugging in the underlying protocol modules. SO_REUSEADDR indicates that the rules used in validating addresses supplied in a bind system call should allow reuse of local addresses. SO_REUSEPORT allows completely duplicate bindings by multiple processes if they all set SO_REUSEPORT before binding the port. This option permits multiple instances of a program to each receive UDP/IP multicast or broadcast datagrams destined for the bound port. SO_KEEPALIVE enables the periodic transmission of messages on a connected socket. SO_DONTROUTE indicates that outgoing messages should bypass the standard routing facilities. Instead, messages are directed to the appropriate network interface according to the network portion of the destination address.
352 The option SO_BROADCAST requests permission to send broadcast datagrams on the socket. Broadcast was a privileged operation in earlier versions of the system. With protocols that support out-of-band data, the SO_OOBINLINE option requests that out-of-band data be placed in the normal data input queue as received; it will then be accessible with recv or read calls without the MSG_OOB flag. Some protocols always behave as if this option is set. SO_SNDBUF and SO_RCVBUF are options to adjust the normal buffer sizes allocated for output and input buffers, respectively. The buffer size may be increased for high-volume connections, or may be decreased to limit the possible backlog of incoming data. The system places an absolute maximum on these values, which is accessible through the sysctl MIB variable "kern.ipc.maxsockbuf."
354 SO_SNDLOWAT is an option to set the minimum count for output operations. Most output operations process all of the data supplied by the call, delivering data to the protocol for transmission and blocking as necessary for flow control. Nonblocking output operations will process as much data as permitted subject to flow control without blocking, but will process no data if flow control does not allow the smaller of the low water mark value or the entire request to be processed. A select operation testing the ability to write to a socket will return true only if the low water mark amount could be processed. The default value for SO_SNDLOWAT is set to a convenient size for network efficiency, often 1024. SO_RCVLOWAT is an option to set the minimum count for input operations. In general, receive calls will block until any (non-zero) amount of data is received, then return with the smaller of the amount available or the amount requested. The default value for SO_RCVLOWAT is 1. If SO_RCVLOWAT is set to a larger value, blocking receive calls normally wait until they have received the smaller of the low water mark value or the requested amount.
356 SO_SNDTIMEO is an option to set a timeout value for output operations. It accepts a struct timeval argument with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if no data were sent. In the current implementation, this timer is restarted each time additional data are delivered to the protocol, implying that the limit applies to output portions ranging in size from the low water mark to the high water mark for output. SO_RCVTIMEO is an option to set a timeout value for input operations. It accepts a struct timeval argument with the number of seconds and microseconds used to limit waits for input operations to complete. In the current implementation, this timer is restarted each time additional data are received by the protocol, and thus the limit is in effect an inactivity timer. If a receive operation has been blocked for this much time without receiving additional data, it returns with a short count or with the error EWOULDBLOCK if no data were received.
358 SO_ACCEPTFILTER places an accept_filter on the socket, which will filter incoming connections on a listening stream socket before being presented for accept. Once more, listen must be called on the socket before trying to install the filter on it, or else the setsockopt system call will fail.
360 SO_LINGER option is not supported by this api.
363 struct accept_filter_arg {
371 The optval argument should point to a struct accept_filter_arg that will select and configure the accept_filter. The af_name argument should be filled with the name of the accept filter that the application wishes to place on the listening socket. The optional argument af_arg can be passed to the accept filter specified by af_name to provide additional configuration options at attach time. Passing in an optval of NULL will remove the filter.
373 Finally, SO_TYPE and SO_ERROR are options used only with getsockopt. SO_TYPE returns the type of the socket, such as SOCK_STREAM; it is useful for servers that inherit sockets on startup. SO_ERROR returns any pending error on the socket and clears the error status. It may be used to check for asynchronous errors on connected datagram sockets or for other asynchronous errors.
380 #include <sys/socket.h>
381 #include <netinet/in.h>
387 unsigned int optlen = sizeof(optval);
389 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
390 setsockopt(sock_fd,SOL_SOCKET,SO_KEEPALIVE,&optval;,optlen);
391 getsockopt(sock_fd,SOL_SOCKET,SO_KEEPALIVE,(void*)&rdoptval;,&optlen;);
403 @externallyDefinedApi
406 /** @fn listen(int fd, int n)
409 @return The listen() function returns the value 0 if successful; otherwise the
410 value -1 is returned and the global variable errno is set to indicate the
413 To accept connections a socket is first created with socket . A willingness to accept incoming connections
414 and a queue limit for incoming connections are specified with listen and then the connections are accepted with accept . The listen system call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.
416 The n argument defines the maximum length the queue of pending connections
417 may grow to. The real maximum queue length will be 1.5 times more than the value
418 specified in the n argument. A subsequent listen system call on the listening socket allows the caller to change
419 the maximum queue length using a new n argument. If a connection request arrives with the queue full
420 the client may receive an error with an indication of ECONNREFUSED , or, in the case of TCP, the connection will be silently
423 Note that before BSD 4.5 and the introduction of the syncache the n argument also determined the length of the incomplete connection
424 queue, which held TCP sockets in the process of completing TCP's 3-way
425 handshake. These incomplete connections are now held entirely in the syncache,
426 which is unaffected by queue lengths. Inflated n values to help handle denial of service attacks are no longer
433 #include <sys/socket.h>
434 #include <netinet/in.h>
436 void listen_example()
440 struct sockaddr_in addr;
441 struct sockaddr_in ss;
442 struct sockaddr_in new_socket;
444 unsigned int addr_len;
446 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
448 addr.sin_family = AF_INET;
449 addr.sin_addr.s_addr = htonl(INADDR_ANY);
450 addr.sin_port = htons(5000);
451 bind(sock_fd,(struct sockaddr*)&addr;,sizeof(addr));
465 @externallyDefinedApi
468 /** @fn recv(int fd, void *buf, size_t cnt, int flags)
474 Note: This description also covers the following functions -
477 @return These calls return the number of bytes received, or -1
478 if an error occurred.
481 MSG_OOB process out-of-band data
484 The recvfrom and recvmsg system calls
485 are used to receive messages from a socket,
486 and may be used to receive data on a socket whether or not
487 it is connection-oriented.
489 If from is not a null pointer
490 and the socket is not connection-oriented,
491 the source address of the message is filled in.
493 is a value-result argument, initialized to the size of
494 the buffer associated with from, and modified on return to indicate the actual size of the
495 address stored there.
497 The recv function is normally used only on a connected socket (see connect )
498 and is identical to recvfrom with a
499 null pointer passed as its from argument.
500 As it is redundant, it may not be supported in future releases.
502 All three routines return the length of the message on successful
504 If a message is too long to fit in the supplied buffer,
505 excess bytes may be discarded depending on the type of socket
506 the message is received from (see socket )
508 If no messages are available at the socket, the
509 receive call waits for a message to arrive, unless
510 the socket is nonblocking (see fcntl )
511 in which case the value
512 -1 is returned and the external variable errno set to EAGAIN. The receive calls normally return any data available,
513 up to the requested amount,
514 rather than waiting for receipt of the full amount requested;
515 this behavior is affected by the socket-level options SO_RCVLOWAT and SO_RCVTIMEO described in getsockopt .
517 The select system call may be used to determine when more data arrive.
519 The flags argument to a recv function is formed by or Ap ing one or more of the values: MSG_OOB process out-of-band data MSG_PEEK peek at incoming message MSG_WAITALL wait for full request or error MSG_DONTWAIT do not block
521 The MSG_OOB flag requests receipt of out-of-band data
522 that would not be received in the normal data stream.
523 Some protocols place expedited data at the head of the normal
524 data queue, and thus this flag cannot be used with such protocols.
525 The MSG_PEEK flag causes the receive operation to return data
526 from the beginning of the receive queue without removing that
528 Thus, a subsequent receive call will return the same data.
529 The MSG_WAITALL flag requests that the operation block until
530 the full request is satisfied.
531 The MSG_DONTWAIT flag requests the call to return when it would block otherwise.
532 If no data is available, errno is set to EAGAIN.
534 The flags MSG_OOB, MSG_PEEK, MSG_WAITALL and MSG_DONTWAIT are not supported for
535 local sockets(AF_LOCAL, AF_LINUX etc..).
537 The recvmsg system call uses a msghdr structure to minimize the number of directly supplied arguments.
538 This structure has the following form, as defined in \#include \<sys/socket.h\>
541 caddr_tmsg_name;/* optional address */
542 u_intmsg_namelen;/* size of address */
543 structiovec *msg_iov;/* scatter/gather array */
544 u_intmsg_iovlen;/* # elements in msg_iov */
545 caddr_tmsg_control;/* ancillary data, see below */
546 u_intmsg_controllen; /* ancillary data buffer len */
547 intmsg_flags;/* flags on received message */
551 Here msg_name and msg_namelen specify the destination address if the socket is unconnected; msg_name may be given as a null pointer if no names are desired or required.
552 The msg_iov and msg_iovlen arguments
553 describe scatter gather locations, as discussed in read .
554 The msg_control argument,
555 which has length msg_controllen, points to a buffer for other protocol control related messages
556 or other miscellaneous ancillary data.
557 The messages are of the form:
560 u_intcmsg_len;/* data byte count, including hdr */
561 intcmsg_level;/* originating protocol */
562 intcmsg_type;/* protocol-specific type */
564 u_charcmsg_data[]; */
568 As an example, one could use this to learn of changes in the data-stream
569 in XNS/SPP, or in ISO, to obtain user-connection-request data by requesting
570 a recvmsg with no data buffer provided immediately after an accept system call.
572 Open file descriptors are now passed as ancillary data for AF_UNIX domain sockets, with cmsg_level set to SOL_SOCKET and cmsg_type set to SCM_RIGHTS.
574 Process credentials can also be passed as ancillary data for AF_UNIX domain sockets using a cmsg_type of SCM_CREDS. In this case, cmsg_data should be a structure of type cmsgcred, which is defined in \#include \<sys/socket.h\> as follows:
577 pid_tcmcred_pid;/* PID of sending process */
578 uid_tcmcred_uid;/* real UID of sending process */
579 uid_tcmcred_euid;/* effective UID of sending process */
580 gid_tcmcred_gid;/* real GID of sending process */
581 shortcmcred_ngroups;/* number or groups */
582 gid_tcmcred_groups[CMGROUP_MAX];/* groups */
586 The kernel will fill in the credential information of the sending process
587 and deliver it to the receiver.
589 The msg_flags field is set on return according to the message received. MSG_EOR indicates end-of-record; the data returned completed a record
590 (generally used with sockets of type SOCK_SEQPACKET). MSG_TRUNC indicates that the trailing portion of a datagram was discarded
591 because the datagram was larger than the buffer supplied. MSG_CTRUNC indicates that some control data were discarded due to
592 lack of space in the buffer for ancillary data. MSG_OOB is returned to indicate that expedited or out-of-band data
597 #include <sys/socket.h>
598 #include <netinet/in.h>
602 struct sockaddr_in serv_addr;
606 serv_addr.sin_family = AF_INET;
607 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
608 serv_addr.sin_port = htons(5000);
609 sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
610 connect(sock_fd,(struct sockaddr*)&serv;_addr,sizeof(serv_addr));
611 recv(sock_fd, line, size, 0);
617 #include <sys/socket.h>
618 #include <netinet/in.h>
622 struct sockaddr_in sender_addr;
624 char line[15] = "Hello World!";
625 unsigned int size = sizeof(sender_addr);
626 sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
627 sender_addr.sin_family = AF_INET;
628 sender_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
629 sender_addr.sin_port = htons(5000);
630 recvfrom(sock_fd,line,13,0,(struct sockaddr*)&sender;_addr,&size;);
636 #include <sys/socket.h>
637 #include <netinet/in.h>
639 void SendMsgRecvMsg()
642 unsigned int sender_len;
645 struct sockaddr_in receiver_addr,sender_addr;
647 sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
648 receiver_addr.sin_family = AF_INET;
649 receiver_addr.sin_addr.s_addr = htonl(INADDR_ANY);
650 receiver_addr.sin_port = htons(5000);
651 bind(sock_fd,(struct sockaddr*)&receiver;_addr,sizeof(receiver_addr));
652 sender_len = sizeof(sender_addr);
653 msg.msg_name = &sender;_addr;
654 msg.msg_namelen = sender_len;
657 msg.msg_iov->iov_base = line;
658 msg.msg_iov->iov_len = 10;
660 msg.msg_controllen = 0;
662 recvmsg(sock_fd,&msg;,0);
675 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
678 @externallyDefinedApi
682 /** @fn recvfrom(int s, void * buf, size_t len, int flags, struct sockaddr * from, socklen_t * fromlen)
690 Refer to recv() for the documentation
700 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
703 @externallyDefinedApi
707 /** @fn recvmsg(int fd, struct msghdr *message, int flags)
712 Refer to recv() for the documentation
722 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
725 @externallyDefinedApi
728 /** @fn send(int fd, const void *buf, size_t cnt, int flags)
734 Note: This description also covers the following functions -
738 and sendto and sendmsg system calls
739 are used to transmit a message to another socket.
741 may be used only when the socket is in a connected state, while sendto and sendmsg may be used at any time.
743 The address of the target is given by to with tolen specifying its size.
744 The length of the message is given by len. If the message is too long to pass atomically through the
745 underlying protocol, the error EMSGSIZE is returned, and
746 the message is not transmitted.
748 No indication of failure to deliver is implicit in a send. Locally detected errors are indicated by a return value of -1.
750 If no messages space is available at the socket to hold
751 the message to be transmitted, then send normally blocks, unless the socket has been placed in
752 non-blocking I/O mode.
753 The select system call may be used to determine when it is possible to
756 The flags argument may include one or more of the following:
758 #defineMSG_OOB0x00001 //process out-of-band data
759 #defineMSG_PEEK0x00002 // peek at incoming message
760 #defineMSG_DONTROUTE0x00004 // bypass routing, use direct interface
761 #define MSG_EOR0x00008 // data completes record
762 #defineMSG_EOF0x00100 // data completes transaction
765 The flag MSG_OOB is used to send "out-of-band"
766 data on sockets that support this notion (e.g. SOCK_STREAM); the underlying protocol must also support "out-of-band"
767 data. MSG_EOR is used to indicate a record mark for protocols which support the
768 concept. MSG_EOF requests that the sender side of a socket be shut down, and that an
769 appropriate indication be sent at the end of the specified data;
770 this flag is only implemented for SOCK_STREAM sockets in the PF_INET protocol family, and is used to implement Transaction TCP MSG_DONTROUTE is usually used only by diagnostic or routing programs.
772 See recv for a description of the msghdr structure.
776 #include <sys/socket.h>
777 #include <netinet/in.h>
781 struct sockaddr_in serv_addr;
783 char line[15] = "Hello world!";
785 serv_addr.sin_family = AF_INET;
786 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
787 serv_addr.sin_port = htons(5000);
788 sock_fd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
789 connect(sock_fd,(struct sockaddr*)&serv;_addr,sizeof(serv_addr));
790 send(sock_fd, line, size, 0);
796 #include <sys/socket.h>
797 #include <netinet/in.h>
801 sockaddr_in receiver_addr;
803 char line[15] = "Hello World!";
804 sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
805 receiver_addr.sin_family = AF_INET;
806 receiver_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
807 receiver_addr.sin_port = htons(5000);
808 sendto(sock_fd, line, 13, 0,(struct sockaddr*)&receiver;_addr,sizeof(receiver_addr));
814 #include <sys/socket.h>
815 #include <netinet/in.h>
819 struct sockaddr_in receiver_addr;
821 char line[15] = "Hello World!";
824 sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
826 receiver_addr.sin_family = AF_INET;
827 receiver_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
828 receiver_addr.sin_port = htons(5000);
829 msg.msg_name = &receiver;_addr;
830 msg.msg_namelen = sizeof(receiver_addr);
833 msg.msg_iov->iov_base = line;
834 msg.msg_iov->iov_len = 13;
836 msg.msg_controllen = 0;
838 sendmsg(sock_fd,&msg;,0);
843 @return This function call returns the number of characters sent; otherwise the
844 value -1 is returned and the global variable errno is set to indicate the
857 Because sendmsg does not necessarily block until the data has been transferred, it
858 is possible to transfer an open file descriptor across an AF_UNIX domain socket
859 (see recv then close it before it has actually been sent, the result being that the receiver
860 gets a closed file descriptor.
861 It is left to the application to
862 implement an acknowledgment mechanism to prevent this from happening.
864 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
867 @externallyDefinedApi
870 /** @fn sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
878 Refer to send() for the documentation
889 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
892 @externallyDefinedApi
895 /** @fn sendmsg(int fd, const struct msghdr *message, int flags)
900 Refer to send() for the documentation
911 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
914 @externallyDefinedApi
918 /** @fn setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
925 Refer to getsockopt() for the documentation
931 Note:For multicast to work on Symbian OS the connection for the interface needs to be started first.
932 On most of the desktop Operating Systems,the network interface is always running, so things like setsockopt() will always pass.
933 But on Symbian, the interface is not always running (in order to save battery and/or data charges) so some options in setsockopt() will not work until the connection is started.
938 @externallyDefinedApi
942 /** @fn shutdown(int fd, int how)
945 @return The shutdown() function returns the value 0 if successful; otherwise the
946 value -1 is returned and the global variable errno is set to indicate the
950 SHUT_RD further receives will be disallowed.
951 SHUT_WR further sends will be disallowed.
953 further sends and receives will be disallowed.
956 The shutdown system call causes all or part of a full-duplex connection on
957 the socket associated with the file descriptor sockfd to be shut down.
958 The how argument specifies the type of shutdown.
959 Possible values are: SHUT_RD further receives will be disallowed. SHUT_WR further sends will be disallowed. SHUT_RDWR further sends and receives will be disallowed.
963 #include <sys/socket.h>
964 #include <netinet/in.h>
966 TInt shutdown_example()
972 sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
974 addr.sin_family = AF_INET;
975 addr.sin_addr.s_addr = htonl(INADDR_ANY);
976 addr.sin_port = htons(5000);
977 bind(sock_fd,(sockaddr*)&addr;,sizeof(addr));
978 shutdown(sock_fd, SHUT_RD)
990 @externallyDefinedApi
993 /** @fn sockatmark(int s)
995 @return Upon successful completion, the sockatmark function returns the value 1 if the read pointer is pointing at
996 the OOB mark, 0 if it is not.Otherwise the value -1 is returned
997 and the global variable errno is set to indicate the error.
999 To find out if the read pointer is currently pointing at
1000 the mark in the data stream, the sockatmark function is provided.
1001 If sockatmark returns 1, the next read will return data
1003 Otherwise (assuming out of band data has arrived),
1004 the next read will provide data sent by the client prior
1005 to transmission of the out of band signal.
1007 in the remote login process to flush output on receipt of an
1008 interrupt or quit signal is shown below.
1009 It reads the normal data up to the mark (to discard it),
1010 then reads the out-of-band byte.
1012 #include <sys/socket.h>
1015 int out = FWRITE, mark;
1017 /* flush local terminal output */
1018 ioctl(1, TIOCFLUSH, (char *)&out;);
1020 if ((mark = sockatmark(rem)) < 0) {
1021 perror("sockatmark");
1026 (void) read(rem, waste, sizeof (waste));
1028 if (recv(rem, &mark;, 1, MSG_OOB) < 0) {
1038 #include <sys/socket.h>
1039 #include <netinet/in.h>
1044 sockaddr_in selfAddr;
1045 sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
1047 selfAddr.sin_family = AF_INET;
1048 selfAddr.sin_addr.s_addr = INADDR_ANY;
1049 selfAddr.sin_port = htons(5000);
1050 bind(sockfd,(struct sockaddr*)&selfAddr;, sizeof(selfAddr));
1064 @externallyDefinedApi
1068 /** @fn socket(int family, int style, int protocol)
1072 @return The socket() function returns valid socket descriptor if successful; otherwise the
1073 value -1 is returned and the global variable errno is set to indicate the
1076 The socket system call
1077 creates an endpoint for communication and returns a descriptor.
1079 The family argument specifies a communications domain within which communication
1080 will take place; this selects the protocol family which should be used. These
1081 families are defined in the include file \#include \<sys/socket.h\> The currently understood formats
1084 PF_LOCALHost-internal protocols, formerly called PF_UNIX,
1085 PF_INETInternet version 4 protocols,
1087 The socket has the indicated style, which specifies the semantics of communication.
1093 SOCK_STREAMStream socket,
1094 SOCK_DGRAMDatagram socket,
1095 SOCK_SEQPACKETSequenced packet stream
1099 A SOCK_STREAM type provides sequenced, reliable,
1100 two-way connection based byte streams.
1101 An out-of-band data transmission mechanism may be supported.
1102 A SOCK_DGRAM socket supports
1103 datagrams (connectionless, unreliable messages of
1104 a fixed (typically small) maximum length).
1105 A SOCK_SEQPACKET socket may provide a sequenced, reliable,
1106 two-way connection-based data transmission path for datagrams
1107 of fixed maximum length; a consumer may be required to read
1108 an entire packet with each read system call.
1109 This facility is protocol specific, and presently unimplemented.
1111 The protocol argument
1112 specifies a particular protocol to be used with the socket.
1113 Normally only a single protocol exists to support a particular
1114 socket type within a given protocol family.
1115 However, it is possible
1116 that many protocols may exist, in which case a particular protocol
1117 must be specified in this manner.
1118 The protocol number to use is
1119 particular to the "communication domain"
1120 in which communication
1123 Sockets of type SOCK_STREAM are full-duplex byte streams, similar
1125 A stream socket must be in a connected state before any data may be sent or received
1127 A connection to another socket is created with a connect system call.
1128 Once connected, data may be transferred using read and write calls or some variant of the send and recv functions.
1129 (Some protocol families, such as the Internet family,
1130 support the notion of an "implied connect,"
1131 which permits data to be sent piggybacked onto a connect operation by
1132 using the sendto system call.)
1133 When a session has been completed a close may be performed.
1134 Out-of-band data may also be transmitted as described in send and received as described in recv
1136 The communications protocols used to implement a SOCK_STREAM insure that data
1137 is not lost or duplicated.
1138 If a piece of data for which the
1139 peer protocol has buffer space cannot be successfully transmitted
1140 within a reasonable length of time, then
1141 the connection is considered broken and calls
1142 will indicate an error with
1143 -1 returns and with ETIMEDOUT as the specific code
1144 in the global variable errno. The protocols optionally keep sockets "warm"
1145 by forcing transmissions
1146 roughly every minute in the absence of other activity.
1147 An error is then indicated if no response can be
1148 elicited on an otherwise
1149 idle connection for an extended period (e.g. 5 minutes).
1151 SOCK_SEQPACKET sockets employ the same system calls as SOCK_STREAM sockets. The only difference is that read calls will return only the amount of data
1152 requested and any remaining in the arriving packet will be discarded.
1154 SOCK_DGRAM sockets allow sending of datagrams to correspondents named
1155 in send calls. Datagrams are generally received
1156 with recvfrom which returns the next datagram with
1161 #include <sys/socket.h>
1164 #inlcude <netinet/in.h>
1165 void SocketExample()
1168 sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1190 @capability Deferred @ref RSocket::Open(RSocketServ &,TUint,TUint,TUint)
1193 @externallyDefinedApi
1197 /** @typedef typedef __sa_family_t sa_family_t
1202 @externallyDefinedApi
1205 /** @typedef typedef __socklen_t socklen_t
1207 Socket address length type.
1210 @externallyDefinedApi
1213 /** @struct sockaddr
1215 Structure used by kernel to store most addresses.
1218 @externallyDefinedApi
1221 /** @var sockaddr::sa_len
1225 /** @var sockaddr::sa_family
1229 /** @var sockaddr::sa_data
1230 actually longer; address value
1235 Header for ancillary data objects in msg_control buffer.
1236 Used for additional information with/about a datagram not expressible by flags.
1237 The format is a sequence of message elements headed by cmsghdr structures.
1240 @externallyDefinedApi
1243 /** @var cmsghdr::cmsg_len
1244 data byte count, including hdr
1247 /** @var cmsghdr::cmsg_level
1248 originating protocol
1251 /** @var cmsghdr::cmsg_type
1252 protocol-specific type
1258 Message header for recvmsg and sendmsg calls.
1259 Used value-result for recvmsg, value only for sendmsg.
1262 @externallyDefinedApi
1265 /** @var msghdr::msg_name
1269 /** @var msghdr::msg_namelen
1273 /** @var msghdr::msg_iov
1274 scatter or gather array
1277 /** @var msghdr::msg_iovlen
1278 x elements in msg_iov
1281 /** @var msghdr::msg_control
1282 ancillary data, see below
1285 /** @var msghdr::msg_controllen
1286 ancillary data buffer len
1289 /** @var msghdr::msg_flags
1290 flags on received message
1293 /** @def CMSG_DATA(cmsg)
1295 given pointer to struct cmsghdr, return pointer to data
1298 @externallyDefinedApi
1301 /** @def CMSG_NXTHDR(mhdr, cmsg)
1303 given pointer to struct cmsghdr, return pointer to next cmsghdr
1306 @externallyDefinedApi
1310 /** @def CMSG_FIRSTHDR(mhdr)
1312 RFC 2292 requires to check msg_controllen, in case that the kernel returns an empty list for some reasons.
1315 @externallyDefinedApi
1320 Structure used for manipulating linger option.
1323 @externallyDefinedApi
1326 /** @var linger::l_onoff
1330 /** @var linger::l_linger
1337 local to host (pipes, portals)
1340 @externallyDefinedApi
1345 internetwork: UDP, TCP, etc.
1348 @externallyDefinedApi
1353 Address family. Unspecified.
1356 @externallyDefinedApi
1361 shut down the reading side
1364 @externallyDefinedApi
1369 shut down the writing side
1372 @externallyDefinedApi
1377 shut down both sides
1380 @externallyDefinedApi
1386 process out-of-band data
1389 @externallyDefinedApi
1394 peek at incoming message
1397 @externallyDefinedApi
1400 /** @def MSG_DONTROUTE
1402 send without using routing tables
1405 @externallyDefinedApi
1410 data completes record
1413 @externallyDefinedApi
1418 data discarded before delivery
1421 @externallyDefinedApi
1426 control data lost before delivery
1429 @externallyDefinedApi
1432 /** @def MSG_WAITALL
1434 wait for full request or error
1437 @externallyDefinedApi
1440 /** @def SO_ACCEPTCONN
1442 socket has had listen()
1445 @externallyDefinedApi
1448 /** @def SO_BROADCAST
1450 permit sending of broadcast msgs
1453 @externallyDefinedApi
1459 turn on debugging info recording KSODebug
1462 @externallyDefinedApi
1465 /** @def SO_DONTROUTE
1467 just use interface addresses
1470 @externallyDefinedApi
1475 get error status and clear
1478 @externallyDefinedApi
1481 /** @def SO_KEEPALIVE
1483 keep connections alive KSoTcpKeepAlive
1486 @externallyDefinedApi
1491 linger on close if data present
1494 @externallyDefinedApi
1497 /** @def SO_OOBINLINE
1499 leave received OOB data in line KSoTcpOobInline
1502 @externallyDefinedApi
1508 receive buffer size KSORecvBuf
1511 @externallyDefinedApi
1514 /** @def SO_RCVLOWAT
1516 receive low-water mark
1519 @externallyDefinedApi
1522 /** @def SO_RCVTIMEO
1527 @externallyDefinedApi
1530 /** @def SO_REUSEADDR
1532 Allow a socket to be bound to an local address that is already in use.
1535 @externallyDefinedApi
1541 send buffer size KSOSendBuf
1544 @externallyDefinedApi
1547 /** @def SO_SNDLOWAT
1552 @externallyDefinedApi
1555 /** @def SO_SNDTIMEO
1560 @externallyDefinedApi
1569 @externallyDefinedApi
1577 @externallyDefinedApi
1580 /** @def SOCK_STREAM
1585 @externallyDefinedApi
1588 /** @def SOCK_SEQPACKET
1590 sequenced packet stream
1593 @externallyDefinedApi
1599 options for socket level KSOLSocket
1602 @externallyDefinedApi
1608 raw-protocol interface