No Route to Host - Android Client TCP connection - java

I am writing a code where the android phone is the client trying to connect to the server on my pc USING WIFI. I am opening the sockets as follows:
try {
servsock = new ServerSocket(13299);
System.out.println("Listening :13299");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket sock = servsock.accept();
and on client side:
Socket sock = new Socket("192.168.0.108", 13299);
Log.i("sending","sending");
However I am receiving on the logcat: "No route to host" I have searched and inserted permission to use internet in the manifest.xml and did a ping from my phone with ip 192.168.0.107 to the pc server with ip 192.168.0.108.
What Am I missing? Why isn't the tcp socket connection established ? the server is written in netbeans. Does it have to do with the ports?
EDIT: I think the problem is in the IP addresses. I set the address of the server (private address) according to the output of "ipconfig" in cmd window.

I had the same issue, i changed the internet access point and the app worked. My app was using a local IP to access the server.

It must be some sort of blockage that keeps your connection to the server out of the scope for your client. Try applying different ports, and see what happens then.

Related

Why is my Android socketserver triggering a socket error 10061 in my client

I'n using Kotlin coroutines to setup a Java serversocket in Android studio 4.0 Beta 5. I'm running in the emulator on Windows 10. When my very reliable c language socket client attempts to connect using 127.0.0.1 as the IP it receives error 10061. The same client program has worked well for many years with a Java Swing socketserver.
Google give the following explanation for error 10061:
10061 is a Connection Refused error sent to you by the server. You could not make a
connection because the target machine actively refused it. The most common cause is a
misconfigured server, full server, or incorrect Port specified by the client.
Here's my code snippet
int myPort = 8080;
String localIP = InetAddress.getLocalHost().getHostAddress();
ServerSocket srv = new ServerSocket();
String hostname = InetAddress.getLocalHost().getHostName();
srv.bind(new InetSocketAddress(hostname,myPort));
srv.setSoTimeout(socketAcceptTimeOut); //This sets the timeout on the accept
Socket cli = srv.accept();
I used the server bind based on another stackoverflow answer but it did't help when I removed it. In any case I believe the serversocket is listening at 127.0.0.1. I'm using port 8080 but I've tried a few others.
On the Android side the srv.accept is just timing out.
What am I missing?
Thanks

Cannot open port 1433 for Azure SQL database in Mac

I'm trying to access a Azure-hosted SQL database on my java application. I checked the port 1433 using nmap and it shows that it's closed:
Starting Nmap 7.12 ( https://nmap.org ) at 2016-09-02 09:44 PHT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00014s latency).
Other addresses for localhost (not scanned): ::1
PORT STATE SERVICE
1433/tcp closed ms-sql-s
I have edited the /etc/pf.conf and restarted my mac but the port is still closed. Here is my pf.conf:
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
pass in proto tcp from any to any port 1433
Also, my firewall is set to off.
My java app is throwing this error:
Error starting database: The TCP/IP connection to the host flowengine7.database.windows.net, port 1433 has failed. Error: "Connection timed out: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Java code:
String connectionString =
"jdbc:sqlserver://dbName.database.windows.net:1433;database=dbName;user=user##user;password=passwordhere;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionString);
println 'connected';
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) try {
connection.close();
} catch (Exception e) {
}
}
You're overlooking the Azure SQL server-level Firewall:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-configure-firewall-settings/
You need to whilelist your Mac's Public IP address in there to be able to connect from local. By default only Azure services can reach 1433/TCP on your Azure SQL instance (permitted by Allow access to Azure Services setting in the Azure SQL Firewall).
If you're getting a new Public IP address every time you reboot your DSL/Cable/Fiber modem you'll need to define a whole range of addresses not just the one (i.e. 174.73.0.0 - 174.73.255.255 vs. listing 174.73.16.180). Hopefully you'll always grab an IP address in the same range.

Java ftp commons.net network coding

I'm currently trying to do some network coding for an android (java) application and I'm facing some problems. I use the Apache library commons.net in order to establish an ftp connection to a server I'm hosting for file transfer to the android unit. this is my code:
public class Server {
public static void main(String[] args)
{
String username = "Username";
String password = "Password";
String host = "AddressString";
FTPSClient ftps;
ftps = new FTPSClient();
System.out.println("trying to connect...");
try{
System.out.println("trying to connect...");
ftps.connect(host, 21);
System.out.println("Connected");
System.out.println("logging in...");
ftps.login(username, password);
System.out.println("logged in!");
ftps.enterLocalPassiveMode();
catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftps.isConnected()) {
System.out.print("LOggin out");
ftps.logout();
ftps.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println("Terminated");
}
}
The program never gets passed the line "ftps.connect(host, 21);", with the error "Connection closed without indication", I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?
Note: I am not trying to connect through an Android device, I'm currently using eclipse for testing.
"I do belive I have configured my server correctly since I can connect to it via "Putty" from another network etc. What am I missing here?"
The "putty" utility talking to an SSH server on port 22 not to an FTP server on port 21. You can say that basic network connectivity / routing appears to be working, but that's not necessarily enough.
Possible problems you may be having include:
the FTP port being blocked by firewalling on the client, the server or somewhere in between, or
the FTP server may be configured incorrectly.
I noticed that the client is trying to use FTP/S but it it using the default port for FTP (21) not FTP/S (991). This is not necessarily wrong (see https://serverfault.com/questions/10807/what-firewall-ports-do-i-need-to-open-when-using-ftps for details) but maybe you should check that your server is configured to support explicit FTP/S.
I would advise:
look at the server and client side logs for clues
temporarily turn on "debug level" logging (client & server side)
see if you can establish a raw TCP connection on port 21; e.g. using telnet <server-host> 21
try changing the client to use FTP rather than FTP/S
if all else fails, try using a packet sniffer to capture the network traffic.
UPDATE
... it seems i confused sftp with ftps ...
Yes, they are very different. SFTP is in effect FTP tunneled over an SSH connection. It requires a client-side library that understands SSH.
So if you are trying to use FTP / FTPS client-side libraries and settings to talk an SSH (SFTP) service, you are probably failing because port 21 is blocked (which it should be if there is no FTP service) or because no FTP service is running (which should be the default for a typical out-of-the-box Linux server).

Java Sockets with Openshift

Through the use of many articles and Stack Overflow questions, I was finally able to successfully bind a Java Socket to a port (port 8000) on an Openshift website.
I know the binding was successful because no exceptions were thrown, and I was able to poll the Socket for its port number. I can also successfully connect to the socket from a client application I have, after using the rhc client tools to forward the ports.
The problem is, how do I connect to the server socket without port forwarding? I'm not able to connect to the server from any other computers, or from my own computer without port forwarding.
These are the things that I have tried so far:
Connecting to "localhost" at port 8000, which works after port forwarding.
Connecting to the website URL at port 8000, which always returns null.
Connecting to the website IP at port 8000, which results in a "Connection refused: connect" error.
Client code:
InetAddress ip = InetAddress.getByName("localhost");
//Also tried '127.5.8.129' which openshift says is the website IP, and the website URL itself.
Socket socket = new Socket(ip, 8000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("Hello World");
out.flush();
System.out.println(in.readLine());
socket.close();
EDIT: I can also bind the Server Socket to port 8443, but still can't access it remotely.
You need to bind to port 8080, and then access your application at it's public url on port 8000. Check out this kb article for more information about how the ports work on OpenShift: https://help.openshift.com/hc/en-us/articles/203263674-What-external-ports-are-available-on-OpenShift-

Google chrome connects multiple times to java HttpServer

I have this tiny java http server:
public class HttpServer {
public static void main(String args[]) {
int port;
ServerSocket server_socket;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
port = 8080;
}
try {
server_socket = new ServerSocket(port, 0, InetAddress.getByName("localhost"));
System.out.println("httpServer running on port "
+ server_socket.getLocalPort()
+ " address " + server_socket.getInetAddress()
);
} catch (IOException e) {
System.out.println(e);
}
}
}
When I connect with google chrome to localhost IDE console writes following:
httpServer running on port 8080 address localhost/127.0.0.1
New connection accepted /127.0.0.1:54839
New connection accepted /127.0.0.1:54840
Seems like google chrome connects two times to the server, but changing its port.
Why is it can be?
Since port 8080 on your client is already taken, the client's operating system will map the connection to a different unused port on the client. Your client connects from port 54839 and 54840 to port 80 on the server. To allow another client to connect to your server, the port is automatically redirected to an untaken port.
Here's a list of what happens...
Client opens up a Socket to connect to your server
Client's OS checks if the port the Socket is trying to connect to is used and if not looks for an unused port.
Client's OS assigns the socket to the unused local port it's detected in step 2.
Server receives connection request and accepts it. Server's OS redirects the connection from port 8080 to another port to allow more clients to connection.
Client and server have a chat and then disconnect.
54839 and 54840 are ports the OS assigned to the two Sockets your browser created when attempting to connect to your local website.
Edit: To correctly answer your question, the resources you send the browser causes it to connect twice. Once to retrieve the first resource and a second time to retrieve the resource the first requires.

Categories

Resources