Network Component
Version 6.3
MDK-Professional Middleware for IP Networking
|
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.
The image below explains the basic communication flow using BSD sockets with TCP.
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
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
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 configuration file Net_Config_BSD.h contains the following settings: