tcp.c File Reference

#include "tcp.h"
#include <netdb.h>
#include <netinet/in.h>
#include <fcntl.h>

Go to the source code of this file.

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.


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