Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT / Raspberry-Pi

Server Raspberry-Clients ASUS TcpIp

4.50/5 (4 votes)
24 Dec 2015CPOL8 min read 11.6K   247  
Raspberry; laptop ASUS: up to 150 clients.

Introduction

This tip is about communication between 2 machines: RaspberryPi2 + Raspbian and laptop ASUS 64bit Intel 2117U + Windows10. These machines are members of the same domestic LAN, managed by a VODAFONE router that is connected to the internet too. However these 2 machines talk to each other without accessing the internet. Both machines use WiFi. The server uses 2 files to load the messages to transmit: one file is in russian. One of the intentional tests uses SIGUSR1 interrupt for Raspberry. To start and interrupt the server from laptop I use PuTTY that also shows messages.

Background

Setup of Raspberry in order to associate it to a LAN and connection of a laptop to the same LAN. A reference is: https://www.marcomc.com/2012/09/how-to-configure-wireless-lan-on-raspberrypi-with-raspbian-kernel-3-2-27-and-solwise-rtl8188cus-wifi-dongle/

Setup of  Raspberry in order to support Cyrillic. A reference is: http://serverfault.com/questions/54591/how-to-install-change-locale-on-debian 

Interrupts under GNU. A reference is: http://man7.org/linux/man-pages/man7/signal.7.html

cin for numbers only. A reference is: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ca3a8fc5-658d-4210-9955-1d16e4789c98/cin-number-only?forum=vclanguage

Using the code

Deployment of files:

RaspberryPi2. Create a directory named MyDirect and put inside: TcpIpServer, SomeItalian, SomeRussian. TcpIpServer searches this directory for the 2 text files.

Laptop ASUS. Create a folder and put inside StartClients.exe and TcpIpClient.exe. StartClients looks for TcpIpClient in the same folder.

 

Abridged description of possible operations.                                                                                                                                                                        Details 

  1. Launch PuTTY and start server program TcpIpServer. Launch StartClients, find server and input a number of clients between 1 and 150. Wait till exchange finishes.
  2. Like  point 1 but also close manually some clients. I don't close clients while they are incrementing in number: initial phase of test. I wait till the batch is complete, nay I prefer to wait till the number of clients starts decrementing.
  3. Like  point 1 but press ctrl c on the prompt in PutTTY when possible. The server shuts down communication with all clients.
  4. Launch 2 PuTTY. The first is used like in previous points. The latter is needed to interrupt Raspberry with SIGUSR1. The server sends “CIAO” to all ready clients when senses a SIGUSR1 interrupt.

 

Details                                                                                                                                                                                                                                     Abridged description.      

When started, the server waits indefinitely for some clients. Then it continues to accept and serve new clients of the same batch. After each reading cycle it answers back to all ready clients. It closes a socket after receiving a “” message from a client. Before exiting, server anyway closes sockets for those clients that haven't transmitted a “” message

To close manually clients while they are communicating with server, press x syscommand on the right-top corner of window. Frequency of closure can be some Hz. While they close they take the left most position as reduced windows like shown below. I had to change the position of closing windows as their normal position right-bottom interfered with a high frequency of mouse hitting x syscommand. I added a delayed closing thread.

For all cases the client windows are placed in the bottom-right region of the screen and are of 5 types:

  1. Bottom-right: client is communicating with server.
  2. Top-left: client has finished exchanging data with server and server has abated communication.
  3. Bottom:left: server has received SIGUSR1, has sent an ending message to all active clients and has abated communication, sending then a "" message after exiting the normal cycle of tx-rx. Not all clients will be able to catch that conventional message because engaged in other activities: transmitting or receiving with excessive delay. Those clients unable to catch that message will remain with messages to send to server and will be placed top-right as they will be unable to use an abated connection.
  4. Top-right: client still has some messages to send to server, but server has abated communication. This can happen for some clients as described in point 3 or for all transmitting clients when server receives a ctrl c. In the latter case server automatically sends a "" string to all active clients and terminates execution. This automatic behaviour exempts me from considering more code dedicated to ctrl c occurrence.
  5. Extreme left: shrunk client window is closing.

So in all cases all clients start in position bottom-right, but in the end this position will be empty. Very rarely some clients remain  bottom-right also when overall communication has ended: they haven't been able to connect to server.

Messages emitted by server are sometimes corrupted: I estimate less than 1%. 

StartClients is a Win32 app that starts clients, pass them a string and waits for all clients to terminate. Then it terminates. The string it passes to each client contains 4 parameters: the cardinal number of the client that will be used by server to associate it to a socket number; a random number that is the number of messages the client will send to the server; the binary address of server; the communication port of server.It has a simple menu. The first input is a choice among 3 alternative ways to search for a server: by standard name; by custom name; by IP. The second input is the number of clients required. 

The client searches for the server and, after having been accepted, starts transmission. After each transmission the client passes to receiving mode. The time of transmission is random and is calculated by a … worker thread in hundredths of a second. The … worker thread contains all the methods to communicate and is needed to provide a separate path of execution regard to UI main thread. The 3 threads use SendPostMessage to exchange messages. CriticalSection: seems not necessary, but I prefere to use it.

Both server and client use non blocking mode for sockets.

To avoid the disappearing of StartClients icon from desktop when starting more than some tens of clients, I use the command prompt to start StartClients.

Some parameters inside code that can be adjusted at compilation time:

TcpIpClient: SELECT_TIMEOUT, SLEEP_CONNECT, SLEEP_QUIT.

TcpIpServer: stTimeRead, stTimeWrite.

 

Normal situation:

Top left is PutTTY showing server operations. At the moment: Client7 has finished sending messages and has closed.  Client48 is still exchaning messages with server. For this batch no lack of resources of server happened. Each position contains many window clients in the same situation. They can be dragged manually, just to see, but it would be silly to drag and drop them when they are more than 10 in total.

Image 1

 

Now operations have finished. All clients have closed connections. Closing manually windows under Client58 should give a total count equal to the clients started. 

Image 2

 

That happens when in below taskbar, righ mouse button over MFC, clase all apps: it lasts less than 2 seconds. I had to be swift to stamp it. The time can be adjusted, but I explain above.

Image 3

 

Resource unavailable situation:

This image shows lack of resources of server. I haven't taken it while it was happening: too fast. I waited till process finished and then scrolled back to catch some of the red lines. VODAFONE router normally on internet. I grabbed the border of PutTTY with the mouse, holding it for some seconds while communication was going on. Red lines could be seen scrolling on PutTTY and the clients accumulated where is Client44. It happens more frequently if trying while clients are over 100.

Image 4

 

VODAFONE router is on internet. I haven't disturbed server, but lack of resources of server thwarts clients even to start communicating. This time has been unlucky.

Image 5

 

Control C situation:

Client7 has just finished exchanging messages with server. All the other 106 are communicating with server. There hasn't been any lack of resources till now.

Image 6

 

After a minute or so, while 22 clients were still active, I input ctrl c on PutTTY. Almost immediatly the 22 windows swarmed to position under Client10. Then all MFC app can be closed from taskbar with right button mouse. Due to overload of laptop, thr green prompt of PutTTY can become inactive (void) while trying to input ctrl c: it's a matter of manual dexterity like in videogames (said by a maladroit).

Image 7

 

SIGUSR1 situation:

Everything is prepared for launch.

Image 8

 

Return on StartClients and waiting half a minute till all clients are communicating with server. (mouse on left top PuTTY repeatedly to speed up.....)

Image 9

 

Return on right top PuTTY to activate SIGUSR1.

Image 10

 

After all is finished I scroll back left top PuTTY to see when server received SIGUSR1. PuTTY (50000 lines).

Image 11

 

Some hundreds of line next.

Image 12

 

Some tens of lines next.

Image 13

 

Uploaded files contain code and exe.

Download TcpIpServer.zip

Download StartClients.zip

Download TcpIpClient.zip

Download SomeItalian.zip

Download SomeRussian.zip

Points of Interest

Sockets.

History

Dongle for Raspberry is RTL8188CUS 802.11n, but others wold do fine as well. OS of Raspberry: 2015-05-05-raspbian-wheezy, VERSION="7 (wheezy)".Originally OS of ASUS was Windows8, then 8.1 and now Windows10: all work well with Raspberry stuff. I use VisualGDB to program Raspberry. All apps, initially developed with VisualStudio13, have undergone migration to VisualStudioCommunity2015.

I've run across the repeated lack of resources of server: normally it happened rarely. I unplugged VODAFONE router from telephonic line and continued well between laptop and Raspberry. 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)