Java sending and receiving file over sockets

A.D

I am currently trying to to create a file transfer program which could transfer files from one location to another. The program works for .txt files, but for other extensions, such as .exe, the transferred files don't open properly.Can anyone spot the problem with my code? Thanks!

Server code:

import java.io.*;
import java.net.*;

public class SendFile{
    static ServerSocket receiver = null;
    static OutputStream out = null;
    static Socket socket = null;
    static File myFile = new File("C:\\Users\\hieptq\\Desktop\\AtomSetup.exe");
    /*static int count;*/
    static byte[] buffer = new byte[(int) myFile.length()];
    public static void main(String[] args) throws IOException{
        receiver = new ServerSocket(9099);
        socket = receiver.accept();
        System.out.println("Accepted connection from : " + socket);
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream in = new BufferedInputStream(fis);
        in.read(buffer,0,buffer.length);
        out = socket.getOutputStream();
        System.out.println("Sending files");
        out.write(buffer,0, buffer.length);
        out.flush();
        /*while ((count = in.read(buffer)) > 0){
            out.write(buffer,0,count);
            out.flush();
        }*/
        out.close();
        in.close();
        socket.close();
        System.out.println("Finished sending");



    }

}

Client code:

import java.io.*;
import java.net.*;

public class ReceiveFile{
    static Socket socket = null;
    static int maxsize = 999999999;
    static int byteread;
    static int current = 0;
    public static void main(String[] args) throws FileNotFoundException, IOException{
        byte[] buffer = new byte[maxsize];
        Socket socket = new Socket("localhost", 9099);
        InputStream is = socket.getInputStream();
        File test = new File("D:\\AtomSetup.exe");
        test.createNewFile();
        FileOutputStream fos = new FileOutputStream(test);
        BufferedOutputStream out = new BufferedOutputStream(fos);
        byteread = is.read(buffer, 0, buffer.length);
        current = byteread;

        do{
            byteread = is.read(buffer, 0, buffer.length - current);
            if (byteread >= 0) current += byteread;
        } while (byteread > -1);
        out.write(buffer, 0, current);
        out.flush();

        socket.close();
        fos.close();
        is.close();

    }
}
Sanjeev

One problem is you are overwriting the content of your buffer while reading from InputStream

    byteread = is.read(buffer, 0, buffer.length);
    current = byteread;

    do{
        byteread = is.read(buffer, 0, buffer.length - current);
        if (byteread >= 0) current += byteread;
    } while (byteread > -1);

InputStream#read states second param as offset where it will be stored in byte array and in your case offset is always 0, so it will overwrite in each iteration.

I would suggest to simplify your logic of reading from InputStream and writing to OutputStream

byte[] buffer = new byte[16384];

while ((byteread = is.read(buffer, 0, buffer.length)) != -1) {
  out.write(buffer, 0, byteread);
}

out.flush();

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java sockets not sending/receiving messages

From Dev

Sending and receiving with sockets

From Dev

Sending a file over TCP sockets in Python

From Dev

Sending a file with Java Sockets, losing data

From Dev

Sending file and receiving response using ICAP and Java

From Dev

Sending and receiving an image over sockets with C# | Screen Sharing Using C#

From Dev

Socket is not sending/receiving over multi-threaded java program

From Dev

Sending screenshot from Java to CPP over socket - Issue receiving image

From Dev

Error Receiving File over Socket Android/Java

From Dev

Error Receiving File over Socket Android/Java

From Dev

Sending ints over sockets in swift

From Dev

How sending and receiving works in Python sockets?

From Dev

Sending and receiving data through sockets and ports

From Dev

How sending and receiving works in Python sockets?

From Dev

C++ issue while sending and receiving a file using sockets within a Linux machine

From Dev

Sending & Receiving Java Objects

From Dev

Sending & Receiving Java Objects

From Dev

Sending file content over HTTP using Java

From Dev

Sending binary data over sockets with Python

From Dev

Properly sending HTTP response over sockets

From Dev

Properly sending HTTP response over sockets

From Dev

Sending data over sockets is slower with concurrent threads?

From Dev

Java file transfer over Sockets trim last bytes

From Dev

Sending and receiving data from an app over the internet

From Dev

Sending objects via java sockets

From Dev

Sending and receiving multiple JSON literals via PHP sockets

From Dev

Python transmitting a file over sockets

From Dev

Receiving python json through sockets with java

From Dev

JAVA - receiving objects using sockets and threads not working

Related Related

  1. 1

    Java sockets not sending/receiving messages

  2. 2

    Sending and receiving with sockets

  3. 3

    Sending a file over TCP sockets in Python

  4. 4

    Sending a file with Java Sockets, losing data

  5. 5

    Sending file and receiving response using ICAP and Java

  6. 6

    Sending and receiving an image over sockets with C# | Screen Sharing Using C#

  7. 7

    Socket is not sending/receiving over multi-threaded java program

  8. 8

    Sending screenshot from Java to CPP over socket - Issue receiving image

  9. 9

    Error Receiving File over Socket Android/Java

  10. 10

    Error Receiving File over Socket Android/Java

  11. 11

    Sending ints over sockets in swift

  12. 12

    How sending and receiving works in Python sockets?

  13. 13

    Sending and receiving data through sockets and ports

  14. 14

    How sending and receiving works in Python sockets?

  15. 15

    C++ issue while sending and receiving a file using sockets within a Linux machine

  16. 16

    Sending & Receiving Java Objects

  17. 17

    Sending & Receiving Java Objects

  18. 18

    Sending file content over HTTP using Java

  19. 19

    Sending binary data over sockets with Python

  20. 20

    Properly sending HTTP response over sockets

  21. 21

    Properly sending HTTP response over sockets

  22. 22

    Sending data over sockets is slower with concurrent threads?

  23. 23

    Java file transfer over Sockets trim last bytes

  24. 24

    Sending and receiving data from an app over the internet

  25. 25

    Sending objects via java sockets

  26. 26

    Sending and receiving multiple JSON literals via PHP sockets

  27. 27

    Python transmitting a file over sockets

  28. 28

    Receiving python json through sockets with java

  29. 29

    JAVA - receiving objects using sockets and threads not working

HotTag

Archive