How to run a C++ TCP/IP server on Raspberry Pi

This guide will explain how to create a TCP/IP Server on Raspberry Pi 3 B, running Raspbian (or Raspbian Lite). The source code file is written in C/C++ language.

The source code is very simple and short. The code below is available on this website, and uses Linux sockets to create a TCP/IP server on port 8888.

The program at first creates a socket descriptor. Then, it initializes a sockaddr_in structure (it contains the address of the server itself, the connection port number and the type of communication), and binds it to the socket. After that, the server is set in listening mode. And when an incoming connection arrives from a client (a Telnet connection from Putty on the PC), it opens the connection and suddenly closes and exit the program.

First of all, you need to create a file with "c" extension and copy it on your Raspberry Pi, for example using Samba from your PC (a guide is available here). I often use this method, because editing a text file on the PC, for example using Notepad++, is faster than doing it on the Raspberry using nano editor. You can edit the file many times on the PC, and when the file is ready copy-paste it on the Pi by using Samba.

In this example, I created a file called "server.c", and included it in the folder "server" located inside the home. In order to compile and run the program using gcc, type the following commands:

gcc server.c -o server
./server

Raspberry Pi 3 B TCP IP socket C language 1The server program is now waiting for a connection coming from a TCP/IP client. You can connect from your PC using Putty. You just need to write the IP address of your Raspberry (example: 192.168.0.13) and the TCP port (8888). Connection protocol is "Telnet" (no encryption). In case you don't know the IP address of your Pi, you can use Advanced IP Scanner or similar programs.

Raspberry Pi 3 B TCP IP socket C language 2When pressing the button "Open", Putty will connect to the Raspberry Pi on port 8888. Raspberry will then show a message and then close the TCP communication.

Raspberry Pi 3 B TCP IP socket C language 3This guide simply explained how to compile a C/C++ program using gcc compiler on Raspbian OS. In the next lesson, I will show how to continuously communicate using TCP/IP, by sending and receiving data.

Author: Davide Cavaliere

I am an Italian Electrical Engineer graduated at Politecnico di Milano. My interests are motorcycles and cars, electronics, programming, Internet of Things, and Japanese culture.

2 thoughts on “How to run a C++ TCP/IP server on Raspberry Pi”

  1. Can you please post the lesson for continuous communications using TCP/IP and C/C++ on the Rasp Pi? It would be greatly appreciated! I am using an R Pi with a PLC for a school project and I don't want to use Python. If you can email me the information that I need, I would appreciate that as well.

    1. Kevin, I don't know if Davide has responded via email or not, but here are my thoughts on your request:

      1) All you need to do is once you have a connection, loop waiting on characters to come in the connection. You can put this loop after line 41.
      2) If you loop waiting on commands to come in, this will block any other activity on the RasPi. You may want to implement some sort of interrupt scheme where on receipt of a command, the program executes it, otherwise the program should sleep (wait in background).
      3) One of the commands should be 'Q!' for quitting the app.

      Good Luck.

Leave a Reply

Your email address will not be published. Required fields are marked *