'Send and Receive Live data using UDP Socket

I am practicing socket programming. Here is my server code.

Server.cpp

/* UDP Server Sample program*/
#include <Windows.h>
#include <iostream>
#include <winsock.h>
using namespace std;
int main()
{
    // Local Variable definitions

    cout << "\t\t------- UDP Server---" << endl;
    cout << endl;

    WSADATA   WinSockData;
    int       iWsaStartup;
    int       iWsaCleanup;

    SOCKET    UDPSocketServer;
    struct    sockaddr_in UDPClient;

    char      Buffer[512];
    int       iBufferLen = strlen(Buffer) + 1;

    int       iBind;
    int       iReceiveFrom;

    int       iUDPClientLen = sizeof(UDPClient);
    int       iCloseSocket;

    // STEP-1 Initialization of Winsock
    iWsaStartup = WSAStartup(MAKEWORD(2, 2), &WinSockData);

    if (iWsaStartup != 0)
    {
        cout << "WSAStartUp Fun Failed" << endl;
    }
    cout << "WSAStartUp Success" << endl;

    //STEP-2 Fill the UDPClient(SOCKET ADDRESS) Structure
    UDPClient.sin_family = AF_INET;
    UDPClient.sin_addr.s_addr = inet_addr("169.254.131.8");
    UDPClient.sin_port = htons(8001);

    // STEP-3 Socket Creation
    UDPSocketServer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (UDPSocketServer == INVALID_SOCKET)
    {
        cout << "Socket Creation Failed " << endl;
        cout << "Error No-> " << WSAGetLastError() << endl;
    }
    cout << "Socket Creation Success" << endl;
    // STEP-4 bind the server
    iBind = bind(
        UDPSocketServer,
        (SOCKADDR*)&UDPClient,
        sizeof(UDPClient));
    if (iBind == SOCKET_ERROR)
    {
        cout << "Binding Failed " << endl;
        cout << "Error No-> " << WSAGetLastError() << endl;
    }
    cout << "Binding Success" << endl;
    //STEP-5 RecvFrom Fun from receive data from client
    while (1)
    {
        iReceiveFrom = recvfrom(
            UDPSocketServer,
            Buffer,
            iBufferLen,
            MSG_PEEK,
            (SOCKADDR*)&UDPClient,
            &iUDPClientLen);

        if (iReceiveFrom == SOCKET_ERROR)
        {
            cout << "Receiving failed " << endl;
            cout << "Error No-> " << WSAGetLastError() << endl;
        }
        cout << "Receiving Success" << endl;
        cout << "Receive Data -> " << Buffer << endl;
    }

    //STEP-6 CloseSocket Function
    iCloseSocket = closesocket(UDPSocketServer);

    if (iCloseSocket == SOCKET_ERROR)
    {
        cout << "Socket Closing failed " << endl;
        cout << "Error No-> " << WSAGetLastError() << endl;
    }
    cout << "Socket CLosing Success" << endl;

    //STEP-7 WSACLeanUp Fun for Terminating the use of DLL
    iWsaCleanup = WSACleanup();
    if (iWsaCleanup == SOCKET_ERROR)
    {
        cout << "WSA CleanUp failed " << endl;
        cout << "Error No-> " << WSAGetLastError() << endl;
    }
    cout << "WSA CleanUp Success" << endl;

    system("PAUSE");
    return 0;
}

and this is client code...

client.cpp



/*All right reserved to awinsyspro.com 2019*/
/* UDP Server Sample program*/

#include <Windows.h>
#include <winsock.h>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    cout << "\t\t------UDP Client------" << endl;
    cout << endl;
    // Local Variable
    WSADATA   WinSockData;
    int       iWsaStartup;
    int       iWsaCleanup;

    SOCKET               UDPSocketClient;
    struct  sockaddr_in  UDPServer;


    // STEP-1 Initialization of Winsock

    iWsaStartup = WSAStartup(MAKEWORD(2, 2), &WinSockData);
    if (iWsaStartup != 0)
    {
        cout << "WSAStartup Failed = " << iWsaStartup << endl;
    }
    cout << "WSAStartup Success" << endl;

    // STEP-2 Fill the UDPServer Structure
    UDPServer.sin_family = AF_INET;
    UDPServer.sin_addr.s_addr = inet_addr("169.254.131.8");
    UDPServer.sin_port = htons(8001);

    // STEP-3 Socket Creation

    UDPSocketClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (UDPSocketClient == INVALID_SOCKET)
    {
        cout << "Socket Creation Failed " << endl;

        cout << "Error No-> " << WSAGetLastError() << endl;
    }

    cout << "UDP Socket Creation Success" << endl;

    int integ = 2656;
    //char Buffer[512] = strint;
    int  iCloseSocket;
    while (1)
    {
        //STEP-4 Sendto Fun.
        string strint = to_string(integ);
        const char* Buffer = strint.c_str();
        int iSendto;

        int  iBufferLen = strlen(Buffer) + 1;
        int  iUDPServerLen = sizeof(UDPServer);

        cout << integ << endl;
        iSendto = sendto(
            UDPSocketClient,
            Buffer,
            iBufferLen,
            MSG_DONTROUTE,
            (SOCKADDR*)&UDPServer,
            sizeof(UDPServer));

        if (iSendto == SOCKET_ERROR)
        {
            cout << "Sending Data Failed " << endl;
            cout << "Error No->" << WSAGetLastError() << endl;
        }
        cout << "Sending Data Success" << endl;
        integ = integ + 1;

        // STEP-5 CloseSocket Function
    }
    iCloseSocket = closesocket(UDPSocketClient);
    if (iCloseSocket == SOCKET_ERROR)
    {
        cout << "Socket Closing failed " << endl;

        cout << "Error No-> " << WSAGetLastError() << endl;
    }

    cout << "Close Socket Success" << endl;

    // STEP-6 WSACleanUp fun for Terminating the Winsock DLL

    iWsaCleanup = WSACleanup();
    if (iWsaCleanup == SOCKET_ERROR)
    {
        cout << "WSA CleanUp failed " << endl;

        cout << "Error No-> " << WSAGetLastError() << endl;
    }
    cout << "Cleanup Success" << endl;
0
    system("PAUSE");
    return 0;
}

I want to send integer data from the client by an increment of 1 after each loop and receive the respective value on the server side. But I am receiving only the constant integer value "0". I don't know how to do this task. Thank you



Solution 1:[1]

   char      Buffer[512];

This declares a char array in automatic storage. This char array is completely uninitialized. The next statement:

   int       iBufferLen = strlen(Buffer) + 1;

This attempts to determine the size of the character string in Buffer, by searching for its terminating \0 byte. That's what strlen() does, after all. It is clear that the intent here is to set iBufferLen to 512, or sizeof(Buffer), instead of to the size of its current string, plus 1, which is obviously incorrect.

Since Buffer is uninitialized, does not contain any character string, and would likely contain random binary garbage, from this point on the shown code is undefined behavior.

If you have not yet learned how to use a debugger, you should consider this to be an excellent opportunity to learn how to do that. If you were to have used your debugger to run your program, one line at a time, your debugger would've immediately shown you that iBufferLen got likely set to 1 or 0, instead of 512.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sam Varshavchik