Communicating between Android phone and PC using TCP in java - java

I'm working on a project where I want my Droid 2 to be able to send and receive data over 3G, to a device connected to an ethernet port (not necessarily a PC, but I'm using this for testing the communication). I believe that eventually the end to end communication will be accomplished over an SSL tunnel, but for right now, I just need to establish basic communication to show that the devices can communicate.
I've written some client/server applications that worked between two computers on the same network, but I'm not sure if the communication in this case will work because of the different networks that the phone and PC are on.
I've been trying to establish if this can be done by pinging some of the PCs at my University and house using a pinging program I got off the market, but I've had no success so far. However, my friend has rooted his phone, and was able to do so.
I suppose this question really has two parts: 1) Is this possible? and 2) Do I need to root my phone to accomplish this, or should I be able to do it without rooting?
Thanks for any responses.

I've never worked with Android phones but I think:
You do not need to have a root access to use sockets;
You said that you want to use 3G to connect to your home PC and I suppose you have some kind of router at home and you need to configure port mapping from your router to your desktop.

I've written some client/server applications that worked between two computers on the same network, but I'm not sure if the communication in this case will work because of the different networks that the phone and PC are on.
One potential problem I can see is that, depending on your 3G provider, there may be a restriction on port numbers and protocols that can be used. For instance, HTTP to any port number may be allowed but unknown packet data (i.e., an unidentifiable protocol) to the same port numbers that work for HTTP, may be dropped.
I've been trying to establish if this can be done by pinging some of the PCs at my University and house using a pinging program I got off the market, but I've had no success so far. However, my friend has rooted his phone, and was able to do so.
Again, another potential problem - ping (ICMP) is not a good test of accessing devices on other networks as most perimeter routers/firewalls will by default refuse ICMP traffic (usually silently) - even my home DSL router/firewall silently blocks ICMP by default. As for needing a rooted phone to be allowed to use a ping app - my phone isn't rooted and I have a ping app from the market which works fine.
I suppose this question really has two parts: 1) Is this possible? and 2) Do I need to root my phone to accomplish this, or should I be able to do it without rooting?
There is no reason I know of that would require a phone to be rooted to use socket-based comms, I'm just wondering if trying to ping across a router/firewall isn't the best test for what you are trying to achieve.
I'd recommend starting with HTTP - I don't know what you use to program for the PC but a simple HTTP 'listener' should be easy enough to put together. Start by listening on port 80 and try from the Android browser - if that works, change the listening port to something like 6789 (remember to open the ports on your home router/firewall). Assuming you've got that working, try using the socket-based stuff you've developed using wi-fi on your LAN, if that works, then try over 3G.

Related

TCP connection between two android devices (even with NAT)

I am doing an android app allowing users to play online.
Currently, I use a TCP server: when two persons are connected, the server takes care of forwarding the packets between the two clients.
I would like to replace my server by a java servlet with google app engine. This new server will just be used to connect the two players.
It would work in that way:
Player A opens a server socket and then post to the server the connection details.
When a player B wants to play against A, he asks to the server the port number of A and he connects directly to A.
The problem is that I am not sure that it will work if player A is behind a NAT. When player A opens a server socket, that opens one port of its 192.168.x.y address, but does it ask to the box a port forwarding? I assume it doesn't...
So two questions:
Is it possible to make a direct connection TCP between two devices even when there is a NAT or a firewall (I don't know how firewalls work on Android...)
If it isn't possible, what is the best solution: Is it possible to make a TCP server to ensure the exchange of the messages with app engine?
Thank you by advance.
game
Creating direct TCP connection between users under different NAT is mostly possible. There are 4 types of NAT. FC, ARC, PRC, Symmetric. If one of player A or B has symmetric NAT then it is impossible to create TCP P2P connection. In this case you will have to use a server in the middle for exchanging data between two players.
For other types of NAT combinations it is very much possible but not guaranteed. The technique that is used to create TCP P2P connection is called TCP hole punching. Read this answer to know in details about this technique.
Also creating TCP P2P connection is not related to any platform.
First, the device itself is probably not going to be the main problem. If they are at home and using WiFi, you will probably have to deal with a cable modem/DSL modem, which typically includes a firewall. Also if they are at work (or a hotel, conference center, etc.), there may be a corporate firewall to deal with.
I believe most home cable/DSL modems support uPnP (Universal Plug and Play), which includes the Internet Gateway Device Protocol (IGD) designed to let devices determine the external IP address and set up port mappings. In general you can look up NAT traversal for ways to handle connections through a home modem/firewall. I will note that corporate firewalls are a different matter and many of these techniques won't work.
So probably I would recommend you be ready for at least the following four scenarios
Direct connection with nothing creating problems. You can test this by having the server do a test connection when the player first contacts the server. If this works, things are simple.
Home NAT device that understands uPnP. If you have a 10.x.x.x, 172.16.x.x-172.31.x.x, or 192.168.x.x number (typical home WiFi), then you can try to set up the NAT traversal and if that works you can send the appropriate information to your server. It probably would be worthwhile for the server to do a test connection just to be sure that things work.
If you have a firewall that you can't get around, then make a note on the server regarding player A, and when B tries to join A's game, look and see if B will accept connections, and if so then arrange for A to connect to B instead.
If none of the above work, then have A and B both connect to the server and have the server relay messages between A and B.
If you don't want to program all those possibilities, then option 4 is the one that is most likely to work, even if it does mean extra traffic going to/from your server. But note that for corporate networks, they may simply have a rule blocking unknown connections, and there may not be much you can do.
Edit: I was able to get a simple TCP server working on Android without anything special regarding Android itself, so removed a comment saying I didn't know about that.

Hosting Java server on my own computer, using "no-ip" for static access

I'm making the android game and my own JAVA server. I want to run that server on my own computer. I don't really need to run it anywhere else, so I really don't need other solution (payed hosts, etc...). I need a static IP access to my computer. Currently im using no-ip.com to do that, but I'm having trouble accessing received data.
On my server Im listening on a specific port using DatagramSocket.receive(), and on the other side Im sending data to :
InetAddress.getByName(HostName.no-ip.biz):port(the port my server is listening to)
I can see that im sending data to the right address ( same address DUC is showing me), but server doesnt catch that data.
If anyone has some experience in using no-ip.com to gain static IP please help.
P.S.
Currently I have internet access via iPhone tethering, so no having real router could be the reason behind this. I have read that i don't need port forwarding on cell-phone because my ISP is doing NAT for me. Any workarounds?
Is it just installing DUC on my computer enough to work with no-ip?
Any easier (free) way to get static IP?
This seems like a port forwarding problem to me.
No-IP resolves the external IP address of your iPhone for you, so your packets go there. The iPhone does not know how to handle them. You would need to forward them from your iPhone to your local PC's IP address, but I do not know if that is possible.
When using your router at home, port forwarding should be no problem at all.

Java program without forward port in router

I made application with java using socket. My computer is the server and my phone(android ) is the client. Only what i trying to do is to forward String from phone to client PC.
Everything works fine when i configure my router and open the port i using. i don't want every time when costumer will install my application will need to open port in the router.
I thought about using remote server that will run my server code. but i didn't find server that can do that . i don't want to make my pc a server for all costumers.
How does all the chats companies do it without open port?
You must look into UPnP. This is what /most/ if not /all/ torrent clients use to allow foreign connections, without forwarding ports. How chat clients do it is a different scenario. They use hacks such as firewall hole-punching using UDP (with an external server) http://www.h-online.com/security/features/How-Skype-Co-get-round-firewalls-747197.html see this link for details on UDP hole punching.
Also see this article http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N for usage of UPnP. But this is in C++, but I think you will understand.
EDIT: http://4thline.org/projects/cling/ I found this. I think it can help you.
You had to open port probably because of your routers firewall :). If you already opened let us say port 9090 then every client app (android phone) will be able to connect to it :). Of course if somebody wants to install server on their own pc they would problably forward some ports and disable some firewalls. If you want server with no special requriements lookup VPS'es. :)

Simple Network chat in Java

just had a question to a simple chat program i wrote in java.
The program uses TCP to transmit simple message strings back and forth from Server/Client. And it works on all the computers on my Network. But when i sent it to a friend of mine to see if it worked from his computer it did not. So my question is: Would i need to add some specific code to allow messages from other comps from outside my network to be recieved? or is it some kind of Firewall that is preventing this?
Probably a firewall or a router.
Firewall: port is not opened.
Router: message are not forwarded to the machine which hosts your application.
Or your application is using the internal IP and not the IP from the internet (which has to be forwarded to your internal ip)
If you don't specify anything special in the code for computers that's not on the internal network, then it won't handle them differently. It won't handle the connections differently by itself. I'd check your(or his) router and open any ports that you use.

How do I send data between two computers over the internet

I have been struggling with this for the entire day now, I hope somebody can help me with this.
My problem is fairly simple: I wish to transfer data (mostly simple commands) from one PC to another over the internet.
I have been able to achieve this using sockets in Java when both computers are connected to my home router. I then connected both computers to the internet using two different mobile phones and attempted to transmit the data again. I used the mobile phones as this provides a direct route to the internet and if I use my router I have to set up port forwarding, at least, that is how I understand it.
I think the problem lies in the method that I set up the client socket. I used:
Socket kkSocket = new Socket(ipAddress, 3333);
where ipAddress is the IP address of the computer running the server. I got the IP address by right-clicking on the connection, status, support. Is that the correct IP address to use or where can I obtain the address of the server? Also, is it possible to get a fixed name for my computer that I can use instead of entering the IP address, as this changes every time I connect to the internet using my mobile phone?
Alternatively, are there better methods to solving my problem such as using http, and if so, where can I find more information about this?
EDIT:
Would it be possible to have the server program running on a server on the internet somewhere. My original server would then be a client that send information to this server. This server would then pass this information to my original client and vice versa. That way, the IP address of my computer won't matter, as I only need to know the address of the server hosted somewhere on the web. Does this seem like a better solution? Where do I begin implementing such a solution?
Thanks!
When you connected to the server that serves StackOverflow, did you type in the IP address? It's 64.34.119.12, if that jogs your memory.
You probably didn't. You probably typed "stackoverflow.com". There's a huge, complex, clever, and in some ways, poorly implemented system called DNS that translates sensible and human-readable names into IP addresses.
One problem with DNS, though, is you need a "static IP", which is exactly what it sounds like: an IP address that doesn't change, which is exactly what you don't have.
So, what can you do?
You can buy a static IP account from your ISP (pretty expensive)
You can use some proxy out in the Internet (a machine that does have a static IP and is willing to bounce your packets back and forth -- I'm not aware of any service that does this for you; you could write one and put it up on Amazon Web Services or Google App Engine, both of which would be free at your level of usage, but they'd be slow, since every packet trying to cross your living room would have have to go via some data-center in Virginia).
You can keep doing what you're doing, looking in the net-configuration of your machine.
You could speed (3) up a little by having your server program look up its own IP address and print it out where you could see it and type it into the server by hand.
You can use DynDNS, as Sergey mentioned (this is the "right" solution, in that it's very general, it just might be a little complicated to set up)
You can use multi-casting.
Multi-casting is an interesting solution, and it may work for you. The idea is, when your server starts up, it announces to the net, "Here I am, I'm providing X server, here's my IP address, talk to me." The problem is, a multi-cast won't leave your living room. Obviously, if every multi-cast were spread to every computer on the Internet, the whole thing would collapse, so your router will ignore, and not route, multi-cast packets. That may or may not be a deal-breaker for you. EDIT Re-reading your question, I see it is a deal-breaker for you. I'd go with #5, but be aware there may be routing issues (address translations that prevent a server from knowing the address that other computers can find it at) or fire-wall issues (that is, your ISP may prevent your server from receiving incoming packets even if the address is correct).
using a direct socket connection with a port like 3333 is usually complicated because different network configurations.
firewalls will make a pleasure preventing the connection, or killing it from time to time.
maintaining a 2-way connection can be a nighmare. the SIP protocol is struggling with this kind of problems.
For a simple application, i suggest you look into the comet technology, where your clients can establish an http connection with a shared server. The server can then bridge commands between them.
html5 will also bring the websocket protocol to the table.
I got the IP address by right-clicking
on the connection, status, support.
Not sure about the "support" part, and I'm not on a Windows machine right now, but I think that the most easy and reliable way to figure out the IP address on Windows is to run "ipconfig" from the command line (Win+R, type "cmd", then "ipconfig" in the opened window). This, of course, should be done on the server side.
However, the problem is that depending on the ISP your IP address may be not within the Internet, but within a local ISP network (so-called NAT). In this case, you'll need to use some sort of black magic called TCP hole punching, which is very complicated and not guaranteed to work. You can figure out if your address is local or not by looking at it. For IPv4 local addresses are almost always like 10.x.x.x or 172.16-31.x.x, or 192.168.x.x. Don't know about IPv6.
You can also check your IP by visiting one of the special sites like www.whatismyip.com. If the address they tell you is different from the one you see by running "ipconfig" or looking at the connection properties, then you're almost certainly behind a NAT (or your ISP is using a transparent proxy, but that's rare).
If you are directly connected to Internet (no local addresses and NAT), then you should also check if you have any firewall software and either to configure it to allow connections to the port you use, or make sure it's in "ask the user" (and not "silently reject") mode, or just disable it completely (this may put your computer at risk, especially if there is no anti-virus software or the system isn't up-to-date).
Also, is it possible to get a fixed
name for my computer that I can use
instead of entering the IP address, as
this changes every time I connect to
the internet using my mobile phone?
Yes, it's possible. There is the thing called DynDNS, and there are DynDNS providers like DynDNS.com, where you can get a third-level domain name for free (like mycoolpc.dyndns.org). You'll have to install and configure some DynDNS client on your PC that will tell the DynDNS server its new IP each time each changed. I don't know anything about particular clients to use because I'm using the one built-in in my home router.
No need to write networking code for this, unless it really floats your boat. Take a look at SCP. http://amath.colorado.edu/computing/software/man/scp.html. There is a windows implementation where you can download putty (windows ssh client), and it is on most linux distributions. Alternatively, you could set up an FTP or SSH server on one or both of the machines.
"a fixed name for my computer that I can use instead of entering the IP address" would be a domain name, these are purchasable online for a few bucks.

Categories