Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Port Communication Protocol in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cosc635_p2;

import java.awt.*;
import static java.awt.Frame.MAXIMIZED_BOTH;
import java.io.*;
//import static java.lang.System.exit;
import java.net.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 *
 * @author obobotette0
 */
public class COSC635_P2 {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     * @throws java.net.UnknownHostException
     * @throws java.net.SocketException
     * @throws java.lang.InterruptedException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, SocketException, InterruptedException, IllegalArgumentException, UnknownHostException {
        // TODO code application logic here

        //create variables
        String inFile = new String();
        String netadd = "\\10.20.101.99";      
        inFile = "..\\COSC635_P2\\src\\cosc635_p2\\COSC635_P2_DataSent.txt";
        String outFile = new String();
        outFile = "..\\COSC635_P2\\src\\cosc635_p2\\COSC635_P2_DataReceived.txt";
        String outfile2 = new String();
        outfile2 = "..\\COSC635_P2\\src\\cosc635_p2\\COSC635_P2_DataRecieved0.txt";
        String seed = JOptionPane.showInputDialog("Enter a number between 0 and 99");
        int randnum = Integer.valueOf(seed);
        Random error = new Random(System.currentTimeMillis());
        int ploss = 0;
        double packets = 0;
        double window = 0;
        double start = 0;
        String data = new String();
        String ack = new String("Send More");
        InetAddress serv = InetAddress.getLocalHost();
        //System.out.println(serv.getHostAddress() + " " + serv.getHostName() + " " + serv.toString());
        //System.out.println("");
        InetAddress cl = InetAddress.getByName("");//getByName(netadd);
        SocketAddress cl1 = new InetSocketAddress("address", 02);

        //Create socket from arbitrary port and local IP
        DatagramSocket server = new DatagramSocket(01, serv);
        //System.out.println(server.getInetAddress() + " " + server.getLocalAddress());
        //System.out.println(server.getLocalSocketAddress() + " " + server.getRemoteSocketAddress());
        DatagramSocket client = new DatagramSocket(02, serv);

        //Connect server and client
        server.connect(client.getLocalSocketAddress());
        client.connect(server.getLocalSocketAddress());

        //Prompt user for protocol
        String protocol = JOptionPane.showInputDialog("Using SAW or GBN");
        protocol = protocol.toUpperCase();

        //Read text file
        File input = new File(inFile);
        Scanner read = new Scanner(input);

        //Prepare written file
        File dir = new File(outfile2);
        //dir.mkdir();
        FileWriter output = new FileWriter(outFile);
        PrintWriter write = new PrintWriter(output);

        //System.out.println(data.length+" "+ new String(data));
        if (Integer.valueOf(seed) >= 0 && Integer.valueOf(seed) <= 99) {
            if (protocol.matches("SAW")) {
                //Start transmission timer
                start = System.currentTimeMillis();
                //Read file line by line
                while (read.hasNextLine()) {
                    //check condition for packet loss
                    error = new Random(System.currentTimeMillis());
                    int percent = error.nextInt(99);
                    data = read.nextLine();
                    //Convert data to bytes for packet
                    DatagramPacket packet1 = new DatagramPacket(data.getBytes(), data.getBytes().length);
                    packets++;
                    if (percent >= randnum) {
                        //packets++;
                        server.send(packet1);
                    } else {
                        ploss++;
                        server.send(packet1);
                        packets++;
                    }
                    client.receive(packet1);
                    //write to file from packet
                    write.append(new String(packet1.getData()));
                    write.println();

                    //check condition for packet loss
                    error = new Random(System.currentTimeMillis());
                    percent = error.nextInt(99);
                    //Send acknowledgement
                    DatagramPacket packet2 = new DatagramPacket(ack.getBytes(), ack.getBytes().length);
                    //Count packets
                    packets++;

                    //if number is less than user inpur
                    //use division to produce an error rate
                    if (percent >= randnum) {

                        client.send(packet2);
                    } else {
                        ploss++;
                        client.send(packet2);
                        packets++;
                    }
                    server.receive(packet2);

                }
                //Stop timer, close file and print results              
                double stop = System.currentTimeMillis();
                read.close();
                write.println();
                write.println("Total transmission time was " + (stop - start) + " milliseconds.");
                write.println(ploss + " packets lost of " + packets + " packets sent for " + (ploss / packets) * 100 + "% of the packets loss during transmission.");
                write.flush();
                JFrame show = new JFrame();
                String newline = new String(" \t\n ");
                String time = new String("Total transmission time was " + (stop - start) + " milliseconds.");
                String stat = new String(ploss + " packets lost of " + packets + " packets sent for " + (ploss / packets) * 100 + "% of the packets loss during transmission.");
                JLabel print = new JLabel(time + newline + stat);
                show.setTitle("Reporting");
                show.add(print);
                show.validate();
                show.setSize(500, 500);
                show.setLocationRelativeTo(null);
                show.pack();
                show.setVisible(true);
                show.setDefaultCloseOperation(EXIT_ON_CLOSE);
            } else if (protocol.matches("GBN")) {
                String goback = JOptionPane.showInputDialog("How many frames will be sent at a time?");
                int loop = Integer.valueOf(goback);
                String data1[] = new String[loop];
                DatagramPacket packet1[] = new DatagramPacket[loop];
                start = System.currentTimeMillis();

                do {//read file by iterations entered by user
                    data = read.nextLine();
                    //System.out.println(data);
                    for (int i = 0; i < loop; i++) {
                        if (i == 0) {
                            //check condition for packet loss
                            error = new Random(System.currentTimeMillis());
                            int percent = error.nextInt(99);

                            data1[i] = data;
                            //Convert line to bytes for packet
                            packet1[i] = new DatagramPacket(data1[i].getBytes(), data1[i].getBytes().length);

                            //count packets
                            packets++;

                            //if number is less than user inpur
                            //use division to produce an error rate
                            if (percent >= randnum) {

                                server.send(packet1[i]);
                                client.receive(packet1[i]);
                            } else {
                                //read.reset();
                                i = -1;
                                ploss++;
                                packets++;

                            }
                        } else if (i > 0 && read.hasNextLine()) {
                            //check condition for packet loss
                            error = new Random(System.currentTimeMillis());
                            data1[i] = read.nextLine();
                            //Convert line to bytes for packet
                            packet1[i] = new DatagramPacket(data1[i].getBytes(), data1[i].getBytes().length);
                            int percent = error.nextInt(99);

                            //count packets
                            packets++;

                            //if number is less than user inpur
                            //use division to produce an error rate
                            if (percent >= randnum) {

                                server.send(packet1[i]);
                                client.receive(packet1[i]);
                            } else {
                                i = -1;
                                ploss++;
                                packets++;

                            }

                        }

                    }
                    for (int j = 0; j < loop; j++) {
                        //write to file from packet
                        write.append(new String (packet1[j].getData()));
                        write.println();

                    }
                    error = new Random(System.currentTimeMillis());
                    int percent = error.nextInt(99);
                    DatagramPacket packet2 = new DatagramPacket(ack.getBytes(), ack.getBytes().length);
                    packets++;
                    if (percent >= randnum) {

                        client.send(packet2);
                        server.receive(packet2);
                    } else {
                        ploss++;
                        client.send(packet2);
                        server.receive(packet2);
                        packets++;
                    }

                    //Count frames
                    window++;
                } while (read.hasNextLine());
                //Stop timer, close file and print results
                double stop = System.currentTimeMillis();
                read.close();
                write.println();
                write.println("Total transmission time was " + (stop - start) + " milliseconds for " + window + " frames.");
                write.println(ploss + " packets lost of " + packets + " packets sent for " + (ploss / packets) * 100 + "% of the packets loss during transmission.");
                write.flush();
                JFrame show = new JFrame();
                String newline = new String(" \t\n ");
                String time = new String("Total transmission time was " + (stop - start) + " milliseconds for " + window + " frames.");
                String stat = new String(ploss + " packets lost of " + packets + " packets sent for " + (ploss / packets) * 100 + "% of the packets loss during transmission.");
                JLabel print = new JLabel(time + newline + stat);
                show.setTitle("Reporting");
                show.add(print);
                show.validate();
                show.setSize(500, 500);
                show.setLocationRelativeTo(null);
                show.pack();
                show.setVisible(true);
                show.setDefaultCloseOperation(EXIT_ON_CLOSE);
            } else {
                System.out.println("Error from User Input");
                System.exit(1);
            }

        } else {
            System.out.println("Error from User Input");
            System.exit(1);
        }

    }
}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!