gtpc1m4h | Transmission Control Protocol/Internet Protocol |
The write function writes data on a socket with descriptor
s. The write function applies only to connected
sockets.
Format
#include <socket.h>
int write(int s,
char *buf,
int len);
- s
- The socket descriptor.
- buf
- Pointer to the buffer holding the data to be written.
- len
- Length of the message pointed to by the msg parameter.
When using TCP/IP offload support:
- The maximum send buffer size is 32 767 bytes when the
SO_SNDBUF option of the setsockopt function is used to
increase the send buffer size.
- The default maximum size is 28 672 bytes.
- The maximum size for datagram sockets is 32 000 bytes when the
SO_SNDBUF option of the setsockopt function is used to
increase the send buffer size.
- The default maximum size for datagram sockets is 9216 bytes.
- The length cannot be larger than the maximum send buffer size for this
socket, which is defined by the SO_SNDBUF option of the
setsockopt function.
When using TCP/IP native stack support:
- The maximum send buffer size is 1 048 576
bytes.
- The default value of the SO_SNDBUF option is
32 767.
- For a TCP socket, the maximum length that you can specify is 1 GB.
- For a UDP or RAW socket, the maximum length that you can specify is the
smaller of the following values:
- 32 KB
- The send buffer size defined by the SO_SNDBUF option.
Normal Return
If it succeeds, the function returns the number of bytes.
Error Return
A return code equal to -1 indicates an error. You can get the
specific error code by calling sock_errno. See Appendix C, Socket Error Return Codes for more information about socket errors.
- Note:
- Unless otherwise stated in the description, the following error codes can be
returned for either TCP/IP offload support or TCP/IP native stack
support.
- Value
- Description
- SOCFAULT
- Using buf and len results in an attempt to access a
protected address space. This error code is returned only for TCP/IP
offload support.
- SOCNOBUFS
- Buffer space is not available to send the message.
- SOCNOTSOCK
- The s parameter is not a valid socket descriptor.
- SOCINVAL
- An invalid length was specified.
- SOCWOULDBLOCK
- The s parameter is in nonblocking mode and no buffer space is
available to hold the message to be sent.
- SOCNOTCONN
- The socket is not connected.
- EIBMIUCVERR
- An error occurred when the function call was sent to the offload
device. This error code is returned only for TCP/IP offload
support.
- E1052STATE
- The socket was closed because the system was in or cycling down to 1052
state.
- EINACT
- All offload devices associated with the socket descriptor have been
disconnected. The socket is closed. This error code is returned
only for TCP/IP offload support.
- EINACTWS
- An offload device associated with the socket descriptor has been
disconnected. The socket is still available. This error code is
returned only for TCP/IP offload support.
- ESYSTEMERROR
- A system error has occurred and closed the socket.
- SOCMSGSIZE
- The message was too large to be sent. This error code is returned
only for TCP/IP native stack support.
- SOCTIMEDOUT
- The operation timed out. The socket is still available. This
error code is returned only for TCP/IP native stack support.
Programming Considerations
- This function writes up to len bytes of data.
- If there is no available buffer space to hold the socket data to be
transmitted, and the socket is in blocking mode, write blocks the
caller until additional buffer space becomes available. If the socket
is in nonblocking mode, write returns a -1 and sets
sock_errno to SOCWOULDBLOCK. See ioctl -- Perform Special Operations on Socket for a description of how to set the nonblocking
mode.
- For sockets using TCP/IP native stack support, the send timeout value (the
SO_SNDTIMEO setsockopt option) determines how long to wait for space to become
available in the send buffer before the write function times
out.
- For sockets using TCP/IP native stack support:
- For TCP sockets, if the value you specify for the len parameter
is less than or equal to the send buffer size of the socket, the send process
will be atomic; that is, either all of the data will be sent or none of
it will be sent. If all of the data is sent, the return code is set to
the value of the len parameter. If none of the data is sent,
the return code is set to -1.
- For TCP sockets, if the value you specify for the len parameter
is greater than the send buffer size of the socket, the TPF system will take
as much data as possible and return to the application indicating that only
part of the data was processed. The application must issue more send
calls for the remaining data and the application must serialize the send calls
if the socket is being shared by multiple ECBs. If the send call is
successful, the return code is set to a value from 1 to the value of the
len parameter, which indicates how much data was sent.
Examples
In the following example, a 100-byte message is written to socket
server_sock.
#include <socket.h>
·
·
·
int server_sock;
char send_msg[100];
·
·
·
if (write(server_sock,send_msg,sizeof(send_msg)) < 0)
printf("error in writing on stream socket\n");
Related Information