Thread: tcp connection without connecting to a server

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    43

    tcp connection without connecting to a server

    Is it possible to set up a connection between two computers without one acting as the host and accepting a socket connection from the client? I'm trying to get around having to forward ports so that it can be run on public wifi connections where port forwarding is not an option.

    At first I thought this was impossible but then I started thinking about torrents. It seems that torrents are able to transfer data between peers without doing any kind of port forwarding. I assume this happens without everything going through some kind of central server.

    Is this possible? Maybe would I need to setup a server to get the 2 clients together and then they could transfer data between one another without going through the server?

    Any hints or advice is greatly appreciated. Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is this possible? Maybe would I need to setup a server to get the 2 clients together and then they could transfer data between one another without going through the server?
    Yes, that is why they're called "peer to peer" (p2p) connections, and not client-server connections.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Alright, any advice on where to start?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try searching for "peer-to-peer programming" or "peer-to-peer in c" (or c++ or whatever).

    EDIT: Found this. libjingle
    Last edited by oogabooga; 10-13-2012 at 08:38 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    43
    Well from what I have read I don't think what I want to do is possible. I want a connection between 2 computers but without one listening then accepting the connection as a host so I just don't think its possible. If you are behind a router you have to forward ports to be able to do this.

    My understanding of peer to peer is that some of the computers in the network have to listen on a port and accept connections in order for the network to work and I can't have that. Am I wrong? I don't want to use a library so if im wrong please point me in the right direction
    Last edited by elmutt; 10-13-2012 at 01:42 PM.

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Put it this way - you need one computer to send data to another. So one of those machines has to listen out for data coming in and, if there's something in the way like a router or firewall, it won't hear anything.

    P2P works by connecting to already-known peers who are listening out. Where those machines are behind routers, they talk OUT to other people who aren't and thus join the network indirectly (so they ask the other side to pass on messages on their behalf). You can have networks that have only one listening peer but AT SOME POINT one of them had to be listening to establish all those connections.

    A TCP connection, by design, has a source and destination. If the destination is not accepting unknown TCP connections from outside (the problem you describe), then you can't do anything to communicate with them without THEM trying to communicate with you. And that needs YOU to accept unknown TCP connections from outside.

    There are various "NAT hole-punching" techniques but they all rely, at some point, on an intermediary server who accepts connections. It might be just the one (a "central" server), or there might be a proportion of users who have open connections and who will pass on data for others ("P2P") - especially if there's an incentive for them to open ports like "getting more connections to download from". But, at the end of the day, in a pure-NAT environment nothing TCP or UDP would work because nobody would be accepted unsolicited packets.

    This is *WHY* port-forwarding exists. The only other workaround is to nominate an external machine without NAT that accepts TCP connections and acts as a proxy, maybe in a variety of clever ways to let NAT users use it as an intermediary. We usually call those "servers" and you don't tend to NAT servers and if you do you need to port-forward things to them so people can reach them. In P2P, it just so happens that the server is also a client that someone else is running to be "nice" to you.

    Look at the TCP handshake process. It inherently relies on a new (SYN) connection being replied to by a server with an acknowledgement. Look at NAT - it's basic tenet is that it doesn't reply to packets connections that aren't part of its existing (outgoing) connections (usually because it has absolutely NO WAY to determine which local client they were intended for!). All a router does is put a NAT interface in the way so your whole local network can appear as, and share, a single external IP. The two concepts mean there's no way to have full communication between NAT'd clients without someone, somewhere accepting unsolicited (SYN) packets and acting on them. That means either no NAT at all, or a port-forward through a NAT.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connection between proxy server and the server
    By vbx_wx in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-07-2011, 01:51 PM
  2. Connecting to Server
    By osal in forum Networking/Device Communication
    Replies: 58
    Last Post: 06-10-2004, 10:10 AM
  3. Connecting to sql database on server
    By jverkoey in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 06:56 PM