HS Sockets C Source Library for Windows and Linux


HS Sockets is a C source code library for interfacing to Windows and Unix network sockets layer. HS Sockets runs on Windows 32 bit and 64 bit desktop OS (XP,Vista,7,8), Windows Mobile OS and on Linux 32 bit and 64 bit OS. HS Sockets is supplied with full C source code and binaries for these platforms

HS Sockets hides the complexity of low level socket api from user application, enabling the programmer to considerably reduce development time.

HS Sockets is designed for light server and client side TCP and UDP applications. HS Sockets supports multiple network connections concurrently - up to 64 sessions at the same time.

HS Sockets allows a programmer to create:

  • TCP server and client applications
  • UDP applications
  • Mixed TCP server, client and UDP applications
  • Secure networking applications using TLS / SSL (using OpenSSL)

HS Sockets supports encrypted communications using OpenSSL library. The use of OpenSSL with HS Sockets is optional

The use of HS Sockets C Source Library is Royalty Free.


  • TCP, UDP server and client sessions
  • Supports up to 64 concurrent sessions
  • Asynchronous non-blocking operation
  • Source code for Windows, Windows Mobile and Linux OS
  • Support secure sockets using OpenSSL
  • Hostname to IP address resolution
  • Retrieve local connection info: hostname, IP address, local port
  • Implicit flow control (buffering in HsSock)
  • IP address to string and vice versa conversion

How It Works

HsSock library incorporates the following software components:

  • Socket I/O receive thread
  • Socket I/O transmit thread
  • Socket I/O accept thread
  • API layer

HsSock library uses the following internal data structures

  • Socket session pool
  • Session pool access mutex
  • Eash session has a transmit buffer queue and receive data end event queue

Socket I/O receive thread monitors the active socket sessions using select() and FD descriptors. It monitors the outgoing TCP socket connected events, session close events and data received events. As events are detected coresponding events and or data are queued into the session receive queue which is read by the user application using the HsSock API layer

Socket I/O transmit thread monitors transmit queues of the active connected socket sessions and sends the data using the underlying BSD socket API (on Linux) or Winsock API (on Windows). The transmit I/O thread corectly handles the partially sent data

Socket I/O accept thread is accepting incoming TCP connections and prepares the session for further handling by the receive and transmit threads

Finally both receive thread and transmit thread also call the SSL processing engine is the connection is secure

Handling SSL / TLS receive and transmit data is implemented by using memory buffered BIOs

After the user application initializes HS Sockets library with HsSockInit it can now estalish TCP client or server sessions or UDP sessions

When acting as a TCP client, the applicatoin calls HsSockTcpConnect. HsSockTcpConnect returns immediately. The application then processes HS Sockets event and data queue by periodically calling HsSockRead. Once TCP connection is accepted by server, HS_SOCK_EV_CONNECTED is received and the applicatoin can send and receive data

When acting as a TCP server, the application calls HsSockTcpListen supplying the event callback function. Again, HsSockTcpListen returns immediatly. New incoming TCP connections are indicated via the event callback.

When acting as a UDP peer, the application calls HsSockUdpOpen supplying the event callback function

To send data the application simply calls HsSockTcpSend or HsSockUdpSendto

The received data are passed up to the application via the event and data queue which is read by calling HsSockRead

Sample Applications

HS Sockets Library is supplied with the following sample applications with full C source code:

  • Command line TCP client application (compiles and runs on Linux and Windows)
  • Command line TCP server application (compiles and runs on Linux and Windows)
  • Command UDP application (compiles and runs on Linux and Windows)
  • Command SSL client application (compiles and runs on Linux and Windows)
  • Command SSL server application (compiles and runs on Linux and Windows)
  • GUI Windows demo application (compiles and runs on Windows)

GUI style demo application for Windows

Commad line TCP server application for Linux

API Summary

  • HsSockInit
  • HsSockCleanUp
  • HsSockUdpOpen
  • HsSockTcpConnect
  • HsSockTcpListen
  • HsSockTcpSend
  • HsSockUdpSendto
  • HsSockClose
  • HsSockRead
  • HsSockTcpSwitchToSecureMode
  • HsSockTcpCheckSecureHandshakeDone
  • HsSockTcpCheckPeerCertSimple
  • HsSockInetNtoa
  • HsSockInetAddr
  • HsSockGetDomain
  • HsSockGetHostIpByName
  • HsSockGetLocalAddress

Secure Sockets Implementation Details

Support for SSL / TLS is implemented in HsSock library using OpenSSL toolkit.

HsSock library has a compile option to incude SSL support or not. If SSL support is compiled in, HsSock library interfaces to OpenSSL API in order to manage SSL connections, secure negotiation and data encryption and decryption.

SSL support is optional and it is possible to use HS Sockets without OpenSSL. To do that HsSock compile option must be changed not to include SSL support.

* This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit


Hillstone Software provides additional services for customization, porting and integration of the library code to better suit the needs of your products