Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

sockets.c File Reference

Go to the source code of this file.

Functions

int open_tcp_connexion (struct sockaddr_in *tcp_add, char *tcp_host, int tcp_port, long tt_sec, long tt_micro, unsigned long int *ellapsed_time)
 Open a TCP connexion to a remote server.

void Get_Sock_Adress (char *Machine_serveur, int port_serveur, struct sockaddr_in *TCP_address)
 Get the Internet address of a service.

int read_from_socket (int sockfd, char *buffer, size_t buffer_size, size_t *extracted, long tt_sec, long tt_micro, unsigned long int *ellapsed_time)
 Read data from an open socket descriptor (read timeout in micro seconds + ellapsed time in milli seconds).

int read_all_socket (int sockfd, char **buffer, long tt_sec, long tt_micro, unsigned long int *ellapsed_time, unsigned long int *size)
 Read from a socket, until the connexion is closed.


Detailed Description

This file implements basic TCP/IP functionalities.

Definition in file sockets.c.


Function Documentation

void Get_Sock_Adress char *    Machine_serveur,
int    port_serveur,
struct sockaddr_in *    TCP_address
 

Get the Internet address of a service.

Parameters:
Machine_serveur Host IP address.
port_serveur TCP port.
TCP_address Pointer to a "sockaddr_in" used to store the internet address.
Returns:
The function always return SCK_OK.

Definition at line 141 of file sockets.c.

Referenced by open_tcp_connexion().

int open_tcp_connexion struct sockaddr_in *    tcp_add,
char *    tcp_host,
int    tcp_port,
long    tt_sec,
long    tt_micro,
unsigned long int *    ellapsed_time
 

Open a TCP connexion to a remote server.

Parameters:
tcp_add Pointer to a structure that will receive the server's IP address. You probably don't need it, but sometimes you must have it.
tcp_host Pointer to a zero terminated string of characters that contains the name (or IP address) of the server running the host.
tcp_port TCP port used by the server to a listen incoming connections.
tt_sec Number of second for the timeout.
tt_micro Number of micro second for the timeout.
ellapsed_time Ellapsed time - in milli seconds - for the connect() system call.
Returns:
If the connection succed, the function returns the newly created socket descriptor (which is a positive value). Otherwise, if the connection failed, the function returns one of the following codes (negative value):
  • SCK_CREATE_ERROR: Can not create the socket (the function socket() failed).
  • SCK_CONNECT_ERROR: Can not connect to the remote server (the function connect() failed).
  • SCK_GET_IP_ERROR: Can not get the remote server's IP address, the function gethostbyname() failed.
  • SCK_TIMEOUT_CONNECT: The connect() call has been broken by a signal - timeout.
  • SCK_TIMER_ERROR: An error occured with the high precision timer.
  • SCK_CHRONO_ERROR: An error occured with the high precision chronometer.
Warning:
Please note that:
  • To use this function you MUST set a handler on the signal SIGALRM. The signal SIGALRM is used to implement the connect() timeout.
  • This use its own timers, so you can still use the timing library that comes with the Common C Library.
Examples:
test_sockets.c.

Definition at line 44 of file sockets.c.

References Get_Sock_Adress(), SCK_CHRONO_ERROR, SCK_CONNECT_ERROR, SCK_GET_IP_ERROR, SCK_TIMEOUT_CONNECT, and SCK_TIMER_ERROR.

Referenced by client_smtp_connect().

int read_all_socket int    sockfd,
char **    buffer,
long    tt_sec,
long    tt_micro,
unsigned long int *    ellapsed_time,
unsigned long int *    size
 

Read from a socket, until the connexion is closed.

Parameters:
sockfd Socket descriptor.
buffer Pointer to a pointer that will be used to point to a dynamically allocated memory space. This memory space will contain a zero terminated string of characters which is the peer's reponse.
tt_sec Number of seconds for the timeout.
tt_micro Number of micro seconds for the timeout.
ellapsed_time Pointer to an unsigned long int that will be used to store the ellapsed time.
size Pointer to an unsigned long int that will be used to store the number of bytes read from the peer.
Returns:
The function may return the following values:
  • SCK_READ_OK: the action was succesful. buffer points to a zero terminated string of characters.
  • SCK_READ_NO_MEM: the function could not allocate memory.
  • SCK_READ_READ_ERROR: The system call read() failed.
  • SCK_READ_TIMEOUT: Read timeout.
  • SCK_READ_CHRONO_ERROR: A problem occured with the high precision chronometer.
  • SCK_READ_TIMER_ERROR: A problem occured with the high precision timer.
Warning:
  • If the returned value is SCK_READ_OK, then the buffer points to a string of characters that has been allocated within the function. You must free it yourself (using free()).
  • This function assumes that the peer closes the connexion when the transfer is finished. This is the case for example if you read the answer from a HTTP server (with no keep alive).
Examples:
test_sockets.c.

Definition at line 259 of file sockets.c.

References read_from_socket(), SCK_BUFFER_INIT_SIZE, SCK_READ_NO_MEM, and SCK_READ_OK.

int read_from_socket int    sockfd,
char *    buffer,
size_t    buffer_size,
size_t *    extracted,
long    tt_sec,
long    tt_micro,
unsigned long int *    ellapsed_time
 

Read data from an open socket descriptor (read timeout in micro seconds + ellapsed time in milli seconds).

Parameters:
sockfd Socket descriptor (this must be a descriptor to an open socket).
buffer Pointer to that memory location that will receive the data extracted from the socket.
buffer_size Size of the memory location pointed by buffer (in other words, the maximum amount of bytes that can be read).
extracted Pointer to an interger that will be used to store the number of extracted bytes.
tt_sec Number of seconds of the timeout.
tt_micro Number of micro seconds of the timeout.
ellapsed_time Pointer to an unsigned long integer that will be used to store the ellapsed time.
Returns:
The function may return one of the following values:
  • SCK_READ_CHRONO_ERROR: an error occured while manipulating the high precesion chronometer.
  • SCK_READ_TIMER_ERROR: an error occured while manipulating the high precesion timer.
  • SCK_READ_TIMEOUT: Read timeout.
  • SCK_READ_READ_ERROR: The system call read() failed.
  • SCK_READ_OK: sucess.

Definition at line 167 of file sockets.c.

References SCK_READ_CHRONO_ERROR, SCK_READ_OK, SCK_READ_READ_ERROR, SCK_READ_TIMEOUT, and SCK_READ_TIMER_ERROR.

Referenced by client_smtp_connect(), read_all_socket(), and smtp_send_email().


Generated on Thu Apr 3 16:23:47 2003 for Common_C_libraries by doxygen1.3-rc1