(mm) A library for TCP/IP client-server applications on Unix
Home | Software | Count
Software:
GWT
  GWTOAuthLogin
X/Motif
  ansi xterm
  grabc
  mdgclock
  miv
  mplaymidi
  mppp
  mxascii
  mcmap
  mxcmap
  mxconsole
  mxkill
  mxshowfont
  qtip
  xmastm
  yrolo
Web
  mhttpd
  web counter
  upload.pl
  TimeTrack.pl
  mod_auth_ldap
Games
  fltkmm
  iphonemm
Java
   cdcl
   cdclgwt
   jdgclock
Libraries
  libcalen
  libmcfg
  libsll
  libmsock
Misc
  bangla font
  dpr
  genmake
  hod
  smtp.pl
  vhtml
  phones_ldap
  showpic_ldap
  mbasecalc
  fluid_hack
  kdialppp
  strip2csv
  googlecode-upload
MS Windows
  mwinclip.pl
  mbasecalc
  mailsend
  wiv

libmsock.a
A library for TCP/IP client-server applications on Unix
Muhammad A Muquit

Description
These functions can be used to develop TCP/IP client-server applications on Unix. The functions encapsulate the complexities of BSD socket APIs. I don't deserve credit for these functions, all credit belong to Unix Socket programming FAQ example code. I just cleaned them out, added a few, created this library, wrote this documentation and wrote few examples.

Download

Source
File: libmsock.tar.gz
Size: 43314 bytes
MD5 Checksum: 1d04542c845edb79d9765a3d9ce48de2
Last updated: ?

Synopsis

#include <msock.h>

int ServerSocket(u_short port,int max_servers);
int ClientSocket(char *netaddress,u_short port);
int sockGets(int sockfd,char *str,size_t count);
int sockRead(int sockfd,char *str,size_t count);
int sockPuts(int sockfd,char *str);
int sockWrite(int sockfd,char *str,size_t count);
int getPeerInfo(int sockfd,char *cli_host,char *cli_ip,u_short *cli_port);
int atoport(char *service,char *proto);
struct in_addr *atoaddr(char *address);
There are several routines available for SSL if compiled with -DUSE_SSL=1 flag. You will need SSLeay. SSLeay is a free implementation of Netscape's Secure Socket Layer - the software encryption protocol behind the Netscape Secure Server and the Netscape Navigator Browser. The routines are:


    int sockReadSSL(SSL *ssl,char *buf,size_t n);
    int sockWriteSSL(SSL *ssl,char *str,size_t count);
    int sockGetsSSL(SSL *ssl,char *str,size_t count);
    int sockPutsSSL(SSL *ssl,char *str);

int ServerSocket(u_short port,int max_servers)
This function listens on a port and returns connections. The connection is returned as the socket file descriptor. The socket is of type SOCK_STREAM and AF_INET family. The function will create a new process for every incoming connections, so in the listening process, it will never return. Only when a connection comes in and a new process for it is created, the function will return.This means, the caller should never loop.

The parameters are as follows:


u_short port        The port to listens to (host byte order)
int max_servers     The maximum number of connection to queue up before 
                    having them rejected automatically.
                    

The function returns the socked file descriptor (a positive number) on success and -1 on failure.

NOTE: _NEVER_ convert port to network byte order by calling htons(). It will be converted by the function. The options SO_REUSEADDR option is set on the socket file descriptor. Read the Chapter 7 of Unix Network Programming (2nd Ed) by Stevens for details on the socket options.

int ClientSocket(char *netaddress,u_short port)
This function makes a connection to a given server's stream socket. The parameters are as follows:

char    *netaddress The host to connect to. The netaddress can
                    be the hostname or the IP address (dot separated 8 octets).
u_short port        The port to connect to

The function returns the socked file descriptor (a positive number) on success and -1 on failure.

NOTE: _NEVER_ convert port to network byte order by calling htons(). It will be converted by the function.

int sockGets(int sockfd,char *str,size_t count)
This function reads from a socket until it receives a line feed character. It fills the buffer "str" up to the maximum size "count".

The parameters are as follows:
int sockfd      The socket to read from.
char *str       The buffer to fill up.
size_t  count   The maximum number of bytes to stuff to str.

This function returns the number of bytes read from the socket or -1 if the connection is closed unexpectedly.

WARNING: If a single line exceeds the length of count, the data will be read and discarded.

int sockRead(int sockfd,char *str,size_t count)
This function reads the "count" number of bytes and stuffs to str. str must have enough space. This routine is like read() system call except that it makes sure all the requested number of bytes are read. Note, str must have enough space to hold the "count" number of bytes.

The function returns >= 0 if succeeds and -1 if there is any error.

int sockPuts(int sockfd,char *str)
This function writes a character string out to a socket.

The function returns number of bytes written to the socket or -1 if the connection is closed while it is trying to write.

int sockWrite(int sockfd,char *str,size_t count)
This function is like the write() system call, except that it will make sure all data is transmitted.

The function returns number of bytes written to the socket and -1 in case of any error.

int getPeerInfo(int sockfd,char *cli_host,char *cli_ip,u_short *cli_port)
This function gets information about the host connected to the socket. The parameters are as follows:

int sockfd       The connected socket
char *cli_host   The hostname connected to the socket (returns)
char *cli_ip     The IP address of the host connected (returns)
u_short *cli_port The client side port of the host (returns)

Note, cli_host, cli_ip must have enough space to hold the info.

int atoport(char *service,char *proto)
This function takes a service name and a service type and returns 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.

The function returns a positive number if succeeds and -1 if fails.

struct in_addr *atoaddr(char *address)
This function converts ASCII text to in_addr struct. NULL is returned if the address can not be found.

Compile

$ gunzip < libmsock.tar.gz | tar xvf -
$ cd libmsock
$ ./configure
$ make
If make succeeds the library libmsock.a will be created. To install it copy libmsock.a to /usr/local/lib and msock.h in /usr/local/include. Go to the examples and its sub-directories, type make to compile an example.

Examples
Several examples programs supplied in the examples directory with Makefiles.

  • echod - an echo server (echod.c)
  • htget - get a webpage from a web server(htget.c)
  • server_type - identify the type of a web server (server_type.c)
  • Todo
    Write examples about SSL routines. Provide more interesting examples.

    Last updated: Jul-10-1999

    URL of this page: http://www.muquit.com/muquit/software/libmsock/libmsock.html

    back Page updated: Sun Mar 31 01:59:56 2013 GMT   Copyright © 2013 muquit@muquit.com.