tcp.h File Reference


Detailed Description

The public interface to the TCP library.

This file contains the public interface to libtcp. For documentation of everything, including things meant only for internal use, see tcp.c.

An example TCP client is provided in client::c. An example server is provided in server::c.

Definition in file tcp.h.

#include <sys/types.h>
#include <sys/socket.h>

Go to the source code of this file.

Defines

#define TCP_CONNECTION   TCP_SOCKET
#define TCP_ERROR   -1
#define tcp_getpeername   getpeername
#define tcp_getsockname   getsockname
#define tcp_recv   recv
#define tcp_send   send
#define tcp_shutdown   shutdown
#define TCP_SOCKET   int

Functions

TCP_CONNECTION tcp_accept (TCP_SOCKET sock)
 Accept an incoming connection.
int tcp_close (TCP_CONNECTION conn)
 Close a TCP connection.
TCP_CONNECTION tcp_connect (const char *host, int port)
 Make a TCP connection to the specified host on the specified port.
TCP_SOCKET tcp_listen (const char *host, int port)
 Listen for incoming connections.
int tcp_read (TCP_CONNECTION conn, void *buf, int len)
 Read from a TCP connection.
int tcp_setblocking (TCP_CONNECTION conn, int blocking)
 Make a connection blocking on nonblocking.
int tcp_write (TCP_CONNECTION conn, const void *buf, int len)
 Write to a TCP connection.


Define Documentation

#define TCP_CONNECTION   TCP_SOCKET

Definition at line 18 of file tcp.h.

#define TCP_ERROR   -1

Definition at line 15 of file tcp.h.

#define tcp_getpeername   getpeername

Definition at line 28 of file tcp.h.

#define tcp_getsockname   getsockname

Definition at line 29 of file tcp.h.

#define tcp_recv   recv

Definition at line 30 of file tcp.h.

#define tcp_send   send

Definition at line 31 of file tcp.h.

#define tcp_shutdown   shutdown

Definition at line 32 of file tcp.h.

#define TCP_SOCKET   int

Definition at line 14 of file tcp.h.


Function Documentation

TCP_CONNECTION tcp_accept ( TCP_SOCKET  sock  ) 

Accept an incoming connection.

Parameters:
sock The socket to accept the connection on.
Returns:
The new connection.

Definition at line 236 of file tcp.c.

References TCP_ERROR.

int tcp_close ( TCP_CONNECTION  conn  ) 

Close a TCP connection.

Parameters:
conn The connection to close.
Returns:
0 on success. Any other value indicates failure.

Definition at line 116 of file tcp.c.

TCP_CONNECTION tcp_connect ( const char *  host,
int  port 
)

Make a TCP connection to the specified host on the specified port.

Parameters:
host The host (name or IP address) of the machine to connect to.
port The port number to connect to.
Returns:
The new TCP_CONNECTION on success; TCP_ERROR on failure.

Definition at line 82 of file tcp.c.

References tcp_close(), and TCP_ERROR.

TCP_SOCKET tcp_listen ( const char *  host,
int  port 
)

Listen for incoming connections.

Parameters:
host The address to listen on. Use NULL to listen on all local adresses.
port The port to listen on.
Returns:
A new TCP_SOCKET on success, TCP_ERROR on failure.

Definition at line 200 of file tcp.c.

References tcp_close(), and TCP_ERROR.

int tcp_read ( TCP_CONNECTION  conn,
void *  buf,
int  len 
)

Read from a TCP connection.

Up to len bytes are read and placed into buf. Note that the number of bytes read may be less than len.

Parameters:
conn The connection to read from.
buf The buffer to place the bytes into.
len The maximum number of bytes to read; typically equal to the size of buf.
Returns:
The number of bytes actually read on success; a negative value on failure. A return value of 0 indicates the end of the transmission.

Definition at line 137 of file tcp.c.

References tcp_recv.

int tcp_setblocking ( TCP_CONNECTION  conn,
int  blocking 
)

Make a connection blocking on nonblocking.

Blocking affects what happens when a read is performed on a connection when there are not currently any bytes available to be read. A blocking connection will block the read until bytes are available. A non-blocking connection will signal an error and set errno to EAGAIN.

A blocking connection should be used when you want to read bytes from the connection and, if none are currently available, wait until they are.

Non-blocking connections should be used when you want to read bytes from a connection, but, if none are currently available, do something else instead.

Parameters:
conn The connection to adjust.
blocking 0 to make the connection blocking, any other value to make it nonblocking.
Returns:
0 on success, TCP_ERROR on failure.

Definition at line 177 of file tcp.c.

References TCP_ERROR.

int tcp_write ( TCP_CONNECTION  conn,
const void *  buf,
int  len 
)

Write to a TCP connection.

Writes up to len bytes from buf to the connection conn. Note that fewer than len bytes may be written. Always check the return value, which indicates the number of bytes actually written.

Parameters:
conn The connection to write to.
buf The buffer containing the bytes to be written.
len The maximum number of bytes to write.
Returns:
The number of bytes written on success; a negative value on failure.

Definition at line 153 of file tcp.c.

References tcp_send.


Generated on Tue Dec 25 09:41:11 2007 for libtcp by  doxygen 1.5.1