Member Avatar for Ajantis

Hello :)

I am creating a simple client/server file sharing application which I run on same computer. Mine. :) I wish to make the client be able to get a certain file by letting user type "get filename" in the console. When they do, and the server responds, the server checks if there is such a file and sends to the user. While I got the basics working - like being able to send/recieve primitive types - I still have trouble actually sending a file. And letting the client have the command "get filename". Could someone assisst please?

Best Wishes

Here's my coding:
http://pastebin.com/Spfp6XFC

Recommended Answers

All 12 Replies

I still have trouble actually sending a file

Can you explain in more detail what the problem is?
Here are two compiler problems I got when I tried to compile your program.

SimpleServer.java:67: cannot find symbol
symbol : constructor BufferedReader(int)
location: class java.io.BufferedReader
BufferedReader bfr = new BufferedReader(inputFromClient.read(buffer));
^
SimpleServer.java:82: cannot find symbol
symbol : variable outToClient
location: class SimpleServer
outToClient.write(buf, 0, len);
^

1)Can you explain why you're putting the read() method in the Constructor for BufferedReader?
What does read() return?
2) where is outToClient defined?

Please compile your code before posting it and fix these typo errors!!

Member Avatar for Ajantis

Can you explain in more detail what the problem is?
Here are two compiler problems I got when I tried to compile your program.

1)Can you explain why you're putting the read() method in the Constructor for BufferedReader?
What does read() return?
2) where is outToClient defined?

Please compile your code before posting it and fix these typo errors!!

Sorry. My bad. Here is the code, which I "cleaned" up.
I used read() because I don't fully understand BufferedReader.
As far as I get it, it returns int value of the character it encounters.
I forgot to add "put" in the outToClient. outputToClient should it be. :)

The problem is, that no file with the name of user input is ever sent to client.
And once the the user writes an input - the client seemingly does nothing. And nothing is sent to client from the server.

Server side:
http://pastebin.com/K2jn8bUt

Client side:
http://pastebin.com/3rHSi7vJ

I don't fully understand

If you read the API doc it tells you what type the Constructor takes. What does it want?
read() returns an int, so that obviously is wrong.

problem is, that no file with the name of user input is ever sent to client

How is this supposed to happen? How does the server get the name of the file to send?

Member Avatar for Ajantis

If you read the API doc it tells you what type the Constructor takes. What does it want?
read() returns an int, so that obviously is wrong.

How is this supposed to happen? How does the server get the name of the file to send?

Yes, true. I don't always understand what they are actually meaning in the API however. That's the trick. Sure, constructors and all and their reutrn types is shown - which makes it a little easier. But when learning about what things do, as the case with read() method, I don't understand always. In the case for read(), it returns an int - but is said to read characters? The best way I interpreted that information is that it returns the int value of that char which is read.


As for the file sending - I have thought this way:
First, the input and output streams are created so that information can be send/recieved.
Then they are tried to be created. When accomplished, the user should write the filename with the Scanner object. That value is then stored in a string "filename". Then, that value is passed to the server (line 56 in SimpleClient class) and the output is flushed.
The "filename" value from client is used as input on the server side (line 58 in SimpleServer class) and in that way is passed as a string value for "filename" in server class. Then, if that file exists in the server side (line 68 SimpleServer class), it is written in the output - and in that way sent to the client. But... it seems like I haven't written a code to actually write a file... doesn't seem like anything will be written either. At least - that's my observation. Do you have an advice?

Your explanation of what you want to do is good and makes sense.
So do the steps one at a time. Try to get these steps done first.
Are you able to get the name of the file from the user?
Are you able to send that name to the server?
Does the server read that name correctly?

Member Avatar for Ajantis

Your explanation of what you want to do is good and makes sense.
So do the steps one at a time. Try to get these steps done first.
Are you able to get the name of the file from the user?
Are you able to send that name to the server?
Does the server read that name correctly?

I've just been checking through it over and over and over again.
And it doesn't seem like it's something from the server side. Franquely,
I believe it is the client part that doesn't send proper information.
Most notably, on line 56. It is supposed to write a sequence of chars and send it to
the server part. And in the server part, I have the code that should do the job right:

BufferedReader bfr = new BufferedReader(new InputStreamReader(inputFromClient));

But wouldn't this code be "translating" the input stream correctly from the client? Sheesh - all these streams, inputs and outputs make me go nuts!

I've made some debugging changes to your code and do not have a solution yet. Here are your programs with an old one of mine.

Member Avatar for Ajantis

Hello again,

I don't work with GUIs so I don't understand that code. :)
HOWEVER, I DID notice something odd. When I commented out
a sc.nextLine() in the client side - it actually wrote a file.
But there was no content. Now I am even more confused. :S
Here's the coding:

Server:
http://pastebin.com/UYb5gbDC

Client:
http://pastebin.com/9kPSFR70

I don't work with GUIs so I don't understand that code

Which code is that?
Did you try my ClientServerTest.java?
It doesn't have any GUI.

Sorry, it does have GUI. But that's not important
Look at the classes and methods it uses to send/receive data.

can u share ur code with me ? actually developing app for android and very less time ... plz reply as soon as poss

commented: Two year old thread -3

This thread is two years old.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.