Network Component  Version 6.6
MDK-Professional Middleware for IP Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
BSD Socket

The BSD sockets application programming interface (API) is a set of standard function calls that can be used in an application. They allow programmers to add Internet communication to their products.

A client/server architecture is mandatory for BSD sockets. Using TCP, a host listens for incoming connection requests. Upon accepting an incoming request, data can be transferred between the hosts. UDP can also be used to establish a connection.

As you can see, BSD sockets is not a stand-alone socket solution, but it is an API that relies on other socket communication for data exchange. Thus, you always need to add TCP and UDP to your project if you wish to use BSD sockets.

Communication Flow

The image below explains the basic communication flow using BSD sockets with TCP.

Flow Diagram for BSD Sockets Communication using TCP

BSD Server

The BSD server creates a socket, uses bind to attach that socket to a port, and configures it as a listening socket. This allows the server to receive incoming connection requests. Afterwards, accept is called, which will block the socket, until an incoming connection request is received. When accept returns, the SOCKADDR structure will have been filled out with the originating IP Address and port of the incoming connection. Then, accept creates a new socket, which is then used to receive data until the connection is closed by the other side.

Code Example for the BSD Server

//Thread 'Server': BSD Server socket process
static void Server (void const *arg) {
int sock, sd, res;
int type = (int)arg;
char dbuf[4];
while (1) {
sock = socket (AF_INET, type, 0);
addr.sin_port = htons(PORT_NUM);
addr.sin_addr.s_addr = INADDR_ANY;
bind (sock, (SOCKADDR *)&addr, sizeof(addr));
if (type == SOCK_STREAM) {
listen (sock, 1);
sd = accept (sock, NULL, NULL);
closesocket (sock);
sock = sd;
while (1) {
res = recv (sock, dbuf, sizeof (dbuf), 0);
if (res <= 0) {
if (dbuf[0] == BLINKLED) {
LED_Out (dbuf[1]);
closesocket (sock);

BSD Client

The BSD Client creates a socket calls connect, because TCP requires a negotiated connection. Afterwards, send is called to send the data to the server. Note that bind is never called, because the stack will pick a random port and an appropriate IP address. To finish the communication, the client calls closesocket.

Code Example for the BSD Client

// Thread 'Client': BSD Client socket process
static void Client (void const *arg) {
int sock, res;
char dbuf[4];
uint8_t p2val,lshf;
while (1) {
sock = socket (AF_INET, SOCKTYPE, 0);
addr.sin_port = htons(PORT_NUM);
addr.sin_addr.s_b1 = IP1;
addr.sin_addr.s_b2 = IP2;
addr.sin_addr.s_b3 = IP3;
addr.sin_addr.s_b4 = IP4;
connect (sock, (SOCKADDR *)&addr, sizeof (addr));
lshf = 1;
p2val = 0x01;
while (1) {
// Shift the LEDs
LED_Out (p2val);
p2val = lshf ? (p2val << 1) : (p2val >> 1);
if (p2val == 0x80) lshf = 0;
if (p2val == 0x01) lshf = 1;
// Send the data to LED Server.
dbuf[0] = BLINKLED;
dbuf[1] = p2val;
res = send (sock, (char *)&dbuf, 2, 0);
if (res < 0) {
osDelay (100 * SPEED);
closesocket (sock);

BSD Socket Functions

The following table shows the available API functions for BSD sockets.

Function Description
accept Accepts a connection request queued for a listening socket.
bind Assigns a name (local address) to a socket.
closesocket Closes an existing socket and releases a socket descriptor.
connect Establishes connection between the endpoints on stream sockets.
gethostbyname Retrieves host address corresponding to a host name from a host database.
getpeername Retrieves the address of the peer to which a socket is connected.
getsockname Retrieves the local address of the socket.
ioctlsocket Sets or retrieves some of the operating parameters on a socket.
listen Sets the socket in a listen mode.
recv Receives incoming data that has been queued for a socket.
recvfrom Receives incoming data on a datagram socket.
send Sends outgoing data on a socket.
sendto Sends outgoing data on a datagram socket to destination address.
socket Creates a communication socket.
  • The BSD sockets implementation in the Network Component is not a complete implementation of the BSD API.
  • The BSD functions are thread safe and must be used with an CMSIS-RTOS.

BSD Socket Configuration

BSD Socket Configuration File

The BSD sockets configuration file Net_Config_BSD.h contains the following settings:

  • Number of BSD Sockets specifies the number of available BSD sockets. This number specifies the maximum number of simultaneously opened connections.
  • Number of Streaming Server Sockets specifies the number of streaming (TCP) server sockets. The server sockets listen for an incoming connection from the client.
  • Receive Timeout in seconds is the timeout, after which the receiving socket returns to blocking mode.
  • The Hostname Resolver switch enables or disables the Berkeley style hostname resolver.