Connecting to my computer from elsewhere

This is a discussion on Connecting to my computer from elsewhere within the Windows Programming forums, part of the Platform Specific Boards category; This doesn't do any port forwarding, it just updates my ip record in the server for resolving the right ip ...

  1. #16
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This doesn't do any port forwarding, it just updates my ip record in the server for resolving the right ip address. It didn't cause my program to receive data sent to my external ip address.
    Last edited by maxorator; 05-02-2006 at 10:08 AM.

  2. #17
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    All DynDNS.com does is constantly monitor your "external" ip address and assign it to the stable URL that you have selected.

    Thus, you'll have to use your stable URL in your remote client which in turn will use the gethostbyname function to convert the URL into your "external" ip address to establish a connection.

    Also, the "server" code you posted implies that it being run on a non networked local machine by the use of a loopback address of 127.0.0.1

    You may want to change that to:

    Code:
     service.sin_addr.s_addr = inet_addr = INADDR_ANY;
    INADDR_ANY gets the ip address of the server computer.

  3. #18
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    My program still doesn't receive data sent to external ip address.
    And did you mean:
    Code:
    service.sin_addr.s_addr = INADDR_ANY;
    ???

  4. #19
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Hopefully, you can humor me a little bit and compile/run the following sample apps. Please note that the command line input for the client is the static URL that you have created in your DynDNS account. Also, the port number must be the same on both client and server. BTW, I have tested this minimal client/server app with DynDNS and it works without any problems.

    Code:
    // Usage Server.exe 27015
    #include <stdio.h>
    #include <winsock.h>
    
    void Server(int iPort);
    
    int main(int argc, char **argv)
    {
        WORD wVersionRequested = MAKEWORD(1,1);
        WSADATA wsaData;
        int iPort;
        if (argc != 2)
        {
            printf("Usage: %s PortNumber\n", argv[0]);
            return -1;
        }
        iPort = atoi(argv[1]);
        WSAStartup(wVersionRequested, &wsaData);
        Server(iPort);
        WSACleanup();
        return 0;
    }
    
    void Server(int iPort)
    {
        int iRet;
        int iLen;
        char szBuf[256] = {0};
        SOCKET  listenSocket;
        SOCKET  remoteSocket;
        SOCKADDR_IN ServerSa;
    
        listenSocket = socket(AF_INET,         
            SOCK_STREAM,     
            IPPROTO_TCP);    
        if (listenSocket == INVALID_SOCKET)
        {
            printf("socket()error \n");
            return;
        }
        ServerSa.sin_family = AF_INET;
        ServerSa.sin_addr.s_addr = INADDR_ANY;  
        ServerSa.sin_port = htons(iPort);       
        iRet = bind(listenSocket,              
            (LPSOCKADDR)&ServerSa,      
            sizeof(struct sockaddr));  
        if (iRet == SOCKET_ERROR)
        {
            printf("bind()\n");
            closesocket(listenSocket);
            return;
        }
        iLen = sizeof(SOCKADDR);
        iRet = gethostname(szBuf, sizeof(szBuf));
        if (iRet == SOCKET_ERROR)
        {
            printf("gethostname()\n");
            closesocket(listenSocket);
            return;
        }
        printf("\nServer named %s waiting on port %d\n",
            szBuf, iPort);
        printf("\nlisten()");
        iRet = listen(listenSocket,                
            SOMAXCONN);                  
        if (iRet == SOCKET_ERROR)
        {
            printf("listen() error\n");
            closesocket(listenSocket);
            return;
        }
        printf("\nBlocking at accept()");
        remoteSocket = accept(listenSocket,         
            NULL,                 
            NULL);
        if (remoteSocket == INVALID_SOCKET)
        {
            printf("accept() error\n");
            closesocket(listenSocket);
            return;
        }
        memset(szBuf, 0, sizeof(szBuf));
        iRet = recv(remoteSocket,               
            szBuf,                      
            sizeof(szBuf),              
            0);                         
        if (iRet == INVALID_SOCKET)
        {
            printf("recv()\n");
            closesocket(listenSocket);
            closesocket(remoteSocket);
            return;
        }
        printf("\nData received from client: %s", szBuf);
        strcpy(szBuf, "This came from the Server");
        iRet = send(remoteSocket,               
            szBuf,                      
            strlen(szBuf),              
            0);                         
        closesocket(remoteSocket);
        closesocket(listenSocket);
        return;
    }
    Code:
    // Usage: Client.exe  www.maxurl.com  27015
    //
    #include <stdio.h>
    #include <winsock.h>
    
    void Client(char *szServer, int iPort);
    
    int main(int argc, char **argv)
    {
        WORD wVersionRequested = MAKEWORD(1,1);
        WSADATA wsaData;
        int iPort;
        if (argc != 3)
        {
            printf("Usage: %s UrlName PortNumber\n", argv[0]);
            return - 1;
        }
        iPort = atoi(argv[2]);
        WSAStartup(wVersionRequested, &wsaData);
        Client(argv[1], iPort);
        WSACleanup();
        return 0;
    }
    
    void Client(char *szServer, int iPort)
    {
        LPHOSTENT lpHostEntry;
        int iRet;
        char szBuf[256] = {0};
    
        printf("\nStream Client connecting to server: %s on port: %d",
            szServer, iPort);
    
        lpHostEntry = gethostbyname(szServer);
        if (lpHostEntry == NULL)
        {
            printf("gethostbyname()error\n");
            return;
        }
        SOCKET  ClientSocket;
    
        ClientSocket = socket(AF_INET,      
            SOCK_STREAM,     
            IPPROTO_TCP);    
        if (ClientSocket == INVALID_SOCKET)
        {
            printf("socket() error\n");
            return;
        }
        SOCKADDR_IN saServer;
        saServer.sin_family = AF_INET;
        saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
        saServer.sin_port = htons(iPort);   
        iRet = connect(ClientSocket,                
            (LPSOCKADDR)&saServer,   
            sizeof(struct sockaddr));
        if (iRet == SOCKET_ERROR)
        {
            printf("socket()\n");
            closesocket(ClientSocket);
            return;
        }
        strcpy(szBuf, "This came from the Client");
        iRet = send(ClientSocket,               
            szBuf,                  
            strlen(szBuf),          
            0);                 
        if (iRet == SOCKET_ERROR)
        {
            printf("send()error\n");
            closesocket(ClientSocket);
            return;
        }
        iRet = recv(ClientSocket,           
            szBuf,              
            sizeof(szBuf),      
            0);                 
        if (iRet == SOCKET_ERROR)
        {
            printf("recv() error\n");
            closesocket(ClientSocket);
            return;
        }
        printf("\nData received from Server: %s", szBuf);
        closesocket(ClientSocket);
        return;
    }

  5. #20
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    DynDNS provides a means of attaching a domain name to your IP, even if your IP changes. It does not equal port forwarding.

    Say he has a network, with two machines behind a router. Both are running servers, on the same port. If data from the Internet arrives at the router, bound for a server on that port, how is the router supposed to know who to send it to? That's what port forwarding does, and is configured on the router.
    For those two computers behind the router, to the outside world, they have the same IP address - that of the router.

    Changing your code to INADDR_ANY should help. All the examples I seen / used have run the port through htons(), and some run INADDR_ANY through htonl(). (Not sure if it's recommended to htonl(INADDR_ANY), but since it's defined as 0 on my system, it won't make a difference. This gives an example that does.)

    Finally, if your code still doesn't work, figure out if it's port forwarding or your code. Try forwarding some other server and connecting to it using your external IP briefly to see if you're setting up port forwarding correctly.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. connecting to a computer inside a LAN
    By hardi in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-25-2006, 04:43 PM
  2. Replies: 34
    Last Post: 02-26-2006, 12:16 PM
  3. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  4. tcp/ip not connecting to my computer
    By Rune Hunter in forum C# Programming
    Replies: 11
    Last Post: 03-14-2005, 03:26 PM
  5. connecting to another computer
    By yinhowe in forum C Programming
    Replies: 4
    Last Post: 11-19-2002, 02:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21