Cannot connect to my HTTP server running on my Android phone - java

I'm trying to run a small, custom HTTP server embedded in my Android application (to aid in debugging the app). However, I cannot reliably to connect to the HTTP server from my desktop web browser when the server is running on my Android 2.3.4 phone.
I know the HTTP server code works (at least the basics), because one time the connection went through fine. And the code works reliably when run on my desktop (my app is a libgdx-based, pure Java app that can be built for Windows or for Android).
I have the Android permissions correct, because I was getting the PermissionDenied fault (when setting up the socket listener) thrown before I added the android:name="android.permission.INTERNET" permission. The server-side code runs in its own thread and boils down to:
ServerSocket ss = new ServerSocket(serverPort);
Socket clientSocket = ss.accept();
I'm using port 8080. (I tried port 80 but hit the must-be-root-below-port-1024 "feature" of Linux).
The LogCat output looks fine. My app just prints its "listening on port 8080" message and no exceptions seem to be thrown.
Network connectivity seems fine (browsing the internet from the phone is working, and I can ping the phone from desktop). I'm using WiFi, not 3G.

I think I found it. I was missing a call to:
ss.setReuseAddress(true);
after creating the server socket. This means each restart of the app (pause/resume, too) was (silently?) failing to create server socket because the socket was marked as 'in use' for a bit. This explains why it was so unreliable (only the very first run, or running after a long enough wait would work ...)
I found it by staring at the code linked to from this blog entry.

Related

Which things should be taken care of in Java Socket Application over DDNS?

I have developed a Chat Application for two person, one being server another will be client, using Java Socket Programming. Every thing was fine till this morning. It was working over localhost, local networks as well as On my DDNS ( My Router is configured to forward any traffic on it's port 8888 and 3434 to same port on my IP, which is again Reserved in my router ). But now it works strangely. I ran a server on my laptop at port 8888 I tried to connect the client through my DDNS on port 8888, Client shows it is connected, but Server shows it is disconnected. Client even sends message successfully which does not appear in server.
I want to know what causes such strange behaviour of my application, is this my firewall, because I have used my DDNS a lot in order to debug some issue.
I also want to know what precautions should one take in order to use DDNS in Java Socket.
Additional Informations:
My DDNS in on Dynu
OS: Windows 7 32 bit
Quick Heal Antivirus and firewall (Outdated)
I am adding some pictures:

Client/Server java - connection refused

I'm trying to write basic client server. The code works fine when I'm using "localhost" and random port. The problems start when I run the server and the client from different hosts, and then I get "Connection Refused".
I searched for this problem and none of the solutions helped (Disable the firewall, run the server before the client, connection to the internet etc)
Again, the code works fine, my firend ran it and all works fine. The problem is probably with my computer. I have a router.
Thank you

Disconnect from EventSource not working

I have a Play 2 (2.3.7) app that uses EventSource connections. Everything works fine except when I try to close the connection from the client. To do that I am using the ES.close method, but even after I disconnect Play only detects that the connection was closed (and processes the onDisconnect callback) when I send a ping to the client. Closing the connection from server side works as expected.
I tried this in Chrome/Firefox (using native implementation) and using Yaffle ES polyfill and the result is the same. Both client and server are running in a linux machine.
EDIT: After closing the connection in the browser console and inspection the network tab in dev tools it seems the connection is indeed closed by the browser. So this may be a problem of Play?
So what may be the problem here?

How to "deploy" a server so that it can be reached over the internet?

I recently developed an Android application with which the Android device can communicate with another Android device running the application.
The communication works over sockets, therefore I developed a server which i run on my computer.
Here is my problem:
The communication between the devices over the Server running on my PC works fine, as long as all devices as well as the PC are in the same LAN (connected over the same Router for example).
Now I want to get the server online, so that the Android devices can connect to the "online" server and communicate with each other over the server from anywhere.
I simply have no idea of how to get the server online and running. How can I do that?
The main issue is, that I know about Client/Server communication locally, but have no experience in the "online" sector.
It is more a network problem than a programming one. Your server open a socket and therefore is available to anyone able to reach that socket.
You have to do a redirection on your router. The problem is that your machine doesn't have a public IP, only your router has one. So when your router receive a packet on port 21 for example, it doesn't know what to do with it. You have to configure it to say "the port 21 has to redirected to the local IP XXX"
Also the public IP of your modem/router can change, depending on your ISP. If your have a fixed IP, it won't change, otherwise you will have to install a software like dyndns to have a domain name associated with your IP.

Android socket chat connection errors

Hy! I'm adapting a chat using sockets and threads from java client to android client. The server remains the same. I've wrote the internet and ACCESS_NETWORK_STATE permissions in the manifest. The problem is that when I try to connect to server it throws some errors.
The try{ socket = new Socket("localhost", 5000);} line throws:
link to screenshot
What could be the problem ? Would you want to put the whole code here ?
I'm sure that you're trying to contact your local machine and not the device itself. The phone will address itself using localhost or 127.0.0.1. So when your device is not a server and is not listening for that port the connection will fail.
Try to use 10.0.2.2. This should target your machine you're developing on. (source)
Have you tried using the real network ip of your machine (sth. like 192.168.0.1) instead of "localhost"? The error itself looks to me like there's nobody listening on port 5000. So I guess the Droid is trying to connect to itself instead to the server.

Categories

Resources