Network Dual-Stack Component  Version 7.0 (Beta)
MDK-Professional Middleware for IPv4 and IPv6 Networking
 All Data Structures Files Functions Enumerations Groups Pages
User API

BSD Socket functions and communication flow. More...

BSD Socket functions and communication flow.

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.
setsockopt Manipulate options for the socket.
socket Creates a communication socket.
Note
  • 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 a CMSIS-RTOS.

Communication Flow

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

NW_Diagrams.png
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

// BSD socket server thread
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_family = PF_INET;
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) {
break;
}
if (dbuf[0] == BLINKLED) {
LED_SetOut (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

// BSD socket client thread
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_family = PF_INET;
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_SetOut (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) {
break;
}
osDelay (100 * SPEED);
}
closesocket (sock);
}
}