|Applying C - Sockets The Client|
|Written by Harry Fairhead|
|Tuesday, 26 April 2022|
Page 1 of 4
Sockets are a fundamental commnication method and you need to know how to use them. This extract is from my book on using C in an IoT context.
Now available as a paperback or ebook from Amazon.
Applying C For The IoT With Linux
Also see the companion book: Fundamental C
Sockets are a general-purpose way of communicating over networks and similar infrastructure. Essentially they are a generalization of files to things other than storage devices. They aren’t part of the C standard, but are available on all POSIX-compliant operating systems. As well as sockets, POSIX systems also support other file-like ways to communicate – in particular, pipes, see Chapter 8, can be thought of as streams of bytes that are produced by one process and consumed by another. In this sense a pipe is an inter-process communication tool. Sockets can also be used in this way, but they can also be used to send streams of bytes from one machine to another using a range of different communications media. For example, X11 makes a connection between a client and server using a pipe when they are on the same machine and sockets when they are on different machines.
In this chapter we focus on using sockets to send data over the internet but it is worth keeping in mind that they are more versatile that this might suggest. All of the code described will work under any POSIX system including Raspbian and Linux running on x86. It doesn’t work as described under Windows or MinGW. To make it work under Windows you either have to use CygWin or the native WinSock library, which is slightly different from the POSIX socket library as explained later.
To send some web data, an HTML page or JSON data, most programmers think of a web server and that the next step is to install and configure Apache. This is usually far too big a solution to a small problem to be implemented on a small machine. However, it is very easy to implement a simple web server or a web client using sockets. All sockets do is transport data from one point to another, so you can use them to communicate using almost any standard protocol, like HTTP, or a custom protocol of your own devising. Put simply, a socket is a stream of bytes that you can send over a communication channel.
The basic steps in using a socket are fairly simple:
Sockets connect to other sockets by their addresses. The simplest case is where there are just two sockets, or two endpoints, communicating. Once the connection is made, the two sockets at each end of the connection operate in more or less the same way. In general one of the sockets, the client, will have initiated the connection and the other, the server, will have accepted it.
There is a conceptual difference between a client and a server socket. A server socket is set up and then it waits for clients to connect to it. A client socket actively seeks a connection with a server. Once connected, data can flow in both directions and the difference between the two ends of the connection becomes less. That is, the difference between client and server is only about who initiates the connection.
The key idea is that a socket is implemented to make it look as much like a standard POSIX file as possible. This conforms with a general principle of Linux/Unix that any I/O facility should follow the conventions of a file.
There are several basic socket functions that are needed for specific purposes:
Create a socket
sockfd= socket(int socket_family,
This returns a socket descriptor, an int which you use in other socket functions. The socket_family is where you specify the type of communications link to be use and this is where sockets are most general. There are lots of communications methods that sockets can use, including AF_UNIX or AF_LOCAL, which don't use a network, but allow inter-communication between processes on the same machine. In most cases, you are going to be using AF_INET for IPv4 or AF_INET6 for IPv6 networking.
The socket_type specifies the general protocol to be used. In most cases you will use SOCK_STREAM which specifies a reliable two-way connection - for IP communications this means TCP/IP is used. For some applications you might want to use SOCK_DGRAM, which specifies that the data should be sent without confirming that it has been received. This is a broadcast mechanism that corresponds to UDP for IP communications.
The protocol parameter selects a sub-protocol of the socket type. In most cases you can simply set it to 0. As we are going to be working with sockets that basically work with the web, we will use AF_INET and SOCK_STREAM.
|Last Updated ( Tuesday, 26 April 2022 )|