Question

I'm trying to write a simple client and server using UDP.

I can send data from the client to the server, and the server recieves it well, but when I send the data from the server to the client, it doesnt work at all... (It doesnt detect anything, and is stuck on recvfrom function..

Here is my Server code :

SOCKET ServerOn()
 {
SOCKET ListenSocket;
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
    exit(0);
}

// Create a SOCKET for listening for
// incoming connection requests.
ListenSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ListenSocket == INVALID_SOCKET) 
{
    WSACleanup();
    exit(1);
}

// The sockaddr_in structure specifies the address family,
// IP address, and port for the socket that is being bound.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("0.0.0.0");
service.sin_port = htons(2583);

if (bind(ListenSocket,(SOCKADDR *) & service, sizeof (service)) == SOCKET_ERROR) 
{
    closesocket(ListenSocket);
    WSACleanup();
    exit(2);
}

return ListenSocket;
}

In this function, I'm initializing the server on port 2583.

Here is my other code in the server :

int size = sizeof(service);
char *data = new char[500];

recvfrom(s,data, 500, NULL, (SOCKADDR*)&service, &size); // Getting a new connection

    sockaddr_in service;
 service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("10.0.0.1");
service.sin_port = htons(2583);

    int addrSize = sizeof(service);
if (sendto(s, "123", 3, NULL, (struct sockaddr*)&service, addrSize) != 3)
    printf("%d", WSAGetLastError()); // Print error if did not send successfully

"10.0.0.1" is the IP of the Client (I made sure it is)... I didnt found a way to get the IP automaticly from the socket, so I'm just putting it right away for now...

Here is my client code :

SOCKET ConnectToServer()
{
//----------------------
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) 
{
    return NULL;
}
//----------------------
// Create a SOCKET for connecting to server
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (ConnectSocket == INVALID_SOCKET)
{
    WSACleanup();
    return NULL;
}
//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr(Default_IP.c_str()); // IP
clientService.sin_port = htons(Default_Port); // Port

//----------------------
// Connect to server.
iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService)); // Connecting
while (iResult == SOCKET_ERROR) // ERROR, could not connect. keep trying
{
    iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService)); // Connecting
}

   return ConnectSocket;
    }

In this code, I'm connecting to the client.

and here is the rest of the client code :

    sockaddr_in service;
 service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("10.0.0.1");
service.sin_port = htons(2583);
s = ConnectToServer();
sendto(s, "123", 3, NULL, (struct sockaddr*)&service, addrSize);

while(true)
{
    result = recvfrom(s, (char*)waveIn, NUMPTS * sizeof(short int), NULL, (struct sockaddr *)&service, &addrSize);

    if (result > 0)
    {
        std::cout << "New Data!" << std::endl;
    }
    else
        printf("%d\n", WSAGetLastError());
}

the sendto function in the client does work, and the server recieves it, though when the server tries to send data back to the client, it recieves nothing, and it is stuck on the recvfrom function.

What am I doing wrong?

P.S - I'm running both client and server from the same computer, which means both has the same IP adress ("10.0.0.1"), but it allways worked for me when using sockets with TCP, so I've did it here too.

though, I did try use this code with 2 different computers, and I still had the same error...

Thanks!

Was it helpful?

Solution

When the server calls recvfrom(), it reports the IP:Port that the data came from. You need to send your reply back to that same IP:Port, eg:

sockaddr_in service;
int size = sizeof(service);
char data[500];

int len = recvfrom(s, data, 500, NULL, (SOCKADDR*)&service, &size);
if (len == -1)
    printf("recv failed: %d", WSAGetLastError()); // Print error if did not recv successfully
else
{
    if (sendto(s, "123", 3, NULL, (struct sockaddr*)&service, size) == -1)
        printf("send failed: %d", WSAGetLastError()); // Print error if did not send successfully
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top