InputStream interference in a Two-way file transfer through sockets - java

After editing my code with y'all suggestions, as well as condensing the code to where i can pinpoint the line of code that is causing the problem.
Server code:`
public class Server2 {
public static void main(String args[]) throws Exception {
ServerSocket servsock = null;
Socket sock = null;
int SOCKET_PORT = 12362;
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_SEND = "SFileToBeSent.txt";
String FILE_TO_RECEIVED = "CFileReceived.txt";
int FILE_SIZE = 6022386;
try {
System.out.println("Server created.");
servsock = new ServerSocket(SOCKET_PORT); // creates servsock with socket port
while (true) {
System.out.println("Waiting for connection...");
try {
sock = servsock.accept(); // accepts a socket connection
System.out.println("Accepted connection : " + sock);
System.out.println();
os = sock.getOutputStream(); // sets Output Stream using the sockets output stream
is = sock.getInputStream(); // get input stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
// if any streams or sockets are not null, the close them
if (os != null)
os.close();
if (is != null)
is.close();
if (sock != null)
sock.close();
} // end of finally
} // end of while
} // end of try
finally {
if (servsock != null)
servsock.close();
} // end of finally
}
}
public class Client2 {
public static void main(String args[]) throws Exception {
Socket sock = null; // used in main
String SERVER = "localhost"; // local host
int SOCKET_PORT = 12362; // you may change this
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_RECEIVED = "SFileReceived.txt";
String FILE_TO_SEND = "CFileToBeSent.txt";
int FILE_SIZE = 6022386;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");
System.out.println();
// get input and output from socket
is = sock.getInputStream(); // get input stream from socket
os = sock.getOutputStream(); // get output stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
if (sock != null)
sock.close();
if (os != null)
os.close();
if (is != null)
is.close();
} // end of finally
}
}
Server output:--------------------
Server created.
Waiting for connection...
Accepted connection : Socket[addr=/127.0.0.1,port=51565,localport=12362]
Sending SFileToBeSent.txt(32 bytes)
byte array from file to send:[B#4e25154f
sendFile complete
Bytes available from received file: 0
byte array from file to receive: [B#70dea4e
Client output--------------------
Connecting...
Bytes available from received file: 32
byte array from file to receive: [B#4e25154f
amount of bytes that was read for while: 32
bos.write
current += bytesRead
I still have the issue with InputStream, and i have found out through debugging that both the server and client get stuck on the while() statement that is in the receive section. For the server, it stops immediately, whereas the client goes through the while loop once then stops when it hits the while statement.
If anyone has any suggestions or solutions, then it would be much appreciated!

First , your receiveFile method should read input and write output in a loop.
Second, please close files after done or you may have a resource leak.
public static void receiveFile() throws IOException {
byte[] mybytearray = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
while ((bytesRead = is.read(mybytearray)) != -1) {
bos.write(mybytearray, 0, bytesRead );
current += bytesRead;
}
bos.flush(); // clears the buffer
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
bos.close();
}
}

Related

creating Socket to connect to windows 10 machine from Raspberry pi 4 Raspbian does not work (Java)

I have a Raspberry pi 4 with Raspbian installed, and I have a computer with Windows 10 installed.I wrote two functions one send a file and the other one receive the file.
when I run this function that sends a file on the raspberry pi 4:
public static void sendFile(String fileName, String ip)
{
BufferedOutputStream outputStream = null;
PrintWriter writer = null;
BufferedReader reader = null;
FileInputStream filein = null;
File file = new File(fileName);
if (!file.exists())
{
System.out.println(fileName + " does not exist");
return;
}
try
{
Socket socket = new Socket(ip, port);
outputStream = new BufferedOutputStream(socket.getOutputStream());
writer = new PrintWriter(socket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
filein = new FileInputStream(file);
long fileSize = file.length();
writer.println(fileName); // sending file name
writer.println(fileSize); // sending file size in bytes
writer.flush();
byte[] dataBuffer = new byte[1024];
int numberOfReadBytes = 0; // the number of read bytes for each read() function call
System.out.println("Entering the loop");
for(long i = 0; i < fileSize && numberOfReadBytes > -1;)
{
numberOfReadBytes = filein.read(dataBuffer); // read read() function returns the number of bytes tha has been assigned to the array or -1 if EOF(end of file) is reached
outputStream.write(dataBuffer, 0, numberOfReadBytes); // writing the bytes in dataBuffer from index 0 to index numberOfBytes
i += numberOfReadBytes;
}
outputStream.flush();
System.out.println(fileName + " sent to " + ip);
String status = reader.readLine();
System.out.println("Status: " + status + "\t file save successfully on the other machine.");
}
catch(IOException ioe)
{
System.err.println("Status: 0\n" + ioe.getMessage());
}
finally // closing streams
{
try
{
outputStream.close();
reader.close();
writer.close();
filein.close();
}
catch (IOException ioe)
{
System.err.println("Error closing the connection.");
}
}
}
it stops at this line Socket socket = new Socket(ip, port);
and this is the other function that runs on windows 10
public static void receiveFile()
{
// 1- read the file name
// 2- read the size of the file
// 3- read the file and write it
ServerSocket server = null;
Socket socket = null;
BufferedReader reader = null;
BufferedInputStream inputStream = null;
FileOutputStream fileout = null;
PrintWriter writer = null;
try
{
server = new ServerSocket(9999);
socket = server.accept();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inputStream = new BufferedInputStream(socket.getInputStream());
writer = new PrintWriter(socket.getOutputStream());
String fileName = reader.readLine(); // reading file name
long fileSize = Long.parseLong(reader.readLine()); // reading file size
System.out.println(fileSize);
// reading file data and write the data
File file = new File(fileName);
fileout = new FileOutputStream(file);
for (long i = 0; i < fileSize; ++i)
{
fileout.write(inputStream.read());
System.out.println(i);
}
fileout.flush();
fileout.close();
writer.println('1');
System.out.println("Status: 1");
System.out.println(fileName+ " is saved successfully");
}
catch (IOException ioe)
{
System.err.println("Status: 0");
System.err.println(ioe.getMessage());
}
finally
{
try
{
reader.close();
inputStream.close();
}
catch(IOException ioe)
{
System.err.println("Error closing connection\n" + ioe.getMessage());
}
}
}
I think windows 10 firewall blocks connection, but I am not sure.
It turns out it was the firewall blocking connection from the raspberry pi

java: send large files through sockets

So im making a client-server based architecture programm where server is sending a file and client receives it.
I have seen a lot of code parts and i also made a lot where i send small files. ex images. In my case i want to send .wav audio files which are large(40mb). This is what i have done so far. When i run my client, it just proceed to download, but never really downloads the file. I guess its because its too large.
How to send such large files?
server
public void send_file_to_client(String requested_file) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
File FILE_TO_SEND = new File("C:\\ServerMusicStorage\\" + requested_file + ".wav");
byte[] mybytearray = new byte[(int) FILE_TO_SEND.length()];
try {
fis = new FileInputStream(FILE_TO_SEND);
bis = new BufferedInputStream(fis);
} catch (FileNotFoundException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
OutputStream os = null;
bis.read(mybytearray, 0, mybytearray.length);
os = connsock.getOutputStream();
System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
toClient.writeUTF(Integer.toString(mybytearray.length));
os.write(mybytearray, 0, mybytearray.length);
os.flush();
System.out.println("Done.");
if (bis != null) {
bis.close();
}
if (os != null) {
os.close();
}
if (connsock != null) {
connsock.close();
}
}
client
public static void receive_file(String requested_file) throws IOException {
File file_to_save = new File("C:\\ClientMusicStorage\\" + requested_file + ".wav");
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
//get file size to create the bytearray
String fileSize=fromServer.readUTF();
int final_file_size = Integer.parseInt(fileSize);
byte[] mybytearray = new byte[final_file_size];
InputStream is = clientSocket.getInputStream();
fos = new FileOutputStream(file_to_save);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
bytesRead
= is.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
bos.write(mybytearray, 0, current);
bos.flush();
}

Send file via socket - buffer size [duplicate]

This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 5 years ago.
TL;DR: How do I send (with a single connection) file, its size and its name. All examples in the internet send a file alone.
Server:
public class Server {
private static int PORT = 6667;
private ServerSocket serverSocket;
public void run() throws IOException {
System.out.println("Opening server");
serverSocket = new ServerSocket(PORT);
while(true) {
try(Socket incomingSocket = serverSocket.accept()) {
System.out.println("Accepted connection: " + incomingSocket);
incomingSocket.setSoTimeout(2000); // Don't let scanner block the thread.
InputStream inputStream = incomingSocket.getInputStream();
Scanner scanner = new Scanner(inputStream);
String command = "";
if(scanner.hasNextLine())
command = scanner.nextLine();
if(command.equals("update")) {
File file = new File("abc.txt");
sendFile(incomingSocket, file);
}
else {
// ...
System.out.println("Another command");
}
}
}
}
private void sendFile(Socket socket, File file) throws IOException {
byte[] bytes = new byte[(int)file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(bytes, 0, bytes.length);
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
writer.println(file.length());
writer.println(file.getName());
System.out.println("Sending " + file.getName() + "(" + bytes.length + " bytes) to " + socket);
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
System.out.println("File sent");
}
public void stopRunning() {
try {
serverSocket.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Client:
public class Client {
private static String HOST = "localhost";
private static int PORT = 6667;
public void run() throws IOException {
Socket socket = new Socket(HOST, PORT);
System.out.println("Connecting...");
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
writer.println("update"); // Example command which will determine what server sends back
receiveFile(socket);
socket.close();
}
private void receiveFile(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
int size = 16384;
String name = "example.txt";
Scanner scanner = new Scanner(inputStream);
size = Integer.parseInt(scanner.next());
name = scanner.next();
FileOutputStream fileOutputStream = new FileOutputStream(name);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[size];
int bytesRead, totalRead = 0;
while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) {
totalRead += bytesRead;
bufferedOutputStream.write(buffer, 0, bytesRead);
}
bufferedOutputStream.flush();
System.out.println("File " + name + " received. " + totalRead + " bytes read");
bufferedOutputStream.close();
fileOutputStream.close();
}
I want my server to send a file to the client. It should also include the file's name and its size. Name because it's quite important and the size because I don't want to make a hardcoded buffer with a huge size.
Tried it with the code above. The client's "scanner part"
Scanner scanner = new Scanner(inputStream);
size = Integer.parseInt(scanner.next());
name = scanner.next();
works just okay, but the file is not received. inputStream.read(buffer, 0, buffer.length) never reads the remaining bytes from the stream.
If i comment out the scanner part, the bytes are read correctly(size and name information + file itself)
So, the question is, how do I send it with a single connection? Or should I make 2 separate connections, in the first one asking for size and file name and sending the file in the second one?
Scanner is good for text-based work.
One way to do what you want is using DataInputStream and DataOutputStream. Only one connection is needed:
public void send(File file, OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
// writing name
dos.writeUTF(file.getName());
// writing length
dos.writeLong(file.length());
// writing file content
... your write loop, write to dos
dos.flush();
}
public void receive(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
String fileName = dis.readUTF();
long fileSize = dis.readLong();
// reading file content
... your read loop, read from dis
}

Transferring Files via TCP/IP works in Windows but not Ubuntu

I was trying my hands at sending multiple files using TCP/IP connection with the help of this guide.
When I execute my testClient codes from Ubuntu 14.04, files are transferred over to testServer in Windows 7. The .txt files received here are in correct format.
However, when I execute testClients codes from Windows 7 and testServer from Ubuntu 14.04, the files received in Ubuntu 14.04 was messed up. (The contents from txt#2 contents spill over to txt#1.)
During the swap, none of the codes in both testServer and testClients were changed other than their IP address and file location.
I am confused. Why did the codes work fine in Windows 7 but not in Ubuntu? Is there something wrong with my codes? I would appreciate any help on this.
TestServer.java
public static void main(String[] args) throws IOException {
FileOutputStream fos;
BufferedOutputStream bos;
OutputStream output;
DataOutputStream dos;
int len;
int smblen;
InputStream in;
boolean flag = true;
DataInputStream clientData;
BufferedInputStream clientBuff;
ServerSocket serverSocket = new ServerSocket(5991);
serverSocket.setSoTimeout(500000);
System.out.println("Waiting for client on port " + serverSocket.getLocalPort());
Socket clientSocket = null;
clientSocket = serverSocket.accept();
System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress());
while (true){
while(flag==true) {
in = clientSocket.getInputStream();
clientData = new DataInputStream(in);
clientBuff = new BufferedInputStream(in);
int fileSize = clientData.read();
if (fileSize != 0)
System.out.println("Receiving " + fileSize + " files.\n");
//Store filenames and file sizes from client directory
ArrayList<File>files=new ArrayList<File>(fileSize);
ArrayList<Integer>sizes = new ArrayList<Integer>(fileSize);
//Server accepts filenames
for (int count=0; count<fileSize; count ++){
File ff=new File(clientData.readUTF());
files.add(ff);
}
for (int count=0; count<fileSize; count ++){
sizes.add(clientData.readInt());
}
for (int count=0; count<fileSize; count++) {
if (fileSize - count == 1) {
flag = false;
}
len = sizes.get(count);
output = new FileOutputStream("/home/pp/Desktop/inResources/" + files.get(count));
dos = new DataOutputStream(output);
bos = new BufferedOutputStream(output);
byte[] buffer = new byte[1024];
bos.write(buffer, 0, buffer.length);
while (len > 0 && (smblen = clientData.read(buffer)) > 0) {
dos.write(buffer, 0, smblen);
len = len - smblen;
dos.flush();
}
dos.close();
System.out.println("File " + files.get(count) + " with " + sizes.get(count) + " bytes recieved.");
}
}
if (flag == false) {
System.out.println("\nTransfer completed. Closing socket...");
serverSocket.close();
break;
}
}
}
TestClient.java
public static void main(String[] args) throws IOException {
String serverName = "192.168.1.12"; //IP address
int port = 5991;
Socket sock = new Socket(serverName, port);
System.out.println("Connected to " + serverName + " on port " + port + "\n");
File myFile = new File("C:\\Users\\inter2\\Desktop\\noobs\\outResources");
File[] files = myFile.listFiles();
OutputStream os = sock.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
//Sending total number of files in folder
dos.writeInt(files.length);
//Sending file names
for (int count=0; count<files.length; count++) {
dos.writeUTF(files[count].getName());
}
//Sending file sizes
for (int count=0; count<files.length; count++) {
int filesize = (int) files[count].length();
dos.writeInt(filesize);
}
//Sending of files
for (int count=0; count<files.length; count ++) {
int filesize = (int) files[count].length();
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(files[count].toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length);
dos.write(buffer, 0, buffer.length);
dos.flush();
System.out.println("Sending file " + files[count].getName() + " with " + filesize + " bytes.");
}
System.out.println("\n" + files.length + " files successfully transfered.");
sock.close();
}
This code never worked.
int fileSize = clientData.read();
Here you are reading the amount of files, as a byte.
dos.writeInt(files.length)
Here you are writing that amount, as an int. So already your writer is three bytes ahead of your reader.
Change read() to readInt() above.
Other notes:
Receiving:
byte[] buffer = new byte[1024];
This is OK but a bigger buffer would be more efficient, say 8192.
bos.write(buffer, 0, buffer.length);
This is a bug. Remove it. You are writing 1024 null bytes at the beginning of the file.
while (len > 0 && (smblen = clientData.read(buffer)) > 0) {
dos.write(buffer, 0, smblen);
len = len - smblen;
dos.flush();
Don't flush inside loops.
}
Sending:
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(files[count].toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length);
dos.write(buffer, 0, buffer.length);
No need to waste a buffer of the size of the file. Use the same code you use for receiving above.

Java Socket Programming Buffer for Large File

I am trying to transfer a large file from a server to a client. My code so far works but only if I set the buffer size in the client code to the exact size of the file. I won't always know what the file size is going to be. I keep finding examples that claim it doesn't matter what the size of the file or buffer is because it will just keep reading from the input stream...? However, when I implement the code that supposedly does this, it transfers 0 bytes.
Client:
public static void main (String [] args ) throws IOException {
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket sock = null;
try {
sock = new Socket(hostname, 20000);
System.out.println("Connecting...");
// receive file
InputStream is = sock.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVE);
bos = new BufferedOutputStream(fos);
//////////// replaced this ////////////////////////////
byte[] buffer = new byte[BUFFER_SIZE];
bytesRead = is.read(buffer,0,buffer.length);
current = bytesRead;
do {
bytesRead = is.read(buffer, current, (buffer.length-current));
if(bytesRead >= 0){ current += bytesRead;}
} while(bytesRead > 0);
bos.write(buffer, 0 , current);
///////////////////////////////////////
// with this:
// int count;
// byte[] buffer = new byte[8192];
// while ((count = is.read(buffer)) > 0)
// {
// bos.write(buffer, 0, count);
// }
//////////// transfers 0 bytes ///////////
bos.flush();
System.out.println("File " + FILE_TO_RECEIVE
+ " downloaded (" + current + " bytes read)");
}
finally {
if (fos != null){ fos.close();}
if (bos != null){ bos.close();}
if (sock != null){ sock.close();}
}
}
Server:
public static void main(String[] args) throws IOException{
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
ServerSocket servsock = null;
Socket sock = null;
try {
servsock = new ServerSocket(20000);
while (true) {
System.out.println("Waiting...");
try {
sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// send file
File myFile = new File (FILE_TO_SEND);
byte [] mybytearray = new byte [(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = sock.getOutputStream();
System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
os.write(mybytearray,0,mybytearray.length);
os.flush();
System.out.println("Done.");
}
finally {
if (bis != null){ bis.close();}
if (os != null){ os.close();}
if (sock!=null){ sock.close();}
}
}
}
finally {
if (servsock != null){
servsock.close();
}
}
}
Thank you for your help
Your copy loops are both different, and both nonsense. One of them isn't even a loop. Try this:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
This works for any buffer of size >= one byte.
Use this at both ends.

Categories

Resources