Here I provide a set of examples on using sockets
with the C
under both Microsoft Windows (using MinGW) and Linux.
The following examples are included in this folder.
The simple TCP Client and TCP Server examples do nothing spectacular.
The client sends one byte with value 2
over a TCP connection to the server listening on port 9999 of the local host and terminates.
The server accepts 5 incoming connections, prints the received byte, and then terminates.
Both programs do not perform any error handling. This is to make them shorter. In reality, you must handle errors.
The simple UDP Client and UDP Server examples do nothing spectacular, actually, basically the same as the TCP client/server, except that they use port 9998 and the UDP instead of the TCP protocol.
The client sends one byte with value 2
in an UDP package to the server and terminates.
The server accepts 5 incoming packages, prints the received byte, and then terminates.
Both programs do not perform any error handling. This is to make them shorter. In reality, you must handle errors.
For building, I here use GCC under both Linux and Windows. You can use other compilers too, if you want.
The program codes whose file names end with _linux.c
are the Linux versions of my socket examples. For an example with name fileName_linux.c
, you would type
gcc fileName_linux.c -o fileName_linux
You will get an executable named fileName_linux
.
For your convenience, you can build all Linux examples directly by executing make_linux.sh
. (You may need to chmod +x make_linux.sh
first.)
The program codes whose file names end with _windows.c
are the Microsoft Windows versions of my sockets examples. You can build the examples using MinGW. For an example with name fileName_windows.c
, you would type
gcc fileName_windows.c -o fileName_windows.exe -lws2_32
The "-lws2_32
" at the end states that we link against Winsock, the Windows socket implementation. Such a compilation will result in a corresponding .exe
executable file.
For your convenience, you can build all Windows examples directly by executing For your convenience, you can build all Linux examples directly by executing make_windows_mingw.bat
.
To use GCC under Windows, you will need MinGW. This is how you get it:
- download and run the installer
- in the installer, select the
gcc
andmsys
packages (don't unselect anything selected) - install ideally to recommended folder
C:\MinGW
- Open the system control and add the following directories to
PATH
environment variablesC:\MinGW\msys\1.0\bin
C:\MinGW\bin
In Linux, we can relatively easily cross-compile for Windows.
For this, we need to install MinGW under Linux, namely do sudo apt-get install gcc-mingw-w64-i686
. In some environments, installing this package directly and alone may fail. This is the case, for instance, in the shippable build environment). There, and in many cases, this can be fixed by the following steps. If one of them fails, that is OK as long as the last line (the install
) passes.
- sudo apt-get -q -y autoclean
- sudo apt-get -f install
- sudo apt-get -q -y update
- sudo apt-get -q -y install gcc binutils-mingw-w64-i686 gcc-mingw-w64-i686
Then, we can simply replace the gcc
in the windows commands with gcc-mingw-w64-i686
, i.e., do
gcc-mingw-w64-i686 fileName_windows.c -o fileName_windows.exe -lws2_32
instead of
gcc fileName_windows.c -o fileName_windows.exe -lws2_32
and we are good.
For your convenience, you can build all Linux and Windows examples directly by executing make_linux_and_windows.sh
. (You may need to chmod +x make_linux_and_windows.sh
first.)