When I tried to do socket communication with Raspberry Pi, the protocol was different

I tried to send signal processing parameters to RPi by socket communication, so when I copied and pasted the Internets code in the first socket communication, the accept function threw an ʻInvalid argument` error and did not work. So, I found out that accept is a function used when the protocol is TCP, and the client side sends it by UDP.

For an understanding of the outline of the UDP protocol and the server code, refer to the following site. Using UDP: Geek Page From the explanation of the above site, it seems that UDP is good if immediacy is required and the parameter is short to some extent, so I decided to rewrite the server reception to UDP. Below is the code that processes the signal in the main routine and receives the parameters in another thread. The part that acquires audio data is omitted.

main.c



#include <netinet/in.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>


#define  PORT_NO    12345


/*Structure to store receive parameters*/
typedef struct {
    /*Appropriate parameter variable group*/
} socket_data_struct;


int serverSock;
struct sockaddr_in serverAddress;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;


/*Socket initialization function*/
int socketInit();      
/*Function for threads that receives parameters in socket communication*/                 
void* parameterReceive(void* pParam);


int socketInit()
{
    serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if(serverSocket < 0) {
        return -1;
    }

    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT_NO);
    /*Receive data addressed to all IP addresses*/                     
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 

    if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) != 0) {
        return -1;
    }
    
    return 0;
}

void* parameterReceive(void* pParam)
{
    socket_data_struct data;
    struct sockaddr_in senderInfo;    
    socklen_t addressLength;

    while (1) {
        recvfrom(serverSock, &data, sizeof(socket_data_struct), 0, (struct sockaddr *)&senderInfo, &addressLength);

        pthread_mutex_lock(&m);            
        
        /*Processing of received parameters*/

        pthread_mutex_unlock(&m);

        /*Echo received data to client*/
        sendto(serverSocket, (char *)&data, sizeof(socket_data_struct), 0, (struct sockaddr *)&senderInfo, addressLength);

        usleep(500);
    }

    close(serverSocket);
}


int main(void)
{
    pthread_t threadIdSocket; 

    if(socketInit()){
        return -1;
    }

    pthread_mutex_init(&m, NULL);
    pthread_create(&threadIdSocket, NULL, parameterReceive, NULL); 

    while(1){
        /*Contains the DSP code*/
    }

    pthread_join(threadIdSocket, NULL);  
    pthread_mutex_destroy(&m); 

    return 0;
}

As a precaution when performing signal processing and socket communication in parallel, do not pthread_join the socket communication thread before the main while loop, put sleep in the while loop of the socket communication thread, Is it about? Also, of course, the port to be used must be opened with ufw or something. You can now receive signal processing parameters while performing signal processing.

Recommended Posts

When I tried to do socket communication with Raspberry Pi, the protocol was different
I tried to automate the watering of the planter with Raspberry Pi
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
I want to run the Python GUI when starting Raspberry Pi
I tried to estimate the pi stochastically
I tried to communicate with a remote server by Socket communication with Python.
When I tried to run Python, it was skipped to the Microsoft Store
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to save the data with discord
I tried L-Chika with Raspberry Pi 4 (Python edition)
When I tried to change the root password with ansible, I couldn't access it.
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
I tried to create a button for Slack with Raspberry Pi + Tact Switch
A story that I was addicted to when I made SFTP communication with python
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I tried to learn the sin function with chainer
Try to visualize the room with Raspberry Pi, part 1
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to solve the problem with Python Vol.1
I tried to make the phone ring when it was posted at the IoT post
What I did when I was angry to put it in with the enable-shared option
A beginner tried coloring line art with chainer. I was able to do it.
I want to be notified of the connection environment when the Raspberry Pi connects to the network
I talked to Raspberry Pi
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried to find the average of the sequence with TensorFlow
I tried connecting Raspberry Pi and conect + with Web API
I tried to automate [a certain task] using Raspberry Pi
Change the message displayed when logging in to Raspberry Pi
I tried to divide the file into folders with Python
I sent the data of Raspberry Pi to GCP (free)
Make a note of what you want to do in the future with Raspberry Pi
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
When I tried to install Ubuntu 18.04, "Initramfs unpacking failed: Decoding failed" was displayed and the startup failed.
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
It's getting cold, so I tried to make it possible to turn on / off the AC heater automatically with Raspberry Pi!
I tried to describe the traffic in real time with WebSocket
The record I was addicted to when putting MeCab on Heroku
I learned how the infrared remote control works with Raspberry Pi
A memorandum when I tried to get it automatically with selenium
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
Serial communication with Raspberry Pi + PySerial
I made a web server with Raspberry Pi to watch anime
I tried to process the image in "sketch style" with OpenCV
I tried to summarize what was output with Qiita with Word cloud
A note I was addicted to when creating a table with SQLAlchemy
I tried to get started with Bitcoin Systre on the weekend
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
A story that didn't work when I tried to log in with the Python requests module
I want to do ○○ with Pandas
What to do when you get "I can't see the site !!!!"
I tried to improve the efficiency of daily work with Python
I tried SMTP communication with Python
I tried to move the ball
I tried to estimate the interval.