Advertisement

How is a real-time server port "find" known to client code

Started by March 02, 2017 11:39 AM
51 comments, last by hplus0603 7 years, 8 months ago
"running 2 Java programs at the same time, in the same IDE" - Are you running this Android program on your phone, or on an emulator? Each has different networking environments, and this information is critical.

I'm running it on phone,

I raised that because hplus0603 suggested i should try running the client and server on the same machine. Actually both the Java and Android code run in the same IDE but since I always connect to my phone, ultimately the Android client code runs on the phone. So i took his suggestion to mean i should run both as Java applications rather just Java and Android

"I don't know a code line that would let me do this" - Step 1, construct an unconnected socket, Step 2, connect while setting the timeout. You'll need to create a SocketAddress from your hostname and destination port number to do that.

Got it now, overlapping/ambiguous interpretation of constructor misled my thoughts... when you initially mentioned "constructor" i confused it to mean the current Android class construct (not realising you meant the constructor for creating the socket).

so I used


sock = new Socket(serverIP, 52059); 

Now changed to


sock = new Socket();
sock.connect(new InetSocketAddress(serverIP, 52059), 2000);                        	

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

You can also run a Java program without the debugger, in a Terminal window, using the "java" command.

This works as long as you use Java SE, and don't use any Android classes. Which, for a super-simple client or server program, ought to be simple.

enum Bool { True, False, FileNotFound };
Advertisement

If you're on the same subnet (e.g. local WiFi), the server is running and listening, the port and IP address is correct on the client, and you're still getting timeouts, then that suggests your computer has a firewall that is either blocking the listening socket or blocking the incoming connection.

You can test that theory by running a server that is known to work - e.g. the Apache web server - verifying that you can connect from that same machine (i.e. from your browser), and then verify whether you can connect to it from your phone browser. Ideally you'd see if you can connect from another, non-phone device, too.

If you're on the same subnet (e.g. local WiFi), the server is running and listening, the port and IP address is correct on the client, and you're still getting timeouts, then that suggests your computer has a firewall that is either blocking the listening socket or blocking the incoming connection.

The first part seems similar to what Putty verified. Anyway still tested all again, server code is connectable from browser, but client side remains the problem ... I need to check my handset's capability (my phone actually actuating (or not) a signal to allow server connection. I think it looks dead from that perspective. Even though the handset is definitely wifi connected, i think its capability to actuate a signal to allow server connection is not guaranteed, or it may be that the handset is ok but the client code is incomplete some where )

At this stage, I'm at the point of giving up on this part of my project for now, since i've been stuck on this part for several days now. There are other parts of the project I can work on in the mean time

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

server code is connectable from browser

Which browser? Your computer's? The phone's? If you're more specific then it makes troubleshooting easier.

Even though the handset is definitely wifi connected, i think its capability to actuate a signal to allow server connection is not guaranteed

Can it access web pages? If so, it can connect to servers. Did you try running a web server on your computer like I suggested?

Unfortunately I'm not going to run your code, but I suggest you clean it up and fix the previous errors before asking people to copy it, otherwise you're potentially compounding further errors on top of the existing ones.

Which browser? Your computer's? The phone's? If you're more specific then it makes troubleshooting easier.

The computer's

Can it access web pages? If so, it can connect to servers. Did you try running a web server on your computer like I suggested?

Yes it can access web pages. Yeah, i did run web server on my PC to test.

Unfortunately I'm not going to run your code

No problems, many thanks for great help so far anyway (though didn't mean you specifically)

but I suggest you clean it up and fix the previous errors before asking people to copy it, otherwise you're potentially compounding further errors on top of the existing ones.

The code I posted in Original Post has got no errors. Only its got '0' as port number, but thats got to be changed anyways. The issue with it is the one that still remains now. (only at that time i had thought the problem was because i didn't choose the right port number, so i started experimenting with randomly chosen ports, hence using '0').

Anyway I just need to eliminate the phone as the problem. I could have borrowed my wife's phone but she's got an iPhone. I guess patience is what I need... I will have to wait for one of my friends with Android handset and then test it.

Yeah, will wait... I will edit my post immediate above to that effect

Many thanks everyone who has tried to help :)

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Advertisement

To extend a bit on hplus0603, 192.168.xxx.xxx is a private subnet, see https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces

These subnets exists for use on your private LAN, typically the network between the ISP modem and your computer(s).

Private subnets are not routable at all, it's impossible to connect to it unless you're already on it.

What is routable is the public address of your ISP connection, and that ends at your ISP modem, at its firewall. In the modem you need to forward the specific server port to your server machine at the LAN (again at a specific port). It is not needed, but probably useful, to keep both port numbers the same.

Your server machine likely also has a firewall, which also needs to let that specific port through (inbound traffic, tcp) from the lan / modem.

For testing your server, connect to localhost (127.0.0.1), the loopback device. It's like a private LAN network, but it exists only inside your computer. Its purpose is to connect two programs both running at your computer using a network. (Technically however, the kernel often uses shortcuts to speed up communication, but that has no influence on the idea.) Your server machine likely also has a firewall.

A Python program of 5 lines connects as client (standard example for networking with python). Start with this, so you can debug the connection at the local machine with only the server, python client, and the firewall to handle (no idea how paranoid your firewall is for localhost, my linux-firewall is pretty much not allowing anything.)

You may even want to start with a python server as well (another 5 lines or so) instead of your own server, to eliminate the programs as source of problems, so any problem is in the firewall.

Firewalls often has some kind of log for failed attempts, that may help.

For testing your server, connect to localhost (127.0.0.1)

I wanted to add to this that 127.0.0.1 corresponds to whatever computer the code is running on. Therefore, an Android app trying to connect to 127.0.0.1 won't connect to the development PC, even if it is connected via USB. Even if Android code is running on the PC in an emulator, it won't connect to the PC via 127.0.0.1 because the virtual computer in this case is the emulator. The address for reaching the PC hosting the emulator is 10.0.2.2.

But I agree with the general premise; the first step is to use known working software at both ends of the connection to test connectivity.

Many thanks guys, I need to give a bit of update.

Rather than being stuck on the same issue for so long, i thought of working on other parts of the project but my mind kept going back to this same issue in a niggling way (should have known better - I've always been like that... I can't leave issues I get stuck on)

Though 127.0.0.1 didn't work, I was still inspired by most recent ideas suggested by Albert and Kylotan, ... I figured it had to do with the Internal IP and I went on googling spree yet again particularly looking for solutions to similar problems. And saw a pattern that port 80 was worth trying with the public IP

+1 Kylotan suggested this in an earlier post,

Another approach is to change your client to contact a known server (e.g. gamedev.net, port 80), to see whether it is capable of making outside connections.

So I used the public IP with port 80. Now the client code (which was previously stuck at sock = new Socket( "192.168.1.2", 52059) ran fully with no problem at all using sock = new Socket( PublicIP, 80); and I no longer have connectException ETIMEDOUT within try-catch in the client-code

But the problem is the server code never listened to this port, so there was still no server connection

Previously Putty connected with server code using


InetAddress IP = InetAddress.getLocalHost();
ServerSocket servsock = new ServerSocket(52059, 1 , IP);

But now trying to connect with Putty ServerSocket servsock = new ServerSocket( 80 ); Putty gave a fatal error server unexpectedly closed network connection.

I googled this error and tried some solutions suggested which predictably didn't work... 'predictably' because most of the solutions were to solve problems of Putty time-outs but in my case the server code was selectively not listening to port 80.

So the questions are:

1. why does the server code fail to listen to port 80?

2. Is there a way of putting the public IP as a parameter to ServerSocket( port )? When I used InetAddress publicIP = InetAddress.getByName( "XX.XX.XX.XX" ); , I got a bind fatal exception at ServerSocket servsock = new ServerSocket( 80, 1 , publicIP );Yet the documentation indicates literal IP could be used as a parameter in getByName();

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Which box on your network publishes the public IP? My guess is your Wifi/router or cable-or-DSL modem.

When you connect to public-IP port 80, you are connecting to whatever is listening on that public IP address on port 80, which often is the configuration/management interface for your router or modem. (I have some issues with these defaults, but that's what the world does.)

If you open up a web browser, and type in http://public-ip:80/ what page does it open?

Anyway, if you can connect to the public IP, then you have likely diagnosed that the computer/PC is not on a network that is routable from your phone/Android. This may be because of guest network isolation, it may be because of WiFi failing to bridge, it may be because of Android emulation emulating a data link over WiFi, or one of a number of other reasons.

However, the good news is that your public IP likely has a port forwarding function. So, set up port forwarding from port 52059 on your router, to port 52059 on your PC. Then try to connect to (public-IP, 52059) from the Android. Chances are, this will reach the router, and the router will port forward to the PC.

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement