src/SftpClient.java
private static void sendDataPacket(DatagramSocket socket, InetAddress dstAddress, byte[] data, byte seq)
            throws IOException {
        // Construct new data array to include sequence number
        byte[] dataWithSeq = new byte[data.length + 1];
        dataWithSeq[0] = seq;
        System.arraycopy(data, 0, dataWithSeq, 1, data.length);

        // Send this data
        DatagramPacket transmitFile = new DatagramPacket(dataWithSeq, dataWithSeq.length, dstAddress, SftpClient.SERVER_PORT);
        DatagramPacket receiveAck = new DatagramPacket(new byte[1], 1);

        for (int retries = RETRANSMISSION_LIMIT; retries >= 0; retries--) {
            try {
                // Send Data Packet
                socket.send(transmitFile);
                if (DEBUG) System.out.println("Packet " + (++packet_count) + " sent, size = " + data.length);

                // Receive Ack
                socket.receive(receiveAck);
                while (receiveAck.getData()[0] != seq) {
                    // Keep Receiving if Ack number is incorrect
                    socket.receive(receiveAck);
                }
                break;
            } catch (SocketTimeoutException socketTimeoutException) {
                if (DEBUG) System.out.println("    Packet " + (packet_count--) + " Timeout.");
                // Check Retry Count
                if (retries - 1 < 0) {
                    throw new ConnectException("packet retransmission limit reached");
                }
            }
        }
    }
/**
     * Send a data packet to designated address and port {@value SERVER_PORT} with current sequence number.
     * @param socket UDP socket which the program uses to send packets / transfer files
     * @param dstAddress Destination Server Address
     * @param data Data to be sent in the packet (without sequence number byte), max {@value MAX_DATA_SIZE_PER_PACKET} Bytes
     * @param seq Current sequence number (expect this sequence number to be ACKed by server)
     * @throws ConnectException When packet retransmission limit has reached (Retried for {@value RETRANSMISSION_LIMIT} times)
     * @throws IOException Receiving and sending packets in socket produces this Exception
     */
Send a data packet to designated address and port  SERVER_PORT with current sequence number.
Send a data packet to designated address and port SERVER_PORT with current sequence number .
private static void sendDataPacket(DatagramSocket socket, InetAddress dstAddress, byte[] data, byte seq)
            throws IOException {
        byte[] dataWithSeq = new byte[data.length + 1];
        dataWithSeq[0] = seq;
        System.arraycopy(data, 0, dataWithSeq, 1, data.length);
        DatagramPacket transmitFile = new DatagramPacket(dataWithSeq, dataWithSeq.length, dstAddress, SftpClient.SERVER_PORT);
        DatagramPacket receiveAck = new DatagramPacket(new byte[1], 1);
        for (int retries = RETRANSMISSION_LIMIT; retries >= 0; retries--) {
            try {
                socket.send(transmitFile);
                if (DEBUG) System.out.println("Packet " + (++packet_count) + " sent, size = " + data.length);
                socket.receive(receiveAck);
                while (receiveAck.getData()[0] != seq) {
                    socket.receive(receiveAck);
                }
                break;
            } catch (SocketTimeoutException socketTimeoutException) {
                if (DEBUG) System.out.println("    Packet " + (packet_count--) + " Timeout.");
                if (retries - 1 < 0) {
                    throw new ConnectException("packet retransmission limit reached");
                }
            }
        }
    }
private static void sendDataPacket ( DatagramSocket socket , InetAddress dstAddress , byte [ ] data , byte seq ) throws IOException { byte [ ] dataWithSeq = new byte [ data . length + 1 ] ; dataWithSeq [ 0 ] = seq ; System . arraycopy ( data , 0 , dataWithSeq , 1 , data . length ) ; DatagramPacket transmitFile = new DatagramPacket ( dataWithSeq , dataWithSeq . length , dstAddress , SftpClient . SERVER_PORT ) ; DatagramPacket receiveAck = new DatagramPacket ( new byte [ 1 ] , 1 ) ; for ( int retries = RETRANSMISSION_LIMIT ; retries >= 0 ; retries -- ) { try { socket . send ( transmitFile ) ; if ( DEBUG ) System . out . println ( "Packet " + ( ++ packet_count ) + " sent, size = " + data . length ) ; socket . receive ( receiveAck ) ; while ( receiveAck . getData ( ) [ 0 ] != seq ) { socket . receive ( receiveAck ) ; } break ; } catch ( SocketTimeoutException socketTimeoutException ) { if ( DEBUG ) System . out . println ( "    Packet " + ( packet_count -- ) + " Timeout." ) ; if ( retries - 1 < 0 ) { throw new ConnectException ( "packet retransmission limit reached" ) ; } } } }
Send a data packet to designated address and port {@value SERVER_PORT} with current sequence number.
Send a data packet to designated address and port { @value SERVER_PORT } with current sequence number .
// Construct new data array to include sequence number // Send this data // Send Data Packet // Receive Ack // Keep Receiving if Ack number is incorrect // Check Retry Count