FTP Client listFiles() work in some machines - java

I have a program that make use of FTP Client, it is running in several machines and it stoped working in one of those machines.
Making the debug I can see that the problem is on listFiles(), listNames()... But it work's perfect giving me the name of the directory when I make printWorkingDirectory() or when I make FTPReply.isPositiveCompletion(ftp.getReplyCode()) it brings me the true value.
I have also made ftp.enterLocalPassiveMode(), ftp.setFileType(FTPClient.BINARY_FILE_TYPE).
I'm lost, can you please help me?

Sounds like the data connection is blocked - most likely in a router or firewall. You need to have the machine "behind" a NAT device be the active one.
What is the exact network configuration between the two machines you have problems with?

Related

How to get IP's of all network devices in Java

I'm working on a project, and basically I need this program to find a list of all connected IP addresses to a network, such as going to your network connections in Windows. I've looked all over the internet, and I cant find what I'm looking for and I hope someone can!
This program cannot just crunch numbers (such as pinging all possible IP addresses), it needs to be faster than that. Being able to just connect to the network and obtain a "list" of all IP's that it can see would be perfect. Using the docs.oracle.com webpage did what it was supposed to, but there were too many extra results (including Eth10, etc). Also, the only IP address it listed that wasnt my own didnt match any active computers, so I dont know what was up with that... (192.168.0.9 was listed, the only other active connection was at 192.168.0.10, maybe I'm missing something?).
What is a way to do this? Sorry if I'm unclear, I'm able to be clearer if there's something specific, hope this helps!
Thanks!
EDIT: I hope this helps; I want to do this in java, because for my designed program to work it needs a list of active IP addresses connectable to the active machine. Meaning that if I run it on a laptop at a school, I need all the IP addresses that I could connect to on the network. It is a network thing, but it does need to be done in java. The purpose is for a file manager, otherwise its pretty tough to explain. Does that help?
The only way to do what you are asking is via brute force. It can be done somewhat quickly by creating multiple threads that fire off TCP SYN requests to random IPs on the network (this is what NMap does). If you had access to the DHCP server then you could likely get a list from that point. I would warn you to tread very lightly. Running NMAP or doing port scans on a network is considered an attack by many network admins... you could get yourself in to trouble (they WILL notice).

windows service access to network before logon

I wrote a windows service in Java, and it starts automatically (before user login to Windows).
My problem is that it looks like the service has no access to the network before logon.
In the logs I see that before logon, I get: java.net.UnknownHostException. However, the same code works fine after logon.
I tried to define the service as "network service", it did not helped.
Can anyone please help?
BTW:
There is no WiFi involved
I looked how TeamViewer program works and I saw that they have some registry values in FirewallPolicy. and they defined their service as local system.
Which made me think maybe it has something to do with the firewall settings, however I cannot find anything in the web about it.
I just managed to solve this. The issue was that the service started before there was access to the internet. Keep in mind that the network might be accessible but the internet isn't.
I solved it by doing: (pseudo code)
while (false) {
check if http://www.example.com is accesible:
if true - break
if false - sleep for 3 seconds
}
... (rest of the code)
If the exception one gets is UnknownHostException, this would imply that somehow DNS resolution failed..
Note further that DNS resolution itself depends on IP. Hence, if the machine in question is slow to connect to the respective gateway (router), then DNS resolution has no chance of succeeding.
Hence, while you may try to resolve the host resolution in a loop with a sleep (as suggested by Kuf).. I'd suggest try to log how soon does the machine gets the IP address after the reboot (a sign of how soon it gets connected to the Network), and then go from there (additional SuperUser or ServerFault question perhaps).

Does windows use the same port for outgoing connections for two programs running in the same jvm?

My specific problem is I have a tomcat comet servlet that my java application connects to. For some reason, I am getting random END events and I cant figure out why. I wonder, as Im trying to weed out whats not wrong, is if windows when connecting to the same ip and port, from within the same jvm, would give two processes the same port? This would make it look like to the server that the connection is coming from the same place? Does it work this way? I open two instances of the application from netbeans, the same JVM, and Im wondering if this can occur?
I wonder, as Im trying to weed out whats not wrong, is if windows when connecting to the same ip and port, from within the same jvm, would give two processes the same port?
No. Windows will always allocate a new local port for outbound connections.
This would make it look like to the server that the connection is coming from the same place? Does it work this way?
No.
I open two instances of the application from netbeans, the same JVM, and Im wondering if this can occur?
No.
The bug referred to in #TapanPandya's answer concerns ServerSockets and explicit bind() invocations. It isn't relevant to outbound connections.
There was a bug in Java 7 some time back which allowed multiple applications to use same port.
JDK-7179799. Also, check similar question Can two applications listen to the same port

Server Socket: No port Forwarding

Thanks for any answers in advance.
Anyways, I made an android application, (This isn't an android question), and it connects to your computer so you can monitor it. Well I have the server for the computer ready to the client from the phone can access it, it works perfectly. The problem is, You have to port forward, and not everybody knows how to do that. Is there a way so you can programmatically make it so you do not have to port forward?
Thanks again!
You mean you had to set up port forwarding on your router (because you're using a router with your broadband connection)?
No.
You would need to set up a intermediary server on the internet somewhere that both could connect to, then forward the data through that.
You can have both connect to a server of yours. More of a pain than any other method, but guaranteed to work.
I'm not sure about the specifics of the droid, but you may be able to access the droid directly through its IP.
Otherwise you could set up a simple tutorial program, with specific instructions on how to port forward. You could even make a little program to access certain popular vendors port forwarding directly. Maybe just a little thing that asks for a password and connects and does everything automatically. Wouldn't be horribly difficult with some vendors.

How do I send data between two computers over the internet

I have been struggling with this for the entire day now, I hope somebody can help me with this.
My problem is fairly simple: I wish to transfer data (mostly simple commands) from one PC to another over the internet.
I have been able to achieve this using sockets in Java when both computers are connected to my home router. I then connected both computers to the internet using two different mobile phones and attempted to transmit the data again. I used the mobile phones as this provides a direct route to the internet and if I use my router I have to set up port forwarding, at least, that is how I understand it.
I think the problem lies in the method that I set up the client socket. I used:
Socket kkSocket = new Socket(ipAddress, 3333);
where ipAddress is the IP address of the computer running the server. I got the IP address by right-clicking on the connection, status, support. Is that the correct IP address to use or where can I obtain the address of the server? Also, is it possible to get a fixed name for my computer that I can use instead of entering the IP address, as this changes every time I connect to the internet using my mobile phone?
Alternatively, are there better methods to solving my problem such as using http, and if so, where can I find more information about this?
EDIT:
Would it be possible to have the server program running on a server on the internet somewhere. My original server would then be a client that send information to this server. This server would then pass this information to my original client and vice versa. That way, the IP address of my computer won't matter, as I only need to know the address of the server hosted somewhere on the web. Does this seem like a better solution? Where do I begin implementing such a solution?
Thanks!
When you connected to the server that serves StackOverflow, did you type in the IP address? It's 64.34.119.12, if that jogs your memory.
You probably didn't. You probably typed "stackoverflow.com". There's a huge, complex, clever, and in some ways, poorly implemented system called DNS that translates sensible and human-readable names into IP addresses.
One problem with DNS, though, is you need a "static IP", which is exactly what it sounds like: an IP address that doesn't change, which is exactly what you don't have.
So, what can you do?
You can buy a static IP account from your ISP (pretty expensive)
You can use some proxy out in the Internet (a machine that does have a static IP and is willing to bounce your packets back and forth -- I'm not aware of any service that does this for you; you could write one and put it up on Amazon Web Services or Google App Engine, both of which would be free at your level of usage, but they'd be slow, since every packet trying to cross your living room would have have to go via some data-center in Virginia).
You can keep doing what you're doing, looking in the net-configuration of your machine.
You could speed (3) up a little by having your server program look up its own IP address and print it out where you could see it and type it into the server by hand.
You can use DynDNS, as Sergey mentioned (this is the "right" solution, in that it's very general, it just might be a little complicated to set up)
You can use multi-casting.
Multi-casting is an interesting solution, and it may work for you. The idea is, when your server starts up, it announces to the net, "Here I am, I'm providing X server, here's my IP address, talk to me." The problem is, a multi-cast won't leave your living room. Obviously, if every multi-cast were spread to every computer on the Internet, the whole thing would collapse, so your router will ignore, and not route, multi-cast packets. That may or may not be a deal-breaker for you. EDIT Re-reading your question, I see it is a deal-breaker for you. I'd go with #5, but be aware there may be routing issues (address translations that prevent a server from knowing the address that other computers can find it at) or fire-wall issues (that is, your ISP may prevent your server from receiving incoming packets even if the address is correct).
using a direct socket connection with a port like 3333 is usually complicated because different network configurations.
firewalls will make a pleasure preventing the connection, or killing it from time to time.
maintaining a 2-way connection can be a nighmare. the SIP protocol is struggling with this kind of problems.
For a simple application, i suggest you look into the comet technology, where your clients can establish an http connection with a shared server. The server can then bridge commands between them.
html5 will also bring the websocket protocol to the table.
I got the IP address by right-clicking
on the connection, status, support.
Not sure about the "support" part, and I'm not on a Windows machine right now, but I think that the most easy and reliable way to figure out the IP address on Windows is to run "ipconfig" from the command line (Win+R, type "cmd", then "ipconfig" in the opened window). This, of course, should be done on the server side.
However, the problem is that depending on the ISP your IP address may be not within the Internet, but within a local ISP network (so-called NAT). In this case, you'll need to use some sort of black magic called TCP hole punching, which is very complicated and not guaranteed to work. You can figure out if your address is local or not by looking at it. For IPv4 local addresses are almost always like 10.x.x.x or 172.16-31.x.x, or 192.168.x.x. Don't know about IPv6.
You can also check your IP by visiting one of the special sites like www.whatismyip.com. If the address they tell you is different from the one you see by running "ipconfig" or looking at the connection properties, then you're almost certainly behind a NAT (or your ISP is using a transparent proxy, but that's rare).
If you are directly connected to Internet (no local addresses and NAT), then you should also check if you have any firewall software and either to configure it to allow connections to the port you use, or make sure it's in "ask the user" (and not "silently reject") mode, or just disable it completely (this may put your computer at risk, especially if there is no anti-virus software or the system isn't up-to-date).
Also, is it possible to get a fixed
name for my computer that I can use
instead of entering the IP address, as
this changes every time I connect to
the internet using my mobile phone?
Yes, it's possible. There is the thing called DynDNS, and there are DynDNS providers like DynDNS.com, where you can get a third-level domain name for free (like mycoolpc.dyndns.org). You'll have to install and configure some DynDNS client on your PC that will tell the DynDNS server its new IP each time each changed. I don't know anything about particular clients to use because I'm using the one built-in in my home router.
No need to write networking code for this, unless it really floats your boat. Take a look at SCP. http://amath.colorado.edu/computing/software/man/scp.html. There is a windows implementation where you can download putty (windows ssh client), and it is on most linux distributions. Alternatively, you could set up an FTP or SSH server on one or both of the machines.
"a fixed name for my computer that I can use instead of entering the IP address" would be a domain name, these are purchasable online for a few bucks.

Categories