How to Change the File Name for Each Uploaded Files to the Socket Server?

Hi, I found this code on the internet and I understand most of the code.
With this code every time I upload a file foe instance "daihatsu_ffmpeg_4.mp4" , the file output name is always name "receivedData".
Could any one help me please, how to create the output name on the server same as the upload file name. Or automatically save the output name same as for every uploaded file name.

Interesting. You say "I understand most of the code" yet you want somebody to modify in an almost trivial manner some poor quality and flawed code that you found on the Internet.

This sounds very very like a request to do your homework.

Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 05, 2011 03:29:23

0

James Sabre wrote:Interesting. You say "I understand most of the code" yet you want somebody to modify in an almost trivial manner some poor quality and flawed code that you found on the Internet.

This sounds very very like a request to do your homework.

Im sorry, I think you misunderstand. This is not my homework. This is part of my Thesis.
My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.

Aha; it is all clear now; it is not homework! Am I missing something? What is the essential difference between 'homework' and 'thesis' when it comes to who is supposed to do the work?

My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.

So have you followed the same approach for the rest of your thesis? Did you take a copy of someone else's work and get a forum to massage that so that it met your brief? It would seem to me that anyone who can do significant work on "Distributed Video encoding" should find the modification required to the upload code to be trivial. You are not so much "seeking for a help" as requesting someone to do the work for you.

Am I being unjust?

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 05, 2011 11:00:38

0

James Sabre wrote:

agustinus lumbantoruan wrote:

Im sorry, I think you misunderstand.

I understand perfectly.

This is not my homework. This is part of my Thesis.

Aha; it is all clear now; it is not homework! Am I missing something? What is the essential difference between 'homework' and 'thesis' when it comes to who is supposed to do the work?

My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.

So have you followed the same approach for the rest of your thesis? Did you take a copy of someone else's work and get a forum to massage that so that it met your brief? It would seem to me that anyone who can do significant work on "Distributed Video encoding" should find the modification required to the upload code to be trivial. You are not so much "seeking for a help" as requesting someone to do the work for you.

Am I being unjust?

OMG man. I am implementing that thesis title. I learned, took bit and pieces of other people's code, combine it, modified it, use it and apply it to make my thesis works. Oh James, you see, there is no original thesis. Everybody borrows on every body idea and works. It's sharing, being open for progress. FYI, I put reference on my thesis book, where i got the piece of code and modified it. If you are sincerely would like to give me help, please do. I do not have time to argue with this and i do not want to argue to this, copying stuff arguement. People invent things by learning/copying what others are doing. Take a look around.

If you're able to write a multithreaded video encoding then you should be able to read documentation and use google.
I would advise you to read this and then you'll be able to comprehend why the
code you found is bad.

Wouter Oet wrote:If you're able to write a multithreaded video encoding then you should be able to read documentation and use google.
I would advise you to read this and then you'll be able to comprehend why the
code you found is bad.

Thanks Wouter for the reference. I will read the tutorial and try to come up with a solution. I'll come back soon.
I appreciate it man.

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 06, 2011 16:08:31

0

Ok so this is the latest update code. I decided to create a server that keeps on listening.
The server uses the socket input stream to get the uploaded data from the client.
At the end, there is a loop which reads buffereinputstream.

Unforunately, each file uploaded from the client is always missing about 8 bytes. Or in other word, the file becomes corrupted (missing bytes).
Interestingly the server side always gave me this message at the end. Correct me if im wrong that this because the loop hsa reached -1?

I haven't gone through the code in detail but these is at least one problem that could cause you to loose bytes. You get the file name from the input by wrapping the input in a BufferedReader and reading a line. You then use this name to create the file and then read the file content from a a BufferedInputStream which wraps the the original InputStream. This means that anything buffered in the BufferedReader will be lost forever.

Consider the use of DataInputStream and DataOutputStream. You can then use the readUTF() and writeUTF() methods to pass the file name and use the data streams directly to write and read the file content.

Edit: This code
is very suspect. You seem to be trying to read a buffer full of data and then write "something" but you sure as heck are not writing out the data you have written. All you need is something along the lines of

Note : there are very few legitimate uses for available() and in conjunction with reading from a socket like you are doing is definitely not one of them.

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 07, 2011 08:12:06

0

James Sabre wrote:I haven't gone through the code in detail but these is at least one problem that could cause you to loose bytes. You get the file name from the input by wrapping the input in a BufferedReader and reading a line. You then use this name to create the file and then read the file content from a a BufferedInputStream which wraps the the original InputStream. This means that anything buffered in the BufferedReader will be lost forever.

Consider the use of DataInputStream and DataOutputStream. You can then use the readUTF() and writeUTF() methods to pass the file name and use the data streams directly to write and read the file content.

Edit: This code
is very suspect. You seem to be trying to read a buffer full of data and then write "something" but you sure as heck are not writing out the data you have written. All you need is something along the lines of

Note : there are very few legitimate uses for available() and in conjunction with reading from a socket like you are doing is definitely not one of them.

Thanks man for the help! I appreciate it. I decide it to re-program it from the beginning. I took your advice about the loop and modify it a little bit. The good news is, it's working perfectly as expected. The output file at the server side is saved same as the original file name and no more byte loss

Looks like I wasted my time; you are still using a buffered reader in the server!

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 07, 2011 08:56:08

0

James Sabre wrote:Looks like I wasted my time; you are still using a buffered reader in the server!

I use buffered reader to get the file name.

From the java docs
public String readLine() throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

If i do not use "BufferedReader" how can i get the input file name? I want the transfered file to the server has the same name as the inputted name from the client.
The bufferedreader reads the client's PrintWriter. In the client side, I wrote this in line 25-27
If i do not use the BufferedReader, i have to write the file name manually and that is not what I want. Imagine if there are >100 videos uploaded from each encoder server to the streaming server
Do you know any other alternative?

Method names should start with a lowercase letter.
Sometimes you're logging with java.util.Logger (which isn't really a good solution, check out SLF4J) and other times you're using System.out.
Your constructors invoke overridable methods.
Your constructors execute functionality. I think it's better to use constructors only for initialization.
You could close resources sooner.
In Client on line 37 you probably want t.start() instead of t.run().

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56

posted Jun 09, 2011 06:06:38

0

Wouter Oet wrote:Couple of remarks:

Method names should start with a lowercase letter.
Sometimes you're logging with java.util.Logger (which isn't really a good solution, check out SLF4J) and other times you're using System.out.
Your constructors invoke overridable methods.
Your constructors execute functionality. I think it's better to use constructors only for initialization.
You could close resources sooner.
In Client on line 37 you probably want t.start() instead of t.run().

Hi thanks for the remarks. I have do your remarks except for this part which
I do not understand "You could close resources sooner."

The methods which close the resources are not correct. For instance if out.close(); (on line 84) throws an exception then dis and sock won't be closed.
You'll need a (couple) of try-finally blocks. Also check if the resource isn't null since that can be the case if the resources couldn't be opened.