Krokcy 14 Newbie Poster

EDIT: Im an idiot. The hand-shake like mechanic i put in where the server waits for the client to respond was the reason it was so slow, removed it and it worked. I misunderstood/forgot how streams work..

Ok. So i just started learning how to use the network utility there is in java.
Basically i have made a program that can transfer a file. The problem is, i'm transferring one byte at a time. So to send a text file that is 22kb in size i send 22 000 individual bytes which, kind of obviously takes along time. Just tried sending a picture, while it worked it took like 20min, and i had arrays that were 150k+ in size (one for the sender and one for the reciver).

I don't know how to optimize it. My first thought were to put it into smaller arrays and send them one by one, by that still have the same problem. Besides it doesn't seem straight forward and if i didn't know if it were goning to help i didn't want to waste my time on it.

To start with just a short bit of code showing how i put the file into a byte array, if that can be done wrong?:

    byte[] fileArray;

    JFileChooser chooser = new JFileChooser();
    chooser.showDialog(this, "Send");

    File file = chooser.getSelectedFile();

    Path path = Paths.get(file.getAbsolutePath());
    try {
            fileArray = Files.readAllBytes(path);
        } catch (IOException e1) {
            fileArray = null;

Here is the code i use for sending the data:

    listenSocket = new ServerSocket(port);
    text.append("\n"+"Listening on port: " + port);

    //listen for, and accept connection
    connection = listenSocket.accept();
    connected = true;
    text.append("\n"+"Server connection established");

    //create input stream from client
    inStream = connection.getInputStream();
    inDataStream = new DataInputStream(inStream);

    //create output stream from client
    outStream = connection.getOutputStream();
    outDataStream = new DataOutputStream(outStream);

    //wait for input from client
    client = inDataStream.readUTF();
    text.append("\n"+"Address of client: " + client);

    //init array
    int length = fileArray.length;
    text.append("Length of array: " + Integer.valueOf(length));

     //send data      
    int toTest = 0;
    for(int i = 0; i<fileArray.length; i++) {
        while(toTest!=2) { // to make sure client is ready!
            toTest = inDataStream.readInt();
            text.append("\nPackets recived "+ i);
    text.append("send all data" + "\n");

And here is the code i use to recive the data, im going to leave out the bit where i open the streams since the two are similiar! :

 try {
 //init fileArray
        int length = stream.readInt();
        text.append("\nArray length"+String.valueOf(length));
        fileArray = new byte[length];
 } catch (IOException e) {
        text.append("reception of array length failed");

 try {
     //recive data
        for(int i = 0; i<fileArray.length; i++) {
            fileArray[i] = stream.readByte();
 }catch(IOException e) {


Any input is appreciated, know its quite a bit of code! And again quite a newbie havn't worked with this utility for very long!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.