Developing a Networking Application with Sockets using WiSeConnect™ SDK v3.x#
This guide provides information on how to write a networking application with socket programming constructs using the WiSeConnect™ SDK v3.x.
A socket provides a communication interface between two devices in a network. It is an operating system (OS) construct that facilitates the management of a network connection and the exchange of data. Embedded systems like the SiWx91x™ chipsets provide their own socket implementations to allow applications from OSes such as Linux to be easily ported to an embedded system, and for experienced socket programmers to easily write networking applications for embedded systems.
Socket programming can be used to create a variety of networking applications such as web servers, chat applications, real-time data feeds, and so on.
Application Flow#
Socket programming follows a client-server architecture where one device acts as a client and the other as a server. The following is a common sequence of steps in socket communication between a client and server:
Both the client and the server create their own sockets.
The server binds its socket to a specific IP address and port and listens for incoming connections.
The client initiates a connection to the server.
The server accepts the client's connection request.
Data is exchanged between the client and server.
To terminate the network connection, the client and server close their respective sockets.
Server API Calls#
The server invokes the
socket()
API to create a new socket. This API returns a unique socket number that is used in subsequent API calls.int sockfd; sockfd = socket(AF_INET, SOCK_STREAM, 0);
The server invokes the
bind()
API to bind its socket to an IP address and port.struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = 8080; // Port number bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
The server invokes the
listen()
API to begin listening for incoming client connections.listen(sockfd, 5);
The server invokes the
accept()
API to accept an incoming client connection. Theaccept()
API blocks until a client connection request is received. After it returns, data transfer between the client and server can begin.int new_sock; struct sockaddr_in client_addr; socklen_t addr_size = sizeof(client_addr); new_sock = accept(sockfd, (struct sockaddr*)&client_addr, &addr_size);
The server uses the
send()
API to transmit data and therecv()
API to receive data. Therecv()
API blocks until the specified number of bytes of data is received.char buffer[1024] = {0}; char *message = "Hello, client!"; send(new_sock, message, strlen(message), 0); recv(new_sock, buffer, 1024, 0);
The server invokes the
close()
orshutdown()
API to terminate the connection and free the socket number for use in a new network connection.close(new_sock); close(sockfd);
Client API Calls#
The client invokes the
socket()
API to create a new socket.int sockfd; sockfd = socket(AF_INET, SOCK_STREAM, 0);
The client invokes the
connect()
API with a specific IP address and port to initiate a connection with a server listening at the specified IP address and port. Theconnect()
API blocks until the connection is established with the server. After it returns, data transfer can begin.struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_port = 8080; // Server port number connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr))
The client uses the
send()
API to transmit data and therecv()
API to receive data. Therecv()
API blocks until the specified number of bytes of data is received.char buffer[1024] = {0}; char *message = "Hello, server!"; send(sockfd, message, strlen(message), 0); recv(sockfd, buffer, 1024, 0);
The client invokes the
close()
orshutdown()
API to terminate the connection and free the socket number for use in a new network connection.close(sockfd);
Next Steps#
Explore the available socket implementations and application examples:
API Reference Guides#
The WiSeConnect SDK v3.x offers a variety of socket implementations for different networking requirements:
BSD Sockets implements the Berkeley Software Distribution (BSD) standard API for socket programming.
IoT Sockets implements the ARM standard Internet-of-things (IoT) socket programming API.
SiWx91x Sockets implements a socket programming API for the SiWx91x chipsets.
Application Examples#
The WiSeConnect SDK v3.x provides application examples demonstrating the use of socket programming:
The Wi-Fi - Throughput example demonstrates the measurement of network throughput over a Wi-Fi connection.
The Wi-Fi - TLS Client example demonstrates the implementation of a Transport Layer Security (TLS) client over a Wi-Fi connection.