I'm trying to send multiple files from client to server using socket but when I click upload button it adds only one file second
I'm trying to send multiple files from client to server using socket but when I click upload button it adds only one file second
Your
copyFile()
is not suitable for network transmissions.You need to get rid of the two
close()
calls inside ofcopyFile()
. On the client side,out.close()
is closing the socket after the 1st file has been sent. On the server side,InputStream.close()
is closing the socket after the 1st file has been received. It is the caller's responsibility to close the streams it passes tocopyFile()
, it is notcopyFile()
's responsibility.More importantly, for each file the client wants to send,
copyFile()
is not sending the file's byte count before sending the file's actual bytes, to indicate where each file ends and the next begins. So, on the server side,copyFile()
does not know when to stop reading from theinputStream
and will just keep reading endlessly until the connection is closed/broken.As-is,
copyFile()
may work for copying files from one folder to another on the local system, but it is not suitable for copying files over a TCP network.Try this instead:
Client side:
Server side: