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; ***Oops, posted in the wrong forum. I compiled a program using a code piece i found from MSDN. Code: #include ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Connecting to my computer from elsewhere

    ***Oops, posted in the wrong forum.

    I compiled a program using a code piece i found from MSDN.
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include "winsock2.h"
    
    int main(){
    // Initialize Winsock.
        WSADATA wsaData;
        int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
        if ( iResult != NO_ERROR )
            printf("Error at WSAStartup()\n");
    
        // Create a socket.
        SOCKET m_socket;
        m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    
        if ( m_socket == INVALID_SOCKET ) {
            printf( "Error at socket(): %ld\n", WSAGetLastError() );
            WSACleanup();
            return 0;
        }
    
        // Bind the socket.
        sockaddr_in service;
    
        service.sin_family = AF_INET;
        service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
        service.sin_port = htons( 27015 );
    
        if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
            printf( "bind() failed.\n" );
            closesocket(m_socket);
            return 0;
        }
        
        // Listen on the socket.
        if ( listen( m_socket, 1 ) == SOCKET_ERROR )
            printf( "Error listening on socket.\n");
    
        // Accept connections.
        SOCKET AcceptSocket;
    
        printf( "Waiting for a client to connect...\n" );
        while (1) {
            AcceptSocket = SOCKET_ERROR;
            while ( AcceptSocket == SOCKET_ERROR ) {
                AcceptSocket = accept( m_socket, NULL, NULL );
            }
            printf( "Client Connected.\n");
            m_socket = AcceptSocket; 
            break;
        }
        
        // Send and receive data.
        int bytesSent;
        int bytesRecv = SOCKET_ERROR;
        char sendbuf[32] = "Server: Sending Data.";
        char recvbuf[32] = "";
        
        bytesRecv = recv( m_socket, recvbuf, 32, 0 );
        printf( "Bytes Recv: %ld\n", bytesRecv );
        
        bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
        printf( "Bytes Sent: %ld\n", bytesSent );
    
        WSACleanup();
        Sleep(1000);
        return 0;
    }
    If I port scan 127.0.0.1 from my own computer, it works but what address do I need to use to connect to it from elsewhere, for example a web server?

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Maybe I fail to understand your question, but it seems that you don't really understand this code. You won't be able to run that code and "connect" to your computer from an outside address. (?)

    But to answer: Run ipconfig to find your ip address.

    And in addition... Moved to correct forum.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>But to answer: Run ipconfig to find your ip address.

    As long as he isn't connected to a local network. Otherwise, there's sites like http://www.showmyip.com
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You have a memory leak because you throw away m_socket without calling closesocket on it.

    EDIT
    If he is on a local network he might still not be able to connect with the external IP if port forewarding isn't set up.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I tried to connect with my external ip address but it didn't work. How can I get it working? What settings do I need to change? How can I turn on that "port forwarding"?
    Last edited by maxorator; 04-28-2006 at 03:10 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Port forwarding is done by your router. How to set it up depends on the type of router that you have. Basicaly you need to set your router to forward a particular port to your internal IP (the one you get from ipconfig).

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Yay, I made the port forwarding thing, I don't know if it works yet, but I am going to try!

    Edit: Sniff... Didn't get it working...
    Last edited by maxorator; 04-28-2006 at 12:41 PM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    You may want to consider using a means to connect a stable URL to a dynamic IP address.

    This can be done as follows:

    1. Create a free account with a Dynamic DNS service such as DynDNS

    2. Use a router capable of port forwarding such as the Cisco Linksys BEFSR43. Configure it for port forwarding.

    3. Modify your client (front end) to resolve hostnames

    Essentially, you can use the hostname from the account you set up to access your backend server from anywhere in the world at any time. The dynamic service provider keeps track of your dynamic IP address.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Isn't port forwarding possible with my router (Ericsson HN294dp)?
    Are you sure I can buy a Cisco router from Europe (Estonia)?

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Not all routers have built in dynamic DNS (port forwarding). If your router does NOT have this feature, then DynDNS can provide free software to be installed on any computer inside your network to accomplish this port forwarding task. Thus, there is absolutely no need to purchase another router. The Cisco router was just an example that I used.

    BTW, there are a couple of other companies in addition to DynDNS that provide this same free basic service. No-Ip.com is one that comes to mind.

    Sorry about any confusion I may have caused.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thank-you for your help.
    I didn't find the software for port forwarding from dyndns.com and no-ip.com, so can you please find a download link for that?

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Here is a link to the DynDNS update client software...

    DynDNS.com client software download

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Looks like I can't do port forwarding with that program...

  15. #15
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Looks like I can't do port forwarding with that program...
    Can you provide some details as to why you can't use it? I have used DynDNS both ways without any problems?

Page 1 of 2 12 LastLast
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, 05:43 PM
  2. Replies: 34
    Last Post: 02-26-2006, 01:16 PM
  3. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 11:58 PM
  4. tcp/ip not connecting to my computer
    By Rune Hunter in forum C# Programming
    Replies: 11
    Last Post: 03-14-2005, 04:26 PM
  5. connecting to another computer
    By yinhowe in forum C Programming
    Replies: 4
    Last Post: 11-19-2002, 03:14 AM

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