• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sending a file over socket

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic