I wrote a simple socket program on android, the server side sets up a server socket waiting for incoming connections, the client side just connect to the server by establish a connection with the server's ip address. I'm using Sprint's 3G Nexus S as the server, and ATT'S 4G Samsung galaxy S3 as client. When my client tries to connect, it throws out the exception"No route to destination". But then I switched to my personal WIFI network, it worked perfectly. Can someone help me with this issue? or is there any special requirement in android to use cellular network to set up socket connection? thank you!
My client side is:
socket = new Socket(serveripaddress, 8008);
out = new PrintWriter(socket.getOutputStream());
My server side is:
ss = new ServerSocket(8008);
update: I just used 2 Sprint cellphones to test my code and it works fine!! This is just a special case in which i have to use phone as server, it is not recommended in general
Carrier firewalls typically don't allow incoming connections to mobile devices. Doing so would leave the mobile devices open to security risks and abuse of their data limits. It can also depend on the APN used for the connection.
Related
I am trying to write a client-server app that connects a PC as a client to my PC as a server.
When I enter 127.0.0.1 as server IP in client side, in my PC, it works properly so it's not a coding problem. Also when I enter my IP (got it from nslookup command in kali) and connect to internet, client connects to server properly.
But when I open my client app in other PC and server app in my PC, the client side a "Connection Time out" Exception will be thrown.
I have tried turning off the firewall in the client side (Windows 10) but not from the server side.
Here are my codes:
Server:
ServerSocket server = new ServerSocket(SERVER_PORT);
Socket client = server.accept();
//Some codes
Client:
Socket server = new Socket(SERVER_IP, SERVER_PORT);
For better answer we need more informations from you. But from this what you post, this is best answer i can make. You didn't explain what is your goal, do you want to make connection inside local network or you would like to make connection to 'outside world', over internet.
If you like to make two PCs communicate inside local network, than you must make sure they are on same network, or more accurate, that they are connected to same router. You have to set your server IP address to IP address on your local network, that would probably be something like 192.168.xxx.xxx
If you want to communicate over internet, i suggest test in local network first, as described before. If it works on local network, then you have to deal with firewalls, router setting,etc to make it work on the internet too.
Take look at this too
Use Socket-communication over different networks
I am trying to build a system in java, where one computer acts as the server and the other one as the client and connect them together (i.e send data b/w them) on the public network. Here is my pseudo code -
Server.java
ServerSocket server = new ServerSocket(55955);
Socket socket = server.accept();
Client.java
Socket socket = new Socket("<ip address of server>", 55955);
//code to send and receive response from server
The code works fine if I have my server and client running on my computer.But if i run the server on a different machine (a friend's pc on the internet) and try to use that ip on the client running my machine, its not working. I did a lot of googling and did not find a appropriate solution other than opening up the firewall or ports on the machine.
Is there any other option to connect b/w the computers on the internet without any forwarding or opening the ports ?
There are a few peer to peer systems (eg: skype)etc that does the same thing I am trying to achieve (and in my best of my knowledge its without opening any ports on the host machine). Please correct me if I am wrong.
Thank you.
Thank you. I am adding the solution which I was finally able to work with.
This can be achieved using NAT Hole punching.
Found these awesome articles which explains the approach.
Peer-to-Peer Communication Across Network Address Translators
How Skype gets around firewalls
I'm trying to connect my Android app to desktop server. The problem is when I try to create LAN via portable Wi-Fi hotspot on my smartphone everything works fine, but when I connect PC and phone to the router I have TimeOutException creating a socket in Android app. On the other hand, when I connect desktop client to desktop server via router it works fine too, so the problem occures only when I try to connect mobile client to desktop server via router.
Client code (Java):
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000); // TimeOutException
Server code (C++/Qt):
QTcpServer m_tcp_server;
// ...
m_tcp_server->listen(QHostAddress::Any, m_port);
ip address and port are right (100%)
UPD:
Thank you, guys.
It was just a router problem. I tested in on the another one and there is no problem
try check which ip you have assigned on android pohone and if you have route to the destination server ip.
Is possilbe you get different ip on wifi and different ip by cable.
Also you can try an android net tool to try connect to the server.
I had same problem, In my case i created server socket with wrong ip address.
Hotspot network band is 192.168.43.1~255
You need to create server socket with ip address that hotspot dhcp made like 192.168.43.11
I had wrongfully made server socket with ip 192.168.0.22, this is wrong.
And your android app should open client socket with it.
Right now, I am writing a server/client program to allow a user to control different parts of their computer from an android phone on the fly. The server runs on the computer and the client runs from the android phone. I am using socket programming to do this, WinSocket on the computer and Android/Java sockets on the phone.
What Works:
The server on the computer end works.
When on the same wifi network, the client on the phone can send and
receive commands to/from the server.
The server is accessible from outside the local wireless network it is connected to. I
tested this by using this open port checking tool to send a request
to the server. The server can successfully receive the request, and
the port checking website reports that the port I am using is
properly forwarded.
When using an app on my android phone that tests open ports, my
server gets the connection request over 3G/4G.
Now for my problem. When I try to connect to the server running on the computer from the client on the phone, when the phone is connected to the internet through 3G or 4G, the socket does nothing and throws a socket timeout exception, and the server does not receive any connection request from the phone. I don't understand this because, like the 4th thing above, the port checking app I used can get through to my server program, but the code I'll post below doesn't do anything when running on 3G/4G.
String hostName = "SERVER_NETWORK_EXTERNAL_IP_HERE";
int port = 58587;
SocketAddress address = new java.net.InetSocketAddress(hostName, port);
sock = new Socket();
sock.connect(address, 5000);
The network activity indicator in the status bar at the top of the screen does not report outbound network activity when running this code, however if I am connected to wifi, everything works correctly. Anyone got any ideas?
I am trying to set up a client/server model with android clients and a Java server.
The connection is over TCP.
I have all of my code working when the android device is on WIFI, however whenever I connect to a wireless network (eg 3g, 4g) the sockets refuse to connect.
I've been reading about NAT traversal but i'm not sure where to begin when implementing the necessary steps to allow the connection.
NOTE: my server has a static IP address
My guess is you have a network related problem. Assuming your server is behind a router, your server socket has to bind into its LAN IP (not directly binding the public internet IP). Then you need to setup port forwarding, firewall rules etc to ensure everyone on the internet can see your server (checking with online port open checker / telnet from another network is a good test)