Main Page   Compound List   File List   Header Files   Sources   Compound Members   File Members  

netapi.c File Reference

Network API - implementation. More...


Functions

void initErrorCodes ()
int openSocket (int type)
This function creates a socket of type TCP or UDP. More...

bool closeSocket (int sockDescr)
This functions closes the socket given by the socket descriptor. More...

bool bind (int sockDescr, unsigned int localPort)
Bind a name to a socket. More...

bool connect (int sockDescr, char* IPAddress, unsigned int remotePort)
Establish a connection to a given IP address on a given port over the socket given by the socket descriptor. More...

int TCPSend (int sockDescr, byte* dataBuffer, int length, int flags)
Sends a TCP message to anothertransport end-point. More...

int TCPReceive (int sockDescr, byte* dataBuffer, int length, int flags, unsigned long timeout)
Receive message from another socket. More...

bool TCPAbortConnection (int sockDescr)
Abort a connection on a socket. More...

bool TCPCloseLinger (int sockDescr, unsigned int seconds)
Sets linger time on close in seconds. More...

bool TCPSocketReuse (int sockDescr)
Sets reuse option on a listening socket. More...

int UDPSend (int sockDescr, char* IPAddress, unsigned int remotePort, byte* dataBuffer, int length, int flags, bool convertPort)
Sends UDP datagrams. More...

int UDPReceive (int sockDescr, byte* dataBuffer, int length, int flags, unsigned long timeout)
Receives UDP datagrams. More...

bool listen (int sockDescr, int connections)
Place the socket in passive mode and set the number of incomimg TCP connections the system will enque. More...

int accept (int sockDescr)
Accept the next incoming connection from the queue of pending connections from the listen function and creates a new socket. More...

int waitingBytes (int sockDescr)
Get the number of bytes on a socket, waiting for read. More...

clientInfoPtr getClientInfo ()
You'll get a pointer to clientInfo struct which contains information about the connection to the client the server is connected. More...

errorInfoPtr getErrorInfo ()
This function returns a pointer to the struct errorInfo. More...

int getErrorCode ()
This function returns the error code of the last API call. More...

void inet2ascii (unsigned long far* IP, char far* IPAddress)
Converts an unsigned long IP address to a dotted decimal IP string The buffer must have the length of 17 Bytes. More...

bool ascii2inet (char far* IPAddress, unsigned long far* IP)
Converts an dotted decimal IP-String to an unsigned long. More...

void sleep (unsigned int mseconds)
Sleeps for mseconds milliseconds. More...

bool evalError (union REGS* outregs)
Evaluate errors. More...


Variables

struct errorInfo errInf
errorInfoPtr errInfp
struct clientInfo cInf
clientInfoPtr cInfp
char IPAddr [17]
char* errorCodes [266]
char* specialCode
int errorCodesInit


Detailed Description

Network API - implementation.

@author: Christian Decker (cdecker@teco.edu)

Development System: Turbo C++ 3.0

Licence: GPL

This API was developed for easy handling the TCPIP/UDP functions of the SC12 from Beck GmbH. The code was inspired from snippets which I found in the examples delivered with the original API descriptions from Beck.

This API contains functions for opening, closing sockets, binding them, connection, sending and receiving data through them, listen on particular ports and some converting routines for IP-Adressen to network byte order. These functions not only for TCP sockets, they are for UDP sockets, too.

See also:
http://www.beck-ipc.com/ , http://www.teco.edu/~cdecker/ , cdecker@teco.edu

Definition in file netapi.c.


Function Documentation

void initErrorCodes () [static]

Definition at line 44 of file netapi.c.

int openSocket (int type)

This function creates a socket of type TCP or UDP.

It returns the socket descriptor.

Parameters:
type   is one of TCP or UDP
Returns:
a socket descriptor or 0 if there was an error
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 80 of file netapi.c.

bool closeSocket (int sockDescr)

This functions closes the socket given by the socket descriptor.

Parameters:
sockDescr   the socket descriptor
Returns:
a value greater 0 if successful, otherwise 0.
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 95 of file netapi.c.

bool bind (int sockDescr, unsigned int localPort)

Bind a name to a socket.

This functions uses the local address. You can specify the local port the socket is bind to.

Parameters:
sockDescr   a socket descriptor
localPort   the local port the socket is bind to
Returns:
a value greater 0 if successful, otherwise 0.
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 111 of file netapi.c.

bool connect (int sockDescr, char * IPAddress, unsigned int remotePort)

Establish a connection to a given IP address on a given port over the socket given by the socket descriptor.

@sockDescr a socket descriptor @IPAddress a char pointer to a IP address to connect to @remotePort the port on the remote computer to connect to

Returns:
a value greater 0 if successful, otherwise 0.
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 139 of file netapi.c.

int TCPSend (int sockDescr, byte * dataBuffer, int length, int flags)

Sends a TCP message to anothertransport end-point.

Can only be used if the socket is in a connected state.

Parameters:
sockDescr   a socket descriptor
dataBuffer   a pointer to an array of bytes which should be sent
length   number of bytes to send
flags   is one of MSG_BLOCKING, MSG_DONTWAIT
Returns:
number of bytes sent
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 176 of file netapi.c.

int TCPReceive (int sockDescr, byte * dataBuffer, int length, int flags, unsigned long timeout)

Receive message from another socket.

Can only be used if the socket is in a connected state.

Parameters:
sockDescr   a socket descriptor
dataBuffer   a pointer to an array of bytes where received bytes are saved
length   number of bytes to receive
flags   is one of MSG_BLOCKING, MSG_TIMEOUT, MSG_DONTWAIT
timeout   timeout in milliseconds - only usedif flags = MSG_TIMEOUT
Returns:
number of bytes received
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 207 of file netapi.c.

bool TCPAbortConnection (int sockDescr)

Abort a connection on a socket.

Works only with TCP sockets.

Parameters:
sockDescr   a socket descriptor
Returns:
greater 0 if successful or 0 otherwise
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 243 of file netapi.c.

bool TCPCloseLinger (int sockDescr, unsigned int seconds)

Sets linger time on close in seconds.

Works only with TCP sockets.

Parameters:
sockDescr   a socket descriptor
seconds   linger time in seconds (0 means linger off)
Returns:
greater 0 if successful or 0 otherwise
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 259 of file netapi.c.

bool TCPSocketReuse (int sockDescr)

Sets reuse option on a listening socket.

This is necessary, if a listening socket was closed and will be open and bind on the same port as it was bind before. Works only with TCP sockets.

Parameters:
sockDescr   a socket descriptor
Returns:
greater 0 if successful or 0 otherwise
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 278 of file netapi.c.

int UDPSend (int sockDescr, char * IPAddress, unsigned int remotePort, byte * dataBuffer, int length, int flags, bool convertPort)

Sends UDP datagrams.

It only works with an UDP socket.

Parameters:
sockDescr   a socket descriptor
IPAddress   a char pointer to the dotted IP address
remotePort   the port the UPD msg will be sent to
dataBuffer   a pointer to an array of bytes where the sending bytes are
length   number of bytes to send
flags   is one of MSG_BLOCKING, MSG_DONTWAIT
convertPort   greater 0 if UDPSend should convert remotePort into network byte order, 0 means it doesn't convert
Returns:
number of bytes sent
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 294 of file netapi.c.

int UDPReceive (int sockDescr, byte * dataBuffer, int length, int flags, unsigned long timeout)

Receives UDP datagrams.

It only works with an UDP socket. It sets up the information about the client (ip and port in network byte order)

Parameters:
sockDescr   a socket descriptor
dataBuffer   a pointer to an array of bytes where received bytes are written
length   number of bytes to receive
flags   is one of MSG_BLOCKING, MSG_TIMEOUT, MSG_DONTWAIT
timeout   timeout in milliseconds - only used if flags = MSG_TIMEOUT
Returns:
number of bytes received
See also:
getErrorInfo(), getErrorCode() for information about errors and clientInfo for information about the client

Definition at line 345 of file netapi.c.

bool listen (int sockDescr, int connections)

Place the socket in passive mode and set the number of incomimg TCP connections the system will enque.

This call is used by a TCP server.

Parameters:
sockDescr   a socket descriptor @connections the max. number (limited to 5) of allowed outstanding connections
Returns:
returns greater than 0 if successful or 0 otherwise
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 388 of file netapi.c.

int accept (int sockDescr)

Accept the next incoming connection from the queue of pending connections from the listen function and creates a new socket.

It sets up the information about the client (ip and port in network byte order)

Parameters:
sockDescr   a socket descriptor from listen function
Returns:
the new socket descriptor for this connection
See also:
listen(), getErrorInfo() and getErrorCode() for information about errors and clientInfo for information about the client

Definition at line 407 of file netapi.c.

int waitingBytes (int sockDescr)

Get the number of bytes on a socket, waiting for read.

Parameters:
sockDescr   a socket descriptor
See also:
getErrorInfo(), getErrorCode() for information about errors

Definition at line 437 of file netapi.c.

clientInfoPtr getClientInfo ()

You'll get a pointer to clientInfo struct which contains information about the connection to the client the server is connected.

The port in this structure is in network byte order.

Returns:
a pointer to the clientInfo struct
See also:
clientInfo for connection information

Definition at line 453 of file netapi.c.

errorInfoPtr getErrorInfo ()

This function returns a pointer to the struct errorInfo.

You can use this pointer to ask for the error code of the last API call and for the appropriate description to this code.

Returns:
a pointer to the errorInfo struct
See also:
errorInfo

Definition at line 458 of file netapi.c.

int getErrorCode ()

This function returns the error code of the last API call.

It uses getErrorInfo and is for simplifying the error handling.

Returns:
error code of the last API call

Definition at line 463 of file netapi.c.

void inet2ascii (unsigned long far * IP, char far * IPAddress)

Converts an unsigned long IP address to a dotted decimal IP string The buffer must have the length of 17 Bytes.

Parameters:
IP   a far pointer to the unsigned long IP
IPAddress   a far char pointer to the buffer where the IP string is saved (must be 17 chars)

Definition at line 468 of file netapi.c.

bool ascii2inet (char far * IPAddress, unsigned long far * IP)

Converts an dotted decimal IP-String to an unsigned long.

Parameters:
IPAddress   a far pointer to a dotted decimal IP-String
IP   a far pointer to the IP in network byte order
Returns:
returns greater 0 if successful or 0 otherwise

Definition at line 483 of file netapi.c.

void sleep (unsigned int mseconds)

Sleeps for mseconds milliseconds.

Parameters:
mseconds   time to sleep in milliseconds

Definition at line 504 of file netapi.c.

bool evalError (union REGS * outregs)

Evaluate errors.

This is used by all API calls for evaluation of the errors. It writes into struct errorInfo the error code and an error description.

Parameters:
a   pointer to the register after the API call
Returns:
a value greater than 0 if there was an error, 0 otherwise

Attention: users should not call this function. It's only for the functions of the API.

Definition at line 514 of file netapi.c.


Variable Documentation

struct errorInfo errInf [static]

Definition at line 32 of file netapi.c.

errorInfoPtr errInfp

Definition at line 33 of file netapi.c.

struct clientInfo cInf [static]

Definition at line 36 of file netapi.c.

clientInfoPtr cInfp

Definition at line 37 of file netapi.c.

char IPAddr[17] [static]

Definition at line 38 of file netapi.c.

char* errorCodes[266] [static]

Definition at line 40 of file netapi.c.

char* specialCode [static]

Definition at line 41 of file netapi.c.

int errorCodesInit [static]

Definition at line 42 of file netapi.c.


Generated at Fri Jun 9 20:55:35 2000 for NETAPI by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999