$Revision: 1.3 $
Example code: A socket module.
Status: NOT REVUED, NOT TESTED
- Online References:
-
Included Files
- #include "modules/sk/socket.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <netdb.h>
- #include <signal.h>
- #include <stdio.h>
Preprocessor definitions
String sizes
#define STR_S 63
#define STR_M 255
#define STR_L 1023
#define STR_XL 4095
#define STR_XXL 16383
Wait for an incoming connection on the specified socket
int SK_accept_connection The socket for communicating to the client
More:
Authors:
joao
int SK_accept_connection ( int listening_socket )
- int listening_socket
- The socket that the server is bound to
Take a service name, and a service type, and return a port number. If the
service name is not found, it tries it as a decimal number. The number
returned is byte ordered for the network.
char *service Service name (or port number).
char *proto Protocol (eg "tcp").
More:
Authors:
ottrey
- Online References:
int SK_atoport ( const char* service, const char* proto )
void SK_close ( int socket )
This function reads from a socket, until it recieves a linefeed
character. It fills the buffer "str" up to the maximum size "count".
More:
Authors:
ottrey
Side Effects:
This function will return -1 if the socket is closed during the read operation.
Note that if a single line exceeds the length of count, the extra data
will be read and discarded! You have been warned.
To Do:
Capture the control-c properly!
int SK_gets ( int sockfd, char* str, size_t count )
- int SK_gets
- The total_count of bytes read.
- int sockfd
- The socket file descriptor.
- char* str
- The buffer to be written from the socket.
- size_t count
- The number of bytes in the buffer.
This function creates a socket and binds to it
- Online References:
int SK_getsock ( int socket_type, u_short port, uint32_t bind_address )
- int SK_getsock
- The new socket
- int socket_type
- SOCK_STREAM or SOCK_DGRAM (TCP or UDP sockets)
- u_short port
- The port to listen on. Remember that ports < 1024 are
reserved for the root user. Must be passed in network byte
order (see "man htons").
- uint32_t bind_address
- Address to bind to, in network order.
More:
Authors:
ottrey
joao
This function writes a character string out to a socket.
char *str The buffer to be written from the socket.
More:
Authors:
ottrey
Side Effects:
This function will return -1 if the socket is closed during the write operation.
Note that if a single line exceeds the length of count, the extra data
will be read and discarded! You have been warned.
int SK_puts ( int sockfd, const char* str )
- int SK_puts
- The total_count of bytes read.
- int sockfd
- The socket file descriptor.
- const char* str
-
XXX Note: Not sure how long this function will last. Shouldn't _really_ need it.
More:
Authors:
ottrey
- Online References:
static void func_atexit ( void )
static void func_sighup ( int n )
static void func_sigint ( int n )
static void log_print ( const char* arg )
This is just like the read() system call, except that it will make
sure that all your data goes through the socket.
More:
Authors:
ottrey
static int sock_read ( int sockfd, char* buf, size_t count )
- int sock_read
- The number of bytes read.
- int sockfd
- The socket file descriptor.
- char* buf
- The buffer to be read from the socket.
- size_t count
- The number of bytes in the buffer.
This is just like the write() system call, accept that it will
make sure that all data is transmitted.
char *buf The buffer to be written to the socket.
More:
Authors:
ottrey
- Online References:
static int sock_write ( int sockfd, const char* buf, size_t count )
- int sockfd
- The socket file descriptor.
- const char* buf
-
- size_t count
- The number of bytes in the buffer.