I want to create a super basic Android App that connects to a python server running on my PC but the python server never gets the connection
my java code:
public class WriteToSocket {
Socket sock;
public void Test() {
try {
this.sock = new Socket("PCName", 9871);
} catch (UnknownHostException e) {
System.out.println("Unknown host: PCName");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
public void Test1(){
try {
this.sock.close();
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
and
public void onClick(View v) {
WriteToSocket a = new WriteToSocket();
a.Test();
}
and my python server is
import socket
sock = socket.socket()
name = "PCName"
port = 9871
sock.bind((name,port))
sock.listen(1)
s,a = sock.accept()
I expected after the button click for the python server to accept the connection (I also tried changing "PCName" to "127.0.0.1")
I've looked around but nothing helped me so far :S
Bind your server socket to one of the IP addresses of your PC which is accessible from your android, and not to 127.0.0.1. Or alternatively bind it to all available interfaces (0.0.0.0).
Then connect from your android to that IP.
E.g. if your PC has IP address 1.2.3.4 then use this IP in both applications.
Use netstat to see if the port is really open on your PC.
Check to see if your android application has the permission to use the internet (specified in the manifest: "USES_INTERNET" or something like that).
Also your python script discards the connection as soon as it is made.
In python change bind address to 0.0.0.0. It will bind for all IPs attached to your machine. Then in android app change to correct IP of your computer.
IP 127.0.0.1 is a loopback and you can't connect to it from outside of the system.
The android phone doesn't know what PCName is, change "PCName" in the python code back to '127.0.0.1', then in the android project put in the local IP address of the server.
This of course assuming that both the phone and the server are on the same local network.
Related
I have a server running in a machine (lets call it MACHINE1). In this file, I have created the start_server = websockets.serve(recv, "serverIP", port). The server is correctly running.
On the other side, I have one android application running working as a client connected to the server (note that both devices are running in the same network)
try {
uri = new URI("ws://" + serverIP + ":port");
} catch (URISyntaxException e) {
e.printStackTrace();
}
mWebSocketClient = new WebSocketClient(uri) {
// my code
}
As I mentioned below, currently the solution is setting serverIP to the IP of MACHINE1 (192.168...), and therefore, if I change the machine to another IP the connection will fail. Is there any way to create a general solution? I tried using localhost but I did not manage to connect to MACHINE1
hi have at home a rasperry pi running a server java app, connected to de router with the dynamic DNS configured and the in/out communication ports openned.
When i run the android apication client througt 4g everithing is working sucessfull. But when i run the same app connected to the wifi on my local net, where the server are running, the server application looks like death.
Any ideas?
Thanks.
router config
I think the problem is related to your DNS. If you are connected using your wifi, you have to return a local IP-Address. Do you have any chance of configuring your router to return the raspberries local IP-Address for wifi-clients?
Take a look here: link
A simple solution, even if it is not very elegant, is adding the following conditions to your code:
If there is an error connecting to the Dyn DNS, try to connect to the local IP address. (In case you are in the Wifi LAN)
If the local IP address fails, try again your Dyn DNS (in case the user is a real user with real communication problems)
(repeat until the connection is successful)
You can also identify your testing devices (using Settings.Secure.ANDROID_ID, or the IMEI) and use the local IP only for them. Another option is making the URL configurable (with a hidden option for example).
Because of my app can run local, only needs to connect to server for updates. I have block the connection on the server app if the ip from client and server are equals.
At the moment is the best solution to keep the server app running properly.
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new
InputStreamReader(whatismyip.openStream()));
String ip = in.readLine();
try {
SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) sslFactory.createServerSocket(PORT);
int idSession = 0;
while (true) {
SSLSocket socket = (SSLSocket)ss.accept();
if(socket.getInetAddress().getHostAddress().equals(ip)){
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
((ServidorThread) new ServidorThread(socket, idSession)).start();
idSession++;
}
} catch (IOException ex) {
ex.printStackTrace(System.out);
Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
I'm programming a game in Android that uses AI, which requires big CPU power, that a normal Android device just doesn't have. So I decided to write a server in Java using sockets that will calculate everything and return a value to the client (the android device).
Now, I'm used to program for PC, but not for phones. In mobile, the IP of the device can change back and forth due to data roaming and WIFI.
My question is, how do you handle a changing IP? How do you tell a new connection is the same device? Or maybe the Android device does all of that automatically?
I'm new to stackoverflow, I hope I didn't ask too many questions. :)
Thank you very much for your answers!
You don't need to handle ip changing at all. A client(an android device) must know server host/ip and reconnect if it was disconnected from network, nothing more.
private static class ConnectionTask implements Runnable {
private boolean connected;
#Override public void run() {
try {
InetAddress serverAddress = InetAddress.getByName("host");
Socket socket = new Socket(serverAddress, 9999);
connected = true;
while (connected) {
// sending or writing data
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
//here you lost the connection due to some reason
//you need to notify user about the problem and wait for connection
}
}
}
To receive event about network state you need to register receiver:
context.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (isNetworkAvailable()) {
unregisterReceiver(this);
tryToConnect();
}
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Okay guys I figured out a way, thank to Fox in socks' answer.
Each time a user connects to the server socket, you take his UUID (or an hashed version of it, if you want more security :P ).
Then, when that user disconnects for some reason and tries to connect to the server socket again, he'll send the same UUID. That way, you can tell both of the connections are the same, and continue with the processing.
For more information about UUID, look here:
Is there a unique Android device ID?
Thank you all! :)
Now how do I mark this question as a closed one? :P
I'm getting some strange results from the javax.comm library. I'm running this constructor:
SerialPort port;
public Scale(String porttotry) {
CommPortIdentifier cpi = null;
try {
cpi = CommPortIdentifier.getPortIdentifier(porttotry);
//Open Port and establish stream
log.trace("Opening CPI: {}", cpi.getCurrentOwner());
port = (SerialPort) cpi.open("My Application", 2000);
log.trace("CPI opened");
//... configuration stuff
} catch (PortInUseException ex) {
log.error("{}: Port in use by {}", porttotry, cpi.getCurrentOwner());
} catch (NoSuchPortException ex) {
log.error("No such port as {}", porttotry);
}
}
What's really strange is the fact that CommPortIdentifier.open(String, int) appears to be throwing a PortInUseException when the port is not in use. The log output says
TRACE [10:19:03.147] Scale:72 Opening CPI: Port currently not owned
ERROR [10:19:05.149] Scale:98 COM4: Port in use by Port currently not owned
The first log line means to me that open() should succeed, but it doesn't. I've connected to this device before and gotten data from it. This is a strange new error. Does anybody have any idea what could cause this? My gut tells me that this is some tricky thing with Windows having possession issues. I'm open to any ideas and I'll provide more information if you need it.
I can't speak for exactly why this strange error occurred, but I did find something of a solution. It seems that I was correct in assuming it was Windows being possessive. When I shut down, unplugged my device, powered on with the device still unplugged, then connected the device, the PortInUseException disappeared.
First of all my programming language is Java. I created a simple chat program using sockets. It works pretty good.
I tried it on my computer (localhost) between two terminal instances.
I want to try it out on my laptop, or on another computer. For this I need the client's internal IP address.
How to figure out client's internal IP address using Java?
I specially want to get it out with Java, not using CMD, or something like this. I mean - that's not just a constant string.
In Java, there is a class InetAddress that represents IP Adress (and its corresponding host name, in some cases).
For example, let's get my IP address and my host name:
import java.net.InetAddress;
public class Main {
public static void main(String[] args) {
try {
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); // host name and IP address
System.out.println(i.getHostName()); // name
System.out.println(i.getHostAddress()); // IP address only
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output (in my case):
LLEITE/192.168.1.100
LLEITE
192.168.1.100
Use the below code:
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());`