Thread: 10 possible ports for remote application

  1. #1
    Registered User
    Join Date
    May 2004

    10 possible ports for remote application

    Hi all, I have a server program which starts up on any one of the 10 ports from 13337 to 13346.

    I just opened a socket with this:
    // create the socket
    socket = ::socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    if (socket == INVALID_SOCKET)
    throw 1;
    // bind to the local address
    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("");
    address.sin_port = 0; // choose any
    if (bind( socket, (sockaddr *)&address, sizeof(address) )) {
    and then I used a for-loop to try each of those ports:
    for (int port = remoteFirstPossiblePort; port < remoteLastPossiblePort; port++) {
        cout << "broadcasting!" << endl;
        struct Hello {
            int blarg;
        Hello hello = { 1337 };
        sockaddr_in dest;
        dest.sin_family = AF_INET;
        dest.sin_addr.s_addr = inet_addr(remoteIPAddress);
        dest.sin_port = htons(port);
        int ret = sendto( socket, (const char *)&hello, sizeof(hello), 0, (sockaddr *)&dest, sizeof(dest) );
        if (ret != sizeof(Hello)) {
            throw 1;
    But it seems, when sendto sends a packet to a remote port that's not opened, any subsequent recvfrom calls return the error WSAECONNRESET.

    So two questions:

    1. Am I going about this the right way? I'm using one socket to try for different remote ports, is that what I should do here?

    2. If I am on the right track, I need to figure out a way to disable this error for this socket. Any ideas?


  2. #2
    Registered User
    Join Date
    Jun 2010
    In a house
    address.sin_port = 0;

    This is your problem. If you want specific port then change it to something else. Other wise it will be different port every time.

  3. #3
    Registered User
    Join Date
    May 2004
    It's not important what port the client's socket is on on the client machine.

    The line where I specify the port on the remote machine, the one that is between 13337 and 13346, is the line:
    dest.sin_port = htons(port);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Howto make own application for remote control handling
    By s-men in forum Windows Programming
    Replies: 16
    Last Post: 08-16-2008, 04:22 PM
  2. Convirt my C program to a GUI application/Web Application
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 07-21-2008, 01:43 AM
  3. COM ports on remote PCs...
    By BrownB in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2006, 11:28 AM
  4. Intercepting Data Bound for Local Application from Remote Server
    By maththeorylvr in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-29-2005, 01:57 AM
  5. wow sub 7? remote?
    By Shadow in forum A Brief History of
    Replies: 10
    Last Post: 05-11-2002, 07:24 PM