I am wondering how this works: https://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readUTF()
If we use a DataOutputStream to writeUTF("dog")
and writeUTF("cat")
, does readUTF()
called once get 'dog' and then called a 2nd time get 'cat'?
Also, what is the best way to accomplish this (from this code: https://github.com/richardrl/downloader/tree/master/src/main/java): I have a file on 1 out of N servers. I have the client query one of the servers. If it's not on the server, I want the client to download the file from the correct server in at most 2 requests.
My plan right now is to have a hashmap (of all files across all servers) that maps each file to the port number and domain name of the server it's on (the two properties to locate a server socket). When the client requests from any server, the requested server will search the hashmap for the file. If not found on the current server, the current server will send an error (currently accomplished by writeBoolean(false)
) and then the port number and domain name of the correct server that holds the file.
My current problem is I am getting NoRouteToHostException through the following process.
Clone all files here: https://github.com/richardrl/downloader/tree/master/target/classes
Go into "dummyserver" folder and type in Terminal: 'java DLServer 1124' to start server. As you can see, the requested file "dummy.txt" is in the same folder as this server.
Go back up one level and type in: 'java DLServer 4444'.
Now type in 'java DownloaderClient localhost 4444' to start client. Type in "dummy.txt" to request "dummy.txt". Instead of correctly connecting to the dummyserver, I get this error:
java DownloaderClient localhost 4444 In while loop dummy.txt Client: dummy.txt Exception in thread "main" java.net.NoRouteToHostException: Can't assign requested address at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.(Socket.java:425) at java.net.Socket.(Socket.java:208) at DownloaderClient.main(DownloaderClient.java:21)