-1

I am trying to write some client code to send a UDP packet to another computer at a particular IP address. When the IP address was set to home it worked just fine. I could use wireshark to see the packets being sent. When I put the server's IP in I didn't see any packets. Upon further investigation I found I was getting an 10049 error when I tried to bind to the socket. I don't quite get why I can't bind to the IP of another computer on the network.

    string m_port = "2033";             //  port that the plugin is talking to
    string m_hostIP = "192.168.2.27";           //  IP address that the plugin is talking to

    //  start Windows Sockets
    if ((error = WSAStartup(version, &data)) != NO_ERROR)
    {
        logError("Error: WSAStartup failed with error: ", error);
        return;
    }
    // check if version 2.2 of Winsock is supported
    if (LOBYTE(data.wVersion) != 2 || HIBYTE(data.wVersion) != 2)
    {
        logError("Error: System could not support WinSock 2.2.", 0);
        // clean up Winsock
        if (WSACleanup() == SOCKET_ERROR)
        {
            logError("Error: Winsock cleanup error: ", WSAGetLastError());
        }
        return;
    }

    //  create a datagram socket using IPv4 and UDP protocol
    if ((m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
    {
        logError("Error: Socket creation failed with error: ", WSAGetLastError());
        //  clean up Winsock
        if (WSACleanup() == SOCKET_ERROR)
        {
            logError("Error: Winsock cleanup error: ", WSAGetLastError());
        }
        return;
    }

    int iSetOption = 1;
    if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption, sizeof(iSetOption)) == SOCKET_ERROR)
    {
        logError("Error: Set Socket Options failed with error: ", WSAGetLastError());

        //  close the socket
        if (closesocket(m_socket) == SOCKET_ERROR)
        {
            logError("Error: Unable to close socket: ", WSAGetLastError());
        }
        //  clean up Winsock
        if (WSACleanup() == SOCKET_ERROR)
        {
            logError("Error: Winsock cleanup error: ", WSAGetLastError());
        }

        m_socket = INVALID_SOCKET;
        return;
    }

    struct sockaddr_in server_addr;             //  server address description struct
    int server_addr_len = sizeof(server_addr);  //  size of server address struct
    int bytesSent = 0;

    memset((char *)&server_addr, 0, server_addr_len);           //  clear the address struct
    server_addr.sin_family = AF_INET;                           //  IPv4 address type
    server_addr.sin_port = htons((unsigned short)stoul(m_port));    //  port used by the simulator to send the data
    server_addr.sin_addr.s_addr = inet_addr(m_hostIP.c_str());          //  any server IP address will do

    //  bind the socket to the address struct
    if (::bind(m_socket, (struct sockaddr *) &server_addr, server_addr_len) == SOCKET_ERROR)
    {
        logError("Error: unable to bind the socket: ", WSAGetLastError());
        //  close the socket
        if (closesocket(m_socket) == SOCKET_ERROR)
        {
            logError("Error: Unable to close socket: ", WSAGetLastError());
        }
        // clean up Winsock
        if (WSACleanup() == SOCKET_ERROR)
        {
            logError("Error: Winsock cleanup error: ", WSAGetLastError());
        }

        m_socket = INVALID_SOCKET;
        return;
    }
user207421
  • 305,947
  • 44
  • 307
  • 483
cHorse
  • 11
  • 1
  • 8
  • You bind a socket to a local address. You connect it, or send, to a remote address. You don't need to bind a client socket at all. Just remove it. – user207421 Dec 10 '19 at 23:19
  • *"some client code"* - that should tell you you're using the wrong function. `bind` is typically used for binding sockets to the local host address(es). `connect` is used to connect sockets to addresses on remotes. And for UDP you may want to use `sendto` regardless. – WhozCraig Dec 10 '19 at 23:24
  • Why you're defining the port as a string instead of an integer is another mystery. – user207421 Dec 11 '19 at 00:41

1 Answers1

0

You bind socket to choose from which local endpoint (address+port) you send data.

Servers may have several network cards that connect you to internet or you may have several networks - by binding you choose from which network card (or whatever it actually is) you send your data.

Usually PCs have just a few workable address (local host and network addresses) but you can also specify the port. port can be used to filtrate unwanted packets which can be helpful when you want to create several sockets.

The other operation is called connect - choosing to which remote endpoint you send data. I don't remember exact details how it works on winsocket/udp. It might be unnecessary and you simply send the data instead and specify each time where you send it.

ALX23z
  • 4,456
  • 1
  • 11
  • 18