Search code examples
csocketsudpsendto

Error with sendto function


I am currently trying to set an UDP protocol between two computers. The first one is a Windows driven computer and the second one is a Raspberry Pi with Raspbian OS.

The problem is that I am able to send information from the Raspberry Pi to the other computer, but I run into problems when sending the message in the other direction. Specifically the problem is at the sendto function. When I call it, it gives me an error and when I use the function WSAGetLastError() it gives me the error 10049 and I was wondering if someone could help me to solve it.

In my schema, the Raspberry Pi is the client and the Windows based computer is the server.

Here are both codes: Client (Raspbian):

#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "ReadWriteSock.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); 
    printf("Hemos recibido el valor: %s", servo);
    close(Descriptor);
    return 0;
}

Here is the server code (Windows)

#include <winsock.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
const int BufLen = 1024;
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();
}

As said before, when doing the WSAGetLastError() I receive the 10049 error and I do not know how to solve it. Thanks in advance.


Solution

  • bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
    recvfrom(RecvSocket,RecvBuf, 5, 0,(SOCKADDR *)&SenderAddr,&SenderAddrSize);
    ...
    int i=sendto(RecvSocket,SendBuf,strlen(SendBuf)+1,0,(SOCKADDR *) &RecvAddr,sizeof(RecvAddr));
    

    The destination you have in the sendto call is the servers address and not the clients address. This means you essentially set sender and recipient of the data to be the same. Since this makes no sense you get error 10049 (WSAEADDRNOTAVAIL). To fix it you need to use SenderAddr instead of RecvAddr as destination.