gtpc1m45Transmission Control Protocol/Internet Protocol

read -- Read Data on a Socket

Note

This description applies only to sockets. See TPF C/C++ Language Support User's Guide for more information about the read function for files.

The read function reads data on a socket with descriptor s and stores it in a buffer.

Format

#include  <socket.h>
int       read(int s,
               char *buf,
               int len);

s
The socket descriptor.

buf
Pointer to the buffer that receives the data.

len
Length, in bytes, of the buffer pointed to by the buf parameter. The maximum amount of data that can be received is 32 768 bytes.

Normal Return

If successful, the number of bytes copied into the buffer is returned. If an end-of-file condition is received or the connection is closed, 0 is returned.

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.

SOCNOTSOCK
The s parameter is not a valid socket descriptor.

SOCWOULDBLOCK
The s parameter is in nonblocking mode and no data is available to read.

SOCNOTCONN
The socket is not connected.

SOCNOBUFS
There is not enough space available to process the function call. This error code is returned only for TCP/IP offload support.

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.

SOCTIMEDOUT
The operation timed out. The socket is still available. This error code is returned only for TCP/IP native stack support.

Programming Considerations

Examples

After the server accepts a client connection, it reads in a message from its client.

#include <socket.h>

·
·
·
int rc; int newclient_sock; int server_sock; char recv_client_msg[100];
·
·
·
newclient_sock = accept(server_sock, (struct sockaddr *) 0, (int) 0); rc = read(newclient_sock,recv_client_msg,sizeof(recv_client_msg));

Related Information