1
votes

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.

  1. Clone all files here: https://github.com/richardrl/downloader/tree/master/target/classes

  2. 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.

  3. Go back up one level and type in: 'java DLServer 4444'.

  4. 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)

1

1 Answers

0
votes

I'm a little confused about what your asking, so I'll just try to answer the question in the title. For the small amount of time I've used java sockets, whenever I sent data I first turned the data into an array of bytes then added a byte in the first index to determine what the byte array should be interpreted as when it is received. Check here to learn how to do that - http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte

I'm not sure if this answered your question, but maybe this can help.