Thread: Connect to external IP

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Connect to external IP

    Hi.
    I have made a chatprogram,... which works fine on the newtwork using local IP adresses.

    What modifications do i have to do on my code to make it work with external IP adresses`?
    (Using Win32API, C++, Winsock)

    EDIT: To make my self clearer:
    What I want is to make my program usable if the server and client is behind a local router.
    Last edited by Da-Nuka; 03-13-2005 at 12:03 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Winsock is not concern that you're behind a router/firewall. It just cares about the IP, port, protocol, etc. In other words, you pass it the correct IP (internal, external, etc.) and a port number.

    Kuphryn

  3. #3
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Then i must have a bug somehwere.. I have looked everywhere, but i cant find it... ):

    I include it if someone really wanna help..

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Then i must have a bug somehwere
    Are you going to give us any kind of clue as to why you think there's a bug? Where do you think the program is going wrong?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Are you forwarding the relevant port(s) on your router?

  6. #6
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    well, no. Actually, what is port-forwarding?
    If it got anything to do with configuration outside my application, is it something I can do to make my simple chat-server/client program work as easy as the MSN-Messenger?
    (No port-forwarding or firewall stuff usualy needed there).

    and ONE more question.. If winsock just takes a IP to connect to..
    What if 2 computers are behind a local router?
    Who of them will then recive the package?(As they have the same external IP)

    If not, can I say that: If LAN-testing goes fine, my APP is ready for internet too?

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    By local router do you mean like a router that does nat for your local network?!

    In that case your server can't be part of that nat network, unless you enable "port forwarding" for the server (configure the router to forward to the server's local address and it's port.)

    example:
    extrenal server's address: 123.1.1.1 --> as known by the world, also the router's address to the world!
    local server's address: 192.168.1.1 --> as known only by local network.
    listening port: 12345

    when you try to connect 123.1.1.1 on port 12345 you're not trying to connect the server, but the router, so this will not work!
    if you configure the router to forward all connections for port 12345 to local address 192.168.1.1, you can make server to listen for external connections even if it's local address is not known by any external router in the world!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Da-Nuka
    and ONE more question.. If winsock just takes a IP to connect to..
    What if 2 computers are behind a local router?
    Who of them will then recive the package?(As they have the same external IP)
    and about that one... i suggest you google the word NAT.
    the main idea that you have one REAL address that the entire world can reach, and you're using a device, in that case a router to do NAT.
    which means: the entire world will see the REAL address of the router, while the router plays with local ports and remembers from which local address came each and every packet, so it will know where to forward the connection to.

    And as I said, unless you configure the router to forward by port you can only connect from with in the local LAN to the outside, and NOT from the outside to local LAN.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    So...Getting things like portforwarding, router config and firewall config isnt the programmers job?

    So if it works fine on LAN/Network, the job is finished, even for internet?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Da-Nuka
    So...Getting things like portforwarding, router config and firewall config isnt the programmers job?
    No it is up to the user to decide what he wants to allow past his firewall but if you want your program to configure a firewall/router you could use Microsoft UPnP if the remote firewall/router suports it.

    Quote Originally Posted by DA-Nuka
    So if it works fine on LAN/Network, the job is finished, even for internet?
    Pretty much yes. One difference is that on a LAN there is going to be a lot less latency then on the ineternet, some programs need to be aware of that. For example:

    If you want to receive 1024 bytes of data
    Code:
    recv(TheSocket, Buffer, 1024, 0);
    This might work fine on a lan because the data can all arive at once but on the inernet it may not, infact it probably wont.
    Last edited by Quantum1024; 03-16-2005 at 05:55 AM.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i disagree on that recv line over there... if you're saying that this will not work then i think you're wrong, it will work only not the entire 1024 bytes will received at once, you will have to call the recv more than once in order to receive the entire 1024.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Yes, but each time you call recv, it's important to check the return value, as that will be the number of bytes actually written to your buffer.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    yes! either that or you can simply use select if you don't know the exact amount of data to receive and also so you will not fall on the exact amount of bytes as you are trying to receive at once with a single recv call.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I don't see what the use of select() has to do with telling how much data is available to be read, other than telling you that there is at least 1 byte (or EOF/error) available.
    http://faq.cprogramming.com/cgi-bin/...80608#Mistake3
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Devil Panther
    i disagree on that recv line over there... if you're saying that this will not work then i think you're wrong, it will work only not the entire 1024 bytes will received at once, you will have to call the recv more than once in order to receive the entire 1024.
    My point was to show how that recv call could seam to work fine on a LAN because there is so little latency that the data can arive at once however on the internet it wont all be received at once so multiple calls are needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. IP without connecting to external socket
    By Hunter2 in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-26-2003, 07:50 AM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM