1
votes

Below you can see my code that implements a pretty basic UDP sender in C++ with Winsock. The thing is that no matter how many times I run the code, the socket (the listenSocket) gets bound to a different UDP port. Is there any specific reason for this? Am I doing some mistake in my code?

thanks

#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])
{
    WSADATA wsaData;    
    SOCKADDR_IN myAddress;
    SOCKADDR_IN targetAddress;

    int myPort = 60888;
    const char *myIP = "192.168.0.1";
    int remotePort = 2048;
    const char *remoteIP = "192.168.0.2";

    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET SendSocket = INVALID_SOCKET;
    SOCKET acceptSocket;

    char cBuffer[1024] = "Test Buffer";
    int nBytesSent = 0;
    int nBufSize = strlen(cBuffer);
    int iResult;

    // Initialize Winsock
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr<<"Socket Initialization: Error with WSAStartup\n";
        system("pause");
        WSACleanup();
        exit(10);
    }

    ListenSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (ListenSocket == INVALID_SOCKET or SendSocket == INVALID_SOCKET)
    {
        cerr<<"Socket Initialization: Error creating socket"<<endl;
        system("pause");
        WSACleanup();
        exit(11);
    }

    //bind
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr(myIP);
    myAddress.sin_port = htons(myPort);

    targetAddress.sin_family = AF_INET;
    targetAddress.sin_addr.s_addr = inet_addr(remoteIP);
    targetAddress.sin_port = htons(remotePort);

    if(bind(ListenSocket, (SOCKADDR*) &myAddress, sizeof(myAddress)) == SOCKET_ERROR)
    {
        cerr<<"ServerSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(14);
    }
    else
        printf("Server: bind() is OK.\n");

    nBytesSent = sendto(SendSocket, cBuffer, nBufSize, 0,
                 (SOCKADDR *) &targetAddress,
                 sizeof(SOCKADDR_IN));    

    printf("Everything is ok\n");

    system("PAUSE");

    closesocket(ListenSocket);
    closesocket(SendSocket);

    return EXIT_SUCCESS;
}

EDIT: Maybe I was not so clear. What I do with this code is to send some data to a remote PC. But what is required is that the UDP segments should appear to be originated from a specific port. How can this be done? Is it wrong what I'm doing here? Now that I'm thinking of it, I guess it is wrong indeed. The SendSocket and ListenSocket don't have any connection, correct? So, how can I make it that the UDP segments appear to originate from a specific UDP port? Thanks!

1

1 Answers

3
votes

You are not calling bind() on SendSocket before sending data with it, so WinSock is free to bind that socket to whatever random local IP/Port it needs to. If you have to send data with a specific source IP/Port every time, you have to bind() to that IP/Port first. If that local IP/Port is the same pair you are binding ListenSocket to, then you don't need to use two separate sockets to begin with. You can send data with the same socket that is listening for incoming data.