How to transfer chunks from different clients and recreate the complete file in server using Java?
So, I'm working on a chat with file transfer funcionality. It's kind of close to be done but I'm struggling to make the transfer like I need to.
I need to achieve the following:
if file "A" is located ONLY in client "2", then client "2" should send 100% of the file to client "1"
if file "A" is located in client "2" and client "3" also has file "A", then client "2" should send 50% of the file to client "1" and client "3" should send the other 50%.
(if the file is located in 4 clients i should be 25% each....and so it goes...)
So, I managed to make the one-to-one (100%) transfer, but now I can't make the shared many-to-one transfer.
I was trying to adapt these methods I found on the internet (I've already made a few modifications to the methods) so I could use tha variable "pos" to control how many chunks would a client send, and then I would create a dinamic int variable to keep the quantity of clients I had to share the file with and use it to control the transfer.
private static void writeFile(File file, OutputStream outStream, long fileSize)
FileInputStream reader = null;
try
reader = new FileInputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int pos = 0;
int bytesRead;
while ((bytesRead = reader.read(buffer, 0, CHUNK_SIZE)) >= 0)
outStream.write(buffer, 0, bytesRead);
outStream.flush();
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
catch (IndexOutOfBoundsException e)
System.err.println("Error while reading file");
e.printStackTrace();
catch (IOException e)
System.err.println("Error while writing " + file.toString() + " to output stream");
e.printStackTrace();
finally
if (reader != null)
try
reader.close();
catch (IOException e)
e.printStackTrace();
private static void saveFile(File file, InputStream inStream, long fileSize)
FileOutputStream fileOut = null;
try
fileOut = new FileOutputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int bytesRead;
int pos = 0;
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos == fileSize)
break;
catch (IOException e)
e.printStackTrace();
finally
if (fileOut != null)
try
fileOut.close();
catch (IOException e)
e.printStackTrace();
System.out.println("Finished, filesize = " + file.length());
But that idea seems to only work for the first transfer. I changed the "while" loop in saveFile like this, so it would only save 50% of the file from the first client, and as expected I got half of the .png i was using to test.
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos >= fileSize/2)
break;
But I know I can't complete the .png! The second client can't send the the chuks where "(pos >= sizeFile/2)"
and I get the following error when I try to do so:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at tor.Client.run(Client.java:109)
Line 109 is:
Socket requester = new Socket("192.168.3.114", 2007);
Can anyone bring some light to me over this problem?
java file sockets output transfer
add a comment |
So, I'm working on a chat with file transfer funcionality. It's kind of close to be done but I'm struggling to make the transfer like I need to.
I need to achieve the following:
if file "A" is located ONLY in client "2", then client "2" should send 100% of the file to client "1"
if file "A" is located in client "2" and client "3" also has file "A", then client "2" should send 50% of the file to client "1" and client "3" should send the other 50%.
(if the file is located in 4 clients i should be 25% each....and so it goes...)
So, I managed to make the one-to-one (100%) transfer, but now I can't make the shared many-to-one transfer.
I was trying to adapt these methods I found on the internet (I've already made a few modifications to the methods) so I could use tha variable "pos" to control how many chunks would a client send, and then I would create a dinamic int variable to keep the quantity of clients I had to share the file with and use it to control the transfer.
private static void writeFile(File file, OutputStream outStream, long fileSize)
FileInputStream reader = null;
try
reader = new FileInputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int pos = 0;
int bytesRead;
while ((bytesRead = reader.read(buffer, 0, CHUNK_SIZE)) >= 0)
outStream.write(buffer, 0, bytesRead);
outStream.flush();
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
catch (IndexOutOfBoundsException e)
System.err.println("Error while reading file");
e.printStackTrace();
catch (IOException e)
System.err.println("Error while writing " + file.toString() + " to output stream");
e.printStackTrace();
finally
if (reader != null)
try
reader.close();
catch (IOException e)
e.printStackTrace();
private static void saveFile(File file, InputStream inStream, long fileSize)
FileOutputStream fileOut = null;
try
fileOut = new FileOutputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int bytesRead;
int pos = 0;
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos == fileSize)
break;
catch (IOException e)
e.printStackTrace();
finally
if (fileOut != null)
try
fileOut.close();
catch (IOException e)
e.printStackTrace();
System.out.println("Finished, filesize = " + file.length());
But that idea seems to only work for the first transfer. I changed the "while" loop in saveFile like this, so it would only save 50% of the file from the first client, and as expected I got half of the .png i was using to test.
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos >= fileSize/2)
break;
But I know I can't complete the .png! The second client can't send the the chuks where "(pos >= sizeFile/2)"
and I get the following error when I try to do so:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at tor.Client.run(Client.java:109)
Line 109 is:
Socket requester = new Socket("192.168.3.114", 2007);
Can anyone bring some light to me over this problem?
java file sockets output transfer
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20
add a comment |
So, I'm working on a chat with file transfer funcionality. It's kind of close to be done but I'm struggling to make the transfer like I need to.
I need to achieve the following:
if file "A" is located ONLY in client "2", then client "2" should send 100% of the file to client "1"
if file "A" is located in client "2" and client "3" also has file "A", then client "2" should send 50% of the file to client "1" and client "3" should send the other 50%.
(if the file is located in 4 clients i should be 25% each....and so it goes...)
So, I managed to make the one-to-one (100%) transfer, but now I can't make the shared many-to-one transfer.
I was trying to adapt these methods I found on the internet (I've already made a few modifications to the methods) so I could use tha variable "pos" to control how many chunks would a client send, and then I would create a dinamic int variable to keep the quantity of clients I had to share the file with and use it to control the transfer.
private static void writeFile(File file, OutputStream outStream, long fileSize)
FileInputStream reader = null;
try
reader = new FileInputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int pos = 0;
int bytesRead;
while ((bytesRead = reader.read(buffer, 0, CHUNK_SIZE)) >= 0)
outStream.write(buffer, 0, bytesRead);
outStream.flush();
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
catch (IndexOutOfBoundsException e)
System.err.println("Error while reading file");
e.printStackTrace();
catch (IOException e)
System.err.println("Error while writing " + file.toString() + " to output stream");
e.printStackTrace();
finally
if (reader != null)
try
reader.close();
catch (IOException e)
e.printStackTrace();
private static void saveFile(File file, InputStream inStream, long fileSize)
FileOutputStream fileOut = null;
try
fileOut = new FileOutputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int bytesRead;
int pos = 0;
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos == fileSize)
break;
catch (IOException e)
e.printStackTrace();
finally
if (fileOut != null)
try
fileOut.close();
catch (IOException e)
e.printStackTrace();
System.out.println("Finished, filesize = " + file.length());
But that idea seems to only work for the first transfer. I changed the "while" loop in saveFile like this, so it would only save 50% of the file from the first client, and as expected I got half of the .png i was using to test.
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos >= fileSize/2)
break;
But I know I can't complete the .png! The second client can't send the the chuks where "(pos >= sizeFile/2)"
and I get the following error when I try to do so:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at tor.Client.run(Client.java:109)
Line 109 is:
Socket requester = new Socket("192.168.3.114", 2007);
Can anyone bring some light to me over this problem?
java file sockets output transfer
So, I'm working on a chat with file transfer funcionality. It's kind of close to be done but I'm struggling to make the transfer like I need to.
I need to achieve the following:
if file "A" is located ONLY in client "2", then client "2" should send 100% of the file to client "1"
if file "A" is located in client "2" and client "3" also has file "A", then client "2" should send 50% of the file to client "1" and client "3" should send the other 50%.
(if the file is located in 4 clients i should be 25% each....and so it goes...)
So, I managed to make the one-to-one (100%) transfer, but now I can't make the shared many-to-one transfer.
I was trying to adapt these methods I found on the internet (I've already made a few modifications to the methods) so I could use tha variable "pos" to control how many chunks would a client send, and then I would create a dinamic int variable to keep the quantity of clients I had to share the file with and use it to control the transfer.
private static void writeFile(File file, OutputStream outStream, long fileSize)
FileInputStream reader = null;
try
reader = new FileInputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int pos = 0;
int bytesRead;
while ((bytesRead = reader.read(buffer, 0, CHUNK_SIZE)) >= 0)
outStream.write(buffer, 0, bytesRead);
outStream.flush();
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
catch (IndexOutOfBoundsException e)
System.err.println("Error while reading file");
e.printStackTrace();
catch (IOException e)
System.err.println("Error while writing " + file.toString() + " to output stream");
e.printStackTrace();
finally
if (reader != null)
try
reader.close();
catch (IOException e)
e.printStackTrace();
private static void saveFile(File file, InputStream inStream, long fileSize)
FileOutputStream fileOut = null;
try
fileOut = new FileOutputStream(file);
byte buffer = new byte[CHUNK_SIZE];
int bytesRead;
int pos = 0;
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos == fileSize)
break;
catch (IOException e)
e.printStackTrace();
finally
if (fileOut != null)
try
fileOut.close();
catch (IOException e)
e.printStackTrace();
System.out.println("Finished, filesize = " + file.length());
But that idea seems to only work for the first transfer. I changed the "while" loop in saveFile like this, so it would only save 50% of the file from the first client, and as expected I got half of the .png i was using to test.
while ((bytesRead = inStream.read(buffer, 0, CHUNK_SIZE)) >= 0)
pos += bytesRead;
System.out.println(pos + " bytes (" + bytesRead + " bytes read)");
fileOut.write(buffer, 0, bytesRead);
if (pos >= fileSize/2)
break;
But I know I can't complete the .png! The second client can't send the the chuks where "(pos >= sizeFile/2)"
and I get the following error when I try to do so:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at tor.Client.run(Client.java:109)
Line 109 is:
Socket requester = new Socket("192.168.3.114", 2007);
Can anyone bring some light to me over this problem?
java file sockets output transfer
java file sockets output transfer
edited Nov 18 '18 at 21:08
Bruno Lira
asked Nov 13 '18 at 22:13
Bruno LiraBruno Lira
112
112
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20
add a comment |
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290304%2fhow-to-transfer-chunks-from-different-clients-and-recreate-the-complete-file-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290304%2fhow-to-transfer-chunks-from-different-clients-and-recreate-the-complete-file-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
We would probably need to see the code for the server side of this, as the error you're reporting happens before you hit any of your code here (it's an error when the client tries to connect to the server - so the question is why is the server refusing the connection request...
– moilejter
Nov 18 '18 at 21:20