Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

TCP Sockets


Data Structures

struct  tcp_socket
 TCP socket information structure. More...

struct  tcp_socket
 TCP socket information structure. More...


Defines

#define SO_FIN
 \brief Send FIN after all data has been transmitted.

#define SO_SYN
 \brief Send SYN first.

#define SO_FORCE
 \brief Force sending ACK.

#define SO_ACK
 \brief Send ACK.

#define TICK_RATE

Typedefs

typedef struct tcp_socket TCPSOCKET
 TCP socket type.


Functions

void NutTcpDestroySocket (TCPSOCKET *sock)
 Destroy a previously allocated socket. More...

TCPSOCKETNutTcpFindSocket (u_short lport, u_short rport, u_long raddr)
 Find a matching socket. More...

TCPSOCKETNutTcpCreateSocket (void)
 Create a TCP socket. More...

int NutTcpConnect (TCPSOCKET *sock, u_long addr, u_short port)
 Connect to a remote socket. More...

int NutTcpAccept (TCPSOCKET *sock, u_short port)
 Wait for incoming connect from a remote socket. More...

int NutTcpSend (TCPSOCKET *sock, void *data, u_short len)
 Send data on a connected TCP socket. More...

int NutTcpReceive (TCPSOCKET *sock, void *data, u_short size)
 Receive data on a connected TCP socket. More...

int NutTcpCloseSocket (TCPSOCKET *sock)
 Close TCP socket. More...

int NutTcpError (TCPSOCKET *sock)
 Return specific code of the last error. More...


Variables

TCPSOCKETtcpSocketList

Function Documentation

int NutTcpAccept ( TCPSOCKET * sock,
u_short port )
 

Wait for incoming connect from a remote socket.

The calling thread will be suspended until until an incoming connection request is received.

This function is typically used by TCP server applications.

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket().
port   Port number to listen to (host byte order).

Returns:
0 on success, -1 otherwise. The specific error code can be retrieved by calling NutTcpError().

int NutTcpCloseSocket ( TCPSOCKET * sock )
 

Close TCP socket.

Note, that the socket is not immediately destroyed after calling this function. However, the application must not use the socket after this call.

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket().

Returns:
0 on success, -1 otherwise.

int NutTcpConnect ( TCPSOCKET * sock,
u_long addr,
u_short port )
 

Connect to a remote socket.

This function tries to establish a connection to the specified remote port of the specified remote server. The calling thread will be suspended until a connection is successfully established or an error occurs.

This function is typically used by TCP client applications.

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket().
addr   IP address of the host to connect (network byte order).
port   Port number to connect (host byte order).

Returns:
0 on success, -1 otherwise. The specific error code can be retrieved by calling NutTcpError().

TCPSOCKET* NutTcpCreateSocket ( void )
 

Create a TCP socket.

Allocates a TCPSOCKET structure from heap memory, initializes it and returns a pointer to that structure.

The first call will also start the TCP timer, which is required by various timeout checks.

Returns:
Socket descriptor of the newly created TCP socket or 0 if there is not enough memory left.

Todo:
Avoid fixed initial sequence number.

Configurable buffer size.

Allow larger maximum segment size.

void NutTcpDestroySocket ( TCPSOCKET * sock )
 

Destroy a previously allocated socket.

Remove socket from the socket list and release occupied memory.

Applications typically do not need to call this function. It is automatically called by a timer after the socket has been closed by NutTcpCloseSocket().

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket().

int NutTcpError ( TCPSOCKET * sock )
 

Return specific code of the last error.

Possible error codes (net/errno.h) are:

  • EWOULDBLOCK: Operation would block
  • EINPROGRESS: Operation now in progress
  • EALREADY: Operation already in progress
  • ENOTSOCK: Socket operation on non-socket
  • EDESTADDRREQ: Destination address required
  • EMSGSIZE: Message too long
  • EPROTOTYPE: Protocol wrong type for socket
  • ENOPROTOOPT: Protocol not available
  • EPROTONOSUPPORT: Protocol not supported
  • ESOCKTNOSUPPORT: Socket type not supported
  • EOPNOTSUPP: Operation not supported on socket
  • EPFNOSUPPORT: Protocol family not supported
  • EAFNOSUPPORT: Address family not supported by protocol family
  • EADDRINUSE: Address already in use
  • EADDRNOTAVAIL: Can't assign requested address
  • ENETDOWN: Network is down
  • ENETUNREACH: Network is unreachable
  • ENETRESET: Network dropped connection on reset
  • ECONNABORTED: Software caused connection abort
  • ECONNRESET: Connection reset by peer
  • ENOBUFS: No buffer space available
  • EISCONN: Socket is already connected
  • ENOTCONN: Socket is not connected
  • ESHUTDOWN: Can't send after socket shutdown
  • ETOOMANYREFS: Too many references: can't splice
  • ETIMEDOUT: Connection timed out
  • ECONNREFUSED: Connection refused
  • ELOOP: Too many levels of symbolic links
  • ENAMETOOLONG: File name too long
  • EHOSTDOWN: Host is down
  • EHOSTUNREACH: No route to host
  • ENOTEMPTY: Directory not empty
Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket().

Note:
Applications must not call this function to retrieve the error code if NutTcpCloseSocket() or NutTcpDestroySocket() failed.

Bug:
Not all error codes are properly set right now. Some socket functions return an error without setting an error code.

TCPSOCKET* NutTcpFindSocket ( u_short lport,
u_short rport,
u_long raddr )
 

Find a matching socket.

Loop through all sockets and find a matching connection (prefered) or a listening socket.

Applications typically do not need to call this function.

Parameters:
lport   Local port number.
rport   Remote port number.
raddr   Remote IP address in network byte order.

Returns:
Socket descriptor.

int NutTcpReceive ( TCPSOCKET * sock,
void * data,
u_short size )
 

Receive data on a connected TCP socket.

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect or NutTcpAccept.
data   Pointer to the buffer that receives the data.
size   Size of the buffer.

Returns:
If successful, the number of received data bytes is returned. This may be less than the specified size of the buffer. The return value -1 indicates an error.

int NutTcpSend ( TCPSOCKET * sock,
void * data,
u_short len )
 

Send data on a connected TCP socket.

Parameters:
sock   Socket descriptor. This pointer must have been retrieved by calling NutTcpCreateSocket(). In addition a connection must have been established by calling NutTcpConnect or NutTcpAccept.
data   Pointer to a buffer containing the data to send.
len   Number of bytes to be sent.

Returns:
If successful, the number of bytes added to the socket transmit buffer. This may be less than the specified number of bytes to send. The return value -1 indicates an error.


Variable Documentation

TCPSOCKET * tcpSocketList
 

Linked list of all TCP sockets.


© 2000-2001 by egnite Software GmbH - visit http://www.ethernut.de/