aspose file tools*
The moose likes Java in General and the fly likes Sending a file over socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sending a file over socket" Watch "Sending a file over socket" New topic
Author

Sending a file over socket

Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Hello

I think i am very close, i have a server and a client, the client sends a file over a socket to the server who create the file.

I get the correct number of bytes when System.out.println("Total number of recieved bytes: " + mybytearray.length); is called, so that part is OK.

But the file is never written on the server side, i get the following error from the client side:



There are no errors coming from the server

Server


Client


I sucpect the problem is in how the file is written, but i am not sure. Any help is greatly appriciated.

Kind regards Mads Nielsen
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

My guess: Line 33 in your Server code throws an exception because that file doesn't exist. That causes the socket to be forced closed as the server crashes, hence the error at the client side.

There's plenty more things waiting to go wrong in that code, but I think that's your immediate problem.
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:My guess: Line 33 in your Server code throws an exception because that file doesn't exist. That causes the socket to be forced closed as the server crashes, hence the error at the client side.

There's plenty more things waiting to go wrong in that code, but I think that's your immediate problem.


I get the same error when the file is present on the server. and the server never crashes.

Edit: i know what you mean by crashing now.


There's plenty more things waiting to go wrong in that code, but I think that's your immediate problem


Please elaborate.

Kind regards Mads Nielsen
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Can you explain why your server code reads data from the socket, then does nothing with it? And why does it attempt to copy a file to itself?

Also you have numerous bad practices in your code. You use DataInputStream even though you aren't using any of its features. That's unnecessary. You use a ridiculously large buffer when you read the file in your client, and you assume without proof that the read from the file will fill the buffer. If the file is very large you will not have enough memory for the buffer, either. You should use a fixed-size buffer (as in your server code) and write the loop with copies from the file to the socket. In your server you have a magic number (3261440) for your buffer size -- where did that come from?
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:Can you explain why your server code reads data from the socket, then does nothing with it? And why does it attempt to copy a file to itself?

Also you have numerous bad practices in your code. You use DataInputStream even though you aren't using any of its features. That's unnecessary. You use a ridiculously large buffer when you read the file in your client, and you assume without proof that the read from the file will fill the buffer. If the file is very large you will not have enough memory for the buffer, either. You should use a fixed-size buffer (as in your server code) and write the loop with copies from the file to the socket. In your server you have a magic number (3261440) for your buffer size -- where did that come from?


Can you explain why your server code reads data from the socket, then does nothing with it? And why does it attempt to copy a file to itself?


No. Thats why i am on this forum, i have tried to make the code work, and i am now at a dead end, and hoping to get some help in here.

In your server you have a magic number (3261440) for your buffer size -- where did that come from?


Thats the size of the file i am testing with at the moment.

Kind regards Mads Nielsen

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Starting at line 37 in your server you have some perfectly good code which copies data from an input stream to an output stream.

You want to copy data from the socket's input stream to that output stream, right?

Then remove all the code before line 37 which doesn't contribute to that process (which is a large chunk of code). Leave only code which copies from the socket's input stream to the file's output stream.
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:Starting at line 37 in your server you have some perfectly good code which copies data from an input stream to an output stream.

You want to copy data from the socket's input stream to that output stream, right?

Then remove all the code before line 37 which doesn't contribute to that process (which is a large chunk of code). Leave only code which copies from the socket's input stream to the file's output stream.


I have removed the code you suggested. But i still have some trouble transfering the data from the socket to the file. (I dont know how)

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Mads Nielsen wrote:I have removed the code you suggested.


Really?

Let me be blunt, then. Lines 20 to 27 in the code you posted should be thrown away. Once you have done that, fix the remaining code so that it copies from the socket's input stream.`
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:
Mads Nielsen wrote:I have removed the code you suggested.


Really?

Let me be blunt, then. Lines 20 to 27 in the code you posted should be thrown away. Once you have done that, fix the remaining code so that it copies from the socket's input stream.`


Done.



fix the remaining code so that it copies from the socket's input stream.[/


How ?

If i knew how i would not have used this forum.

Kind regards Mads Nielsen
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Mads Nielsen wrote:If i knew how i would not have used this forum.


You seem to be under the impression that this forum is a code-writing or homework-doing service. It isn't. I can't believe that you don't know what to do now; did you not write the original code yourself? Anyway, you're going to have to explain what your problem is. Just saying "I don't know what to do" isn't going to get you anywhere.
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:
Mads Nielsen wrote:If i knew how i would not have used this forum.


You seem to be under the impression that this forum is a code-writing or homework-doing service. It isn't. I can't believe that you don't know what to do now; did you not write the original code yourself? Anyway, you're going to have to explain what your problem is. Just saying "I don't know what to do" isn't going to get you anywhere.


You seem to be under the impression that this forum is a code-writing or homework-doing service


This code is 100% percent for my own purposes. I think its ok that you tell me how to fix the problem when i already have 95% of the code.

I can't believe that you don't know what to do now


I am new at Java. No some of the code is not written by me, and some of it is, its a mix.

I dont know how to transfer the data from the clientSocket to the fileOutputStream.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Okay. You would copy the data from the socket's input stream. (I didn't think I should have to tell you that because your original post had code which read data from the socket's input stream.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Mads Nielsen wrote:This code is 100% percent for my own purposes. I think its ok that you tell me how to fix the problem when i already have 95% of the code.


The original server code looks like it is trying to do two different things. The first half is opening a server socket, accepting a data socket, and doing nothing with it. The second half, is trying to open a file for reading, and open the same file for writing, and trying to copy the file to itself. There doesn't seem to be much, if any, relationships between the two halves of the server code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Paul Clapham wrote:Okay. You would copy the data from the socket's input stream. (I didn't think I should have to tell you that because your original post had code which read data from the socket's input stream.)




This is working. Thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending a file over socket