Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ARM).Porting TCP-IP programmer's guide.Ver 1.6.pdf
Скачиваний:
43
Добавлен:
23.08.2013
Размер:
2.64 Mб
Скачать

Sockets

5.2.8t_listen()

This function tells the socket library that socket is going to be used for accepting connections from other hosts.

Syntax

extern int t_listen(long socket, int backlog)

where:

socket Is the socket used for accepting connections.

backlog Defines the maximum length to which the queue of pending connections can grow. If a connection request arrives when the queue is full, the client receives the error message ECONNREFUSED.

Return value

Returns one of the following:

0

If successful.

–1

If not successful. The internal socket variable errno is set to one of the

 

errors listed in ipport.h. The value of errno can be retrieved by a call

 

to t_errno(socket).

Usage

To accept connections:

1.A socket is created with t_socket().

2.A backlog for incoming connections is specified with t_listen().

3.The connections are then accepted with t_accept().

The t_listen() call applies only to SOCK_STREAM sockets.

ARM DUI 0144B

Copyright © 1998-2001 ARM Limited. All rights reserved.

5-13

Sockets

5.2.9t_recv() and t_recvfrom()

These functions are used to receive messages from another socket.

Syntax

int t_recv(long socket, char *buffer, int length, int flags)

int t_recvfrom(long socket, char *buffer, int length, int flags, struct sockaddr *from)

where:

 

 

socket

Is the identifier of the socket from which the messages are received. The

 

socket is created with t_socket().

buffer

Is the received message. If a message is too long to fit in buffer, excess

 

bytes may be discarded depending on the type of socket from which the

 

message is received.

 

length

Is the length of buffer in bytes.

flags

Is formed by ORing zero or more of the following:

 

MSG_OOB

Reads any out-of-band data present on the socket,

 

 

rather than the regular in-band data.

 

MSG_PEEK

Looks at the data present on the socket. The data is

 

 

returned, but not consumed, so a subsequent receive

 

 

operation will see the same data.

from

Is either NULL, or points to a struct sockaddr that will be filled in by

 

t_recvfrom() with the source address of the message.

Return value

Returns one of the following:

number

The number of bytes received, if successful.

–1

If not successful. The internal socket variable errno is set to one of the

 

errors listed in ipport.h. The value of errno can be retrieved by a call

 

to t_errno(socket).

5-14

Copyright © 1998-2001 ARM Limited. All rights reserved.

ARM DUI 0144B

Sockets

Usage

You can only use the t_recv() function on a connected socket (see t_connect()). The t_recvfrom() function can be used to receive data on a socket, whether it is in a connected state or not.

If no messages are available at the socket and the socket is blocking, the receive call waits for a message to arrive. If the socket is nonblocking (see t_setsockopt() on page 5-19), –1 is returned, with the external socket errno set to EWOULDBLOCK.

You can use the t_select() function to determine when more data arrives.

ARM DUI 0144B

Copyright © 1998-2001 ARM Limited. All rights reserved.

5-15

Sockets

5.2.10t_select()

This function examines the input/output descriptor sets (whose addresses are passed in readfds, writefds, and exceptfds) to see if some of their descriptors are ready for reading, ready for writing, or have an exceptional condition pending. On return, t_select() replaces the given descriptor sets with subsets consisting of the descriptors that are ready for the requested operation. The total number of ready descriptors in all the sets is returned.

Syntax

int t_select(fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long timeout)

readfds Is the set of socket descriptors to be tested for available data to be read.

writefds Is the set of socket descriptors to be tested for available buffer space for write operations.

exceptfds Is the set of socket descriptors to be tested for pending exceptional conditions (if out-of-band data is available to be read).

timeout Is the wait interval in cticks clock ticks. If timeout is neither 0 nor –1, it specifies the maximum number of clock ticks (at TPS ticks per second) to wait for the selection to complete. If timeout is zero, t_select() modifies the descriptor sets to indicate which are ready for the requested operation, and returns immediately. If timeout is –1, t_select() blocks until at least one of the requested operations is ready, and there is no timeout.

Return value

Returns one of the following:

+value

A positive value indicates the number of ready descriptors in the

 

descriptor sets.

0

Indicates that the time limit referred to by timeout has expired.

–1

If not successful.

5-16

Copyright © 1998-2001 ARM Limited. All rights reserved.

ARM DUI 0144B

Sockets

Usage

You can give the parameters readfds, writefds, and exceptfds as NULL pointers if no descriptors are of interest.

To determine if a call to t_accept() will return immediately, call t_select(), passing the socket as a member of the readfds set.

Note

Under rare circumstances, t_select() can indicate that a descriptor is ready for writing when, in fact, an attempt to write would block. This can happen if system resources necessary for a write are subsequently exhausted after the select has returned or are otherwise unavailable. If an application deems it critical that writes to a socket descriptor do not block, it should set the descriptor for nonblocking input/output using the SO_NBIO request to t_setsockopt().

The descriptors are stored within the fd_set structures as opaque objects. The macros below are provided for manipulating such structures.

The behavior of these macros is undefined if an invalid descriptor value is passed.

FD_ZERO

FD_ZERO(fd_set *fdset)

 

This macro initializes a descriptor set fdset to the null set.

FD_SET

FD_SET(long fd, fd_set *fdset)

 

This macro includes a particular socket descriptor fd in fdset.

FD_CLR

FD_CLR(long fd, fd_set *fdset)

 

This macro removes fd from fdset.

FD_ISSET

FD_ISSET(long fd, fd_set *fdset)

 

This macro is nonzero if fd is a member of fdset, and zero otherwise.

ARM DUI 0144B

Copyright © 1998-2001 ARM Limited. All rights reserved.

5-17