Data Structures | Files | Defines | Functions | Variables

EGnet

Data Structures

struct  EGsocket_t
 structure for socket More...

Files

file  eg_net.c
file  eg_net.ex.c
file  eg_net.h

Defines

#define BOSS   1
#define CLIENT   2
#define EG_NET_ALLOW_NON_BLOCKING   0
 Set to one to enable non-blocking calls, zero to disable them. What happen in that case is that non-blocking calls behave as regular blocking calls.
#define EG_NET_CONFIRM   0
 If set to one, check that the information send was properly received, this may slow down the code by a significant factor, and also may fix synchronization issues without enabling EG_NET_SYNCHRONIZE.
#define EG_NET_DATA_QUEUE_SIZE   131072U
 minimum length of the data queue in any socket connection. By default it is set to 64Kb
#define EG_NET_LISTEN_QUEUE_SIZE   16384U
 Minimum length of the incomming connection queue at the listening socket.
#define EG_NET_MARKER   '5'
#define EG_NET_RECEIVE   1
#define EG_NET_SEND   0
#define EG_NET_SYNCHRONIZE   0
 If set to one, use synchronization among sender and receiver.
#define EG_NET_TCPIP_OVERHEAD   128
 Overhead of a MAC/TCP/IP package over the data part of the packet, note that the MAC header is about 14 bytes long, that IP has an overhead of 20 bytes plus 4 bytes for options and padding. TCP other 20 bytes plus up to 44 bytes for options and padding, thus the overhead is between 58 and 102 bytes. Then to send x bytes across a TCP/IP connection, we actually send x bytes + [58-102] bytes. This overhead is the constant that we define bellow. For details on the structure of the MAC/IP/TCP headers, see the TCP/IP description. Note also that we shouldn't send packets of length above 572 bytes, thus the actual data length should be less than 470 bytes.
#define EGnetClearSocket(__lskt)
 clear a socket structure and leave it ready to be freed
#define EGnetInitSocket(skt)   memset(skt,0,sizeof(EGsocket_t))
 Initialize a socket structure.
#define EGnetSendInt(skt, n)   EGnetSendUint(skt,(unsigned) n)
 send an int through a connected socket
#define EGnetSendInt(skt, n)   EGnetSendUint(skt,(unsigned) n)
#define EGnetSendShort(skt, n)   EGnetSendUshort(skt,(unsigned)n)
#define EGnetSendShort(skt, n)   EGnetSendUshort(skt,(unsigned)n)
 send a short through a connected socket
#define EGnetSynchronize(skt, type, length)
 Assure that the data queue don't overflow by using syncronizations calls between both ends of the connections.
#define KILL   3

Functions

int EGnetConnect (EGsocket_t *const skt, const char *host_name, unsigned port)
 try to stablish a remote connection
int EGnetDisconnect (EGsocket_t *const skt)
 close an established connection
int EGnetListen (EGsocket_t *const skt, unsigned p)
 set a socket to wait for connections. Used as the server side of the connection
int EGnetRecvChar (EGsocket_t *const skt, char *const c)
 recieve a char through a connected socket
int EGnetRecvDouble (EGsocket_t *const skt, double *const d)
 recieve a double through a connected socket. The current implementation has some limits, it tries to represent the number $ d = 2^ex $ where $0.5\leq|x|<1$, unfortunatelly, $ |e|\leq 128 $, and numbers that can't be represented in this form won't be transmitted in a right way.
int EGnetRecvInt (EGsocket_t *const skt, int *n)
 recieve an int through a connected socket
int EGnetRecvShort (EGsocket_t *const skt, int *n)
 recieve a short through a connected socket
int EGnetRecvString (EGsocket_t *const skt, char *const str, size_t max_size)
 recieve a string through a connected socket, the string must have allocated memory and its maximum size be max_size (including the '\0' char)
int EGnetRecvUint (EGsocket_t *const skt, unsigned int *n)
 recieve an unsigned int through a connected socket
int EGnetRecvUshort (EGsocket_t *const skt, unsigned *n)
 recieve a unsigned short through a connected socket
int EGnetSendChar (EGsocket_t *const skt, const int c)
 send a char through a connected socket
int EGnetSendDouble (EGsocket_t *const skt, const double d)
 send a double through a connected socket. The current implementation has some limits, it tries to represent the number $ d = 2^ex $ where $0.5\leq|x|<1$, unfortunatelly, $ |e|\leq 128 $, and numbers that can't be represented in this form won't be transmitted in a right way.
int EGnetSendString (EGsocket_t *const skt, const char *const str)
 send a string through a connected socket, it will send up to the '\0' char at the end of the string (including it).
int EGnetSendUint (EGsocket_t *const skt, unsigned int n)
 send an unsigned int through a connected socket
int EGnetSendUshort (EGsocket_t *const skt, unsigned n)
 send a unsigned short through a connected socket
int EGnetStartRead (EGsocket_t *const skt)
 accept an incomming connection from another program/host, if no connection is on the queue, the program will block until one is found.
int EGnetStartReadNB (EGsocket_t *const skt)
 accept an incomming connection from another program/host. If no connection is on the queue (of incomming connections), the program will return EAGAIN, otherwise it return zero on success and non zero (1) in error.
int EGnetStopRead (EGsocket_t *const skt)
 close an established connection with another program/host
int main (int argc, char **argv)
static void net_usage (char *program)
static int parseargs (int argc, char **argv)

Variables

static char * hname = 0
static int mode = 0
static unsigned int nchars = 0
static unsigned port = 0

Detailed Description

This header contain the definitions to make basic client/server communications. The protocol used to comunicate is TCP/IP. and the type of communication is permanent (within each cycle connect/disconnect start_read/stop_read).

Version:
0.9.1
History:
  • 2005-08-30
    • A different aproach is to always send back and forth the information (so that if debugging is enabled, we get confirmation that the data was sent right), the drawback is that the performance fall by a factor of two, but on the other hand, the previous fix had a degradation factor of 8 (in bytes per second)... so it seems that this approach is betteer and more stable, still waiting for further tests.
  • 2005-08-29
    • When sending lot's of information during a single connection, the internal send/receive data queue may overflow, to avoid this, we add a low level syncronization step every EG_NET_DATA_QUEUE_SIZE bits.
    • Add EGnetClearSocket and EGnetInitSocket functions.
  • 2005-06-14
    • Take out the non-blocking versions (with a define) because it won't run on some machines, but according to the ISO definitions it should.... it's just the way it goes.
    • Add flag to disable non-blocking calls.
  • 2005-02-09
    • Fix EGnetSendDouble, error comprises values < 0.5
  • 2004-09-10
    • Renan added some compatibility modifications to run this library on windows.
  • 2004-01-14
    • first implementation/

Define Documentation

#define BOSS   1
Examples:
eg_net.ex.c.

Definition at line 39 of file eg_net.ex.c.

Referenced by main().

#define CLIENT   2
Examples:
eg_net.ex.c.

Definition at line 40 of file eg_net.ex.c.

Referenced by main(), and parseargs().

#define EG_NET_ALLOW_NON_BLOCKING   0

Set to one to enable non-blocking calls, zero to disable them. What happen in that case is that non-blocking calls behave as regular blocking calls.

Definition at line 124 of file eg_net.h.

#define EG_NET_CONFIRM   0

If set to one, check that the information send was properly received, this may slow down the code by a significant factor, and also may fix synchronization issues without enabling EG_NET_SYNCHRONIZE.

Definition at line 73 of file eg_net.h.

#define EG_NET_DATA_QUEUE_SIZE   131072U

minimum length of the data queue in any socket connection. By default it is set to 64Kb

Definition at line 97 of file eg_net.h.

#define EG_NET_LISTEN_QUEUE_SIZE   16384U

Minimum length of the incomming connection queue at the listening socket.

Definition at line 102 of file eg_net.h.

#define EG_NET_MARKER   '5'

Definition at line 333 of file eg_net.c.

#define EG_NET_RECEIVE   1

Definition at line 332 of file eg_net.c.

#define EG_NET_SEND   0

Definition at line 331 of file eg_net.c.

#define EG_NET_SYNCHRONIZE   0

If set to one, use synchronization among sender and receiver.

Definition at line 77 of file eg_net.h.

#define EG_NET_TCPIP_OVERHEAD   128

Overhead of a MAC/TCP/IP package over the data part of the packet, note that the MAC header is about 14 bytes long, that IP has an overhead of 20 bytes plus 4 bytes for options and padding. TCP other 20 bytes plus up to 44 bytes for options and padding, thus the overhead is between 58 and 102 bytes. Then to send x bytes across a TCP/IP connection, we actually send x bytes + [58-102] bytes. This overhead is the constant that we define bellow. For details on the structure of the MAC/IP/TCP headers, see the TCP/IP description. Note also that we shouldn't send packets of length above 572 bytes, thus the actual data length should be less than 470 bytes.

Definition at line 91 of file eg_net.h.

#define EGnetClearSocket (   __lskt  ) 
Value:
({\
  EGsocket_t*const _EGskt = (EGsocket_t*)(__lskt);\
  if(_EGskt->s_fd) close(_EGskt->s_fd);\
  if(_EGskt->f_fd) close(_EGskt->f_fd);\
  memset(_EGskt,0,sizeof(EGsocket_t));\
  0;})

clear a socket structure and leave it ready to be freed

Definition at line 132 of file eg_net.h.

#define EGnetInitSocket (   skt  )     memset(skt,0,sizeof(EGsocket_t))

Initialize a socket structure.

Examples:
eg_net.ex.c.

Definition at line 128 of file eg_net.h.

Referenced by main().

#define EGnetSendInt (   skt,
  n 
)    EGnetSendUint(skt,(unsigned) n)

send an int through a connected socket

Definition at line 212 of file eg_net.h.

#define EGnetSendInt (   skt,
  n 
)    EGnetSendUint(skt,(unsigned) n)
Examples:
eg_net.ex.c.

Definition at line 530 of file eg_net.c.

Referenced by main().

#define EGnetSendShort (   skt,
  n 
)    EGnetSendUshort(skt,(unsigned)n)
Examples:
eg_net.ex.c.

Definition at line 466 of file eg_net.c.

Referenced by main().

#define EGnetSendShort (   skt,
  n 
)    EGnetSendUshort(skt,(unsigned)n)

send a short through a connected socket

Definition at line 193 of file eg_net.h.

#define EGnetSynchronize (   skt,
  type,
  length 
)

Assure that the data queue don't overflow by using syncronizations calls between both ends of the connections.

Parameters:
skt socket where we are working.
type either EG_NET_SEND or EG_NET RECEIVE.
length bytes being transfered.

Definition at line 381 of file eg_net.c.

#define KILL   3
Examples:
eg_net.ex.c.

Definition at line 41 of file eg_net.ex.c.

Referenced by main().


Function Documentation

int EGnetConnect ( EGsocket_t *const   skt,
const char *  host_name,
unsigned  port 
)

try to stablish a remote connection

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetDisconnect ( EGsocket_t *const   skt  ) 

close an established connection

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetListen ( EGsocket_t *const   skt,
unsigned  p 
)

set a socket to wait for connections. Used as the server side of the connection

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvChar ( EGsocket_t *const   skt,
char *const   c 
)

recieve a char through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvDouble ( EGsocket_t *const   skt,
double *const   d 
)

recieve a double through a connected socket. The current implementation has some limits, it tries to represent the number $ d = 2^ex $ where $0.5\leq|x|<1$, unfortunatelly, $ |e|\leq 128 $, and numbers that can't be represented in this form won't be transmitted in a right way.

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvInt ( EGsocket_t *const   skt,
int *  n 
)

recieve an int through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvShort ( EGsocket_t *const   skt,
int *  n 
)

recieve a short through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvString ( EGsocket_t *const   skt,
char *const const   exstr,
size_t  max_size 
)

recieve a string through a connected socket, the string must have allocated memory and its maximum size be max_size (including the '\0' char)

int EGnetRecvUint ( EGsocket_t *const   skt,
unsigned int *  n 
)

recieve an unsigned int through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetRecvUshort ( EGsocket_t *const   skt,
unsigned *  n 
)

recieve a unsigned short through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetSendChar ( EGsocket_t *const   skt,
const int  c 
)

send a char through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetSendDouble ( EGsocket_t *const   skt,
const double  exd 
)

send a double through a connected socket. The current implementation has some limits, it tries to represent the number $ d = 2^ex $ where $0.5\leq|x|<1$, unfortunatelly, $ |e|\leq 128 $, and numbers that can't be represented in this form won't be transmitted in a right way.

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetSendString ( EGsocket_t *const   skt,
const char *const const   exstr 
)

send a string through a connected socket, it will send up to the '\0' char at the end of the string (including it).

int EGnetSendUint ( EGsocket_t *const   skt,
unsigned int  n 
)

send an unsigned int through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetSendUshort ( EGsocket_t *const   skt,
unsigned  n 
)

send a unsigned short through a connected socket

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetStartRead ( EGsocket_t *const   skt  ) 

accept an incomming connection from another program/host, if no connection is on the queue, the program will block until one is found.

Examples:
eg_net.ex.c.

Referenced by main().

int EGnetStartReadNB ( EGsocket_t *const   skt  ) 

accept an incomming connection from another program/host. If no connection is on the queue (of incomming connections), the program will return EAGAIN, otherwise it return zero on success and non zero (1) in error.

int EGnetStopRead ( EGsocket_t *const   skt  ) 

close an established connection with another program/host

Examples:
eg_net.ex.c.

Referenced by main().

int main ( int  argc,
char **  argv 
)
static void net_usage ( char *  program  )  [static]
Examples:
eg_net.ex.c.

Definition at line 27 of file eg_net.ex.c.

Referenced by parseargs().

static int parseargs ( int  argc,
char **  argv 
) [static]
Examples:
eg_net.ex.c.

Definition at line 46 of file eg_net.ex.c.

References CLIENT, hname, MESSAGE, mode, nchars, net_usage(), and port.

Referenced by main().

Here is the call graph for this function:


Variable Documentation

char* hname = 0 [static]
Examples:
eg_net.ex.c.

Definition at line 44 of file eg_net.ex.c.

Referenced by main(), and parseargs().

int mode = 0 [static]
Examples:
eg_net.ex.c, and eg_slk.ex.c.

Definition at line 45 of file eg_net.ex.c.

Referenced by main(), and parseargs().

unsigned int nchars = 0 [static]
Examples:
eg_net.ex.c.

Definition at line 42 of file eg_net.ex.c.

Referenced by main(), and parseargs().

unsigned port = 0 [static]
Examples:
eg_net.ex.c.

Definition at line 43 of file eg_net.ex.c.

Referenced by main(), and parseargs().