Connecting to peers and how to read/write to a device on another network - sockets

I think I have a fundamental misunderstanding of how networking and peer-to-peer applications work, I would be really appreciative if someone could help clear things up for me.
I am designing a small peer-to-peer file sharing application, and it seems like I can't connect to a device on another network without forwarding a port from the router to that device. This seems rather tedious; if everyone using the application has to forward a port to their device it would make the application unpractical. Is there some way that p2p apps communicate with each other directly, or are p2p applications actually just a set of server and client applications working together to form a network?
In other words, do I need to forward a port on one machine and use it as a server initially, then as the network grows larger just hope that others will do the same? Or is there something I don't know / don't understand about how p2p communication works?

Related

Is it possible to connect two ios devices using sockets by getting their IP addressess?

I want to connect multiple devices through socket without any server implementation.I will use that only for getting the IP addresses of the devices that will register.
There are two major problems to peer-to-peer communications: discovery, and reachability.
First, you need to know the IP address of the other peers to connect to them. Once you're connected to a mesh of peers, they can all keep each other updated on the state of the network, suggesting better peers to each other, passing around notifications of new peers who've joined and left, etc. But you have to design and implement a mechanism for trading that information. More importantly, you need to jumpstart things in some way, because when a new peer starts up, it's in a mesh of just itself, and it has no information to give itself.
One possibility is to have a handful of well-known "superpeers" (that you run) that are always connected, and bake their addresses into the app. Or you can have "introduction servers" instead of peers, serving much the same function. Or you can have some external way of trading addresses (the simplest is that users trade them on a web forum or an IRC channel or in person and type them in manually), which can be automated to various degrees. There are also shortcuts that can help—Bonjour can get other peers onto the mesh as long as one peer on the LAN is already there; GameCenter/GameKit can be used as an automated external trading network; etc.
Once you've solved the discovery problem, you still have the reachability problem. Most iOS devices usually don't have publicly-accessible IP addresses; instead, they're behind routers that do Network Address Translation, whether they be a home WiFi router or a cell carrier's 3G network. This means you need some way to do NAT Hole Punching to get two iPhones talking to each other. Somebody who knows both the public address and the internal address of each device can arrange for them to set up a connection to each other. You can have either normal peers do this (although that makes the jumpstart problem even bigger) or have your superpeers/introduction servers/etc. do it.
If you want to build all of this yourself, you probably want to look at other implementations. BitTorrent (including trackers and DHT) is well-understood and documented at a continuum of levels ranging from "lies-to-children" for curious end users to detailed protocol specs and open source implementations. And then look at some other P2P networks, because BitTorrent is not perfect, and doesn't try to do everything that everyone's come up with.
You can use GameKit. It has the matchmaking api that can help you.
It can be used for non game apps.
I've been working on something similar and it's a giant pain in the ass. There are 3 considerations: 1) Reachability 2) Discovery 3) The connection itself.
1) Don't even consider using 3g/4g, it just won't work well for keeping an open socket connection.
2) I'd use some sort of broker service between the two on the internet to connect the two. For discovery, you can just list what devices are available on the service.
3) For the connection, I find the IOS socket libraries to be rather painful to use, but if you go down to the BSD socket level it's not as bad. I think it'd be very interesting to use zmq sockets; that might simplify writing the broker service.
You can't. If the device is all online with wifi, it maybe possible and rely on the router setting just like pc connect. If some device is connected with 3g or gprs protocal , they may have no ip address at all.

Alternative to using the Bonjour protocol for iPhone peer to peer

I am implementing an iPhone/iPad app that allows users to send each other messages using the bonjour protocol. Basically, a server publishes his service over bonjour and the clients connected to the same wifi can discover his service and connect to it to start sending messages. The problem I've found is that some routers seem to have problems with Bonjour. When running my app at home or at some other places it works perfectly. The publishing and discovering of the bonjour services works flawlessly. However when I tried my app at one of my friend's apartments, some (not all) of the clients were not able to discover the published bonjour service. I also noticed that other apps that use Bonjour (such as Apple's Remote app) were also having trouble on said network. After doing some digging I've found that other people have had similar problems with bonjour and some routers. Therefore I have 2 questions:
1) Does anyone know if this problem with Bonjour that some routers seem to have is a widespread issue? In other words, if my app relies on Bonjour in order to function at all, do I have to be worried that it won't work in 50% of the WLAN networks or do most routers not have any issues with bonjour. Obviously I can't expect anyone to know how every router out there deals with bonjour packets but maybe there are some Networking gurus that can point me in the right direction :-p.
2) Second, if bonjour is too risky of a protocol to build my app on, what are some alternatives with similar features? The features I would (preferably) need would be service publishing and discovery without users having to manually enter IP addresses of other phones.
Ok thanks for your help! I understand that this is kind of a broad question but any help is appreciated! :)
This is potentially a tough nut to crack. The problem causing devices not to find each other on certain networks is related to the underlying transport (i.e. multicast UDP) and not Bonjour (or mDNS or whatever else you wanna call it).
In high-level, non-rigorous terms, multicast data puts more load on routers. Large corporate and university networks sometimes like to drop Bonjour messages (i.e. UDP packets sent to the multicast group 224.0.0.251 on port 5353) because it means the network isn't inundated with thousands of clients advertising their iTunes libraries or whatever, and at scale this can improve general performance. At the other end of the spectrum, some domestic routers drop multicast packets out of the box for reasons known only to the manufacturer. There's not a whole lot you can do about either situation.
Try running a tcpdump on the network to see if the packets are actually coming through. I've not come across a home router that doesn't forward them for a while. If they're not, you'll have to come up with some central [internet-facing, perhaps?] lookup facility. If they're visible, then there's something up with your implementation.
There's nothing stopping you from writing your own simpler protocol for service discovery to be sent over multicast- it's just that there are loads of Bonjour clients in a ton of different languages already written and tested.

iPhone chat application

I have try to make chat application with socket programing and I am using Chatty source code and its working perfect on same network. But not work for different network.
Is chatty work for different network iPhone?
I have tried chatty for different network in iPhone but no success.
Any idea about chatty for different network for iPhone?
From looking at the source code for Chatty, it seems that it just uses an IP and port number to connect to a different device, and that's the reason why you can't connect when users are in a different network. Being able to connect or not will depend a lot on the network configuration you both of your devices have.
For example: it works if you're in the same wifi, because there's no firewall between the devices, and the IP is reachable (192.168.x.x, for example). If you take one of the phones to a 3G network, it won't be able to reach the other one in the wifi, but the one in the wifi might still be able to reach the one on the 3G, depending on your career and port number, and if the network stack is on in the phone. So, conclusion is: this will be highly unreliable.
Usually, for this kind of communication, apps rely on a third party, so both of your phones connect first to a webserver, that is always available on the internet, and the web server sends the messages to each of the apps. This also solves the problem of one app going off the network (when you take an elevator, for example).

How can I simplify addressing an iOS device using Bonjour?

I would like to make an application for iOS devices that would communicate with a special type of remote device using HTTP POST requests. The problem is that this device would actually act in the role of a client, while iOS device has to be the server accepting the requests. The next problem, host name and port has to be configured statically at the device sending the requests.
While I'm aware it's possible to give it a try using Dynamic DNS services and UPnP/IGD or NAT-PMP to overcome the problem with iOS device being behind the NAT router, I was wondering whether using Bonjour could simplify the problem in some way. I'm quite a newbie in networking so I'm looking for the direction and want to avoid research of Bonjour if it's not going to bring me anything.
Any other idea of how to solve the problem is appreciated as well.
I had a think about this. IMHO you can't guarantee you'll always be able to expose an iOS based service over a WAN (although it's possible) so this isn't practical.
The best solution I suggest is using uPNP to open the device behind a router to the outside world, then have it send an iOS push notification to the iPhone with the details (hostname, ip, port, etc).
The iPhone would then would then have to set up some kind of persistent connection with the firewalled device to receive data.
Bonjour is a good system to discover and connect to services/clients, but only on your local network. I don't think it is a good idea to try it over the internet.

Question about using Bonjour or other methods to secure a connection with a non computer device

So I have a project I am working on, and I would basically like to send a basic yes/no command from the iPhone to a wifi enabled device that is also connectedto the network on the same router. This other device is not a computer, so I am wondering if I can still use Bonjour? In order to use Bonjour, do both of the communicating devices need to have some sort of a script installed and running that is allowing them to communicate? What would be the best way to connect to this device that is connected to the router, and then to send information/commands to it? I could really use some help finding a direction, because once I identify which tools I need to use, I can research them out and get it done (maybe). If anyone could offer any insight/suggestions whatsoever, it would be greatly appreciated!! Thanks in advance
Bonjour is a local service discovery protocol based on multicast DNS. Even if your device supports it you still need some piece of software on the device to listen to your commands.