Sento function error in UDP protocol

0

Good I put you a little in situation to see if someone can help me. I'm trying to communicate two computers using a UDP protocol. One computer has a Windows operating system and the other is a Raspberry Pi with Raspbian. The Raspbeery Pi works as a client and the Windows computer works as a server.

I have the problem at the moment of executing the programs. Communication in the direction of the Raspberry Pi to the computer works fine, but not the other way around. I get an error with the sendto function from the server, and when doing the WSAGetLastError () function I get the error 10049 and I do not know how to solve it.

Here I leave the codes in case someone can help me.

Client

#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ReadWriteSock.h"
#include "changetypes.h"
#include "Socket.h"
int main(){
struct sockaddr_in Directionwrite;
//struct servent *Puerto;
//struct hostent *Host;
char pressure[5]="3.42";
char servo[5];
float fpressure, fservo;
int Descriptor,aux;


Directionwrite.sin_family = AF_INET;
Directionwrite.sin_addr.s_addr=inet_addr("169.254.51.37");
Directionwrite.sin_port=htons(49205);

do{Descriptor = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
while(Descriptor==-1);


do{
    aux=connect (Descriptor, (struct sockaddr *)&Directionwrite, sizeof (Directionwrite));
}
    while(aux==-1);
    aux=sizeof(Directionwrite);
writeSocket(Descriptor,pressure,strlen(pressure);
int b=recvfrom(Descriptor, servo, 5, 0, (struct sockaddr *) &Directionwrite, &aux); //Tengo el error 10049
//readSocket(Descriptor,servo,4);
printf("Hemos recibido el valor: %s", servo);
close(Descriptor);
return 0;
}

And now here's the server code:

#include <winsock.h>
#include <stdio.h>
int main()
{
WSADATA wsaData;
SOCKET RecvSocket;
struct sockaddr_in RecvAddr;
int Puerto = 49204;
char RecvBuf[5];
char SendBuf[5]="Hola";
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
WSAStartup(MAKEWORD(2,2), &wsaData);
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Puerto);
RecvAddr.sin_addr.s_addr = INADDR_ANY;
bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
recvfrom(RecvSocket,RecvBuf, 5, 0,(SOCKADDR *)&SenderAddr,&SenderAddrSize);
printf("Hemos recibido %s\n",RecvBuf);
// Ahora vamos a enviar un dato
int i=sendto(RecvSocket,SendBuf,strlen(SendBuf)+1,0,(SOCKADDR *) &RecvAddr,sizeof(RecvAddr));
if(i==-1){
    printf("The error is %d", WSAGetLastError());
}
closesocket(RecvSocket);
WSACleanup();
}

The error I get in this last code when doing the sendto function, receives the client data well. And as I said before, the error that the sendto gives me after using the function WSAGetLastError () is 10049. Thanks in advance.

    
asked by sebs 10.06.2018 в 15:11
source

1 answer

0

Finally I found the solution to the problem. What you have to do is use the SenderAddr variable instead of RecvAddr in the client program for the last two fields. This is because having done a recvfrom before, the SenderAddr field is filled with the client information, and therefore when doing a sendto if we include this variable in the function we are already giving the information where we want to send our data.

    
answered by 26.06.2018 / 14:34
source