My question is how do I read from the user Datainput.txt file to get the value of myVal?

I can handle all the other stuff required for this, but with out the correct value to start well nothing will be right.


public class MyClient {
private final int myServerPort = 0x28b5;
private final String myServerAddr = "127.0.0.1";

public static void main (String[] args) {
try {
new MyClient ().go ();
} catch (Exception e) {
System.err.println (e);
}
}

private void go ()
throws IOException, SocketException, UnknownHostException {
DatagramSocket dgSocket = new DatagramSocket ();
InetAddress inAddr = InetAddress.getByName (myServerAddr);
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
String message;

int seqi=0; //seqnumber
int myVal = ; // read number from file DataInput.txt

do { // read a number from user and send it to the server
byte byToIn[] = new byte[8];
// convert myVal into 4 bytes
// convert seqi into 4 bytes

seqi++;


DatagramPacket dgPacket = new DatagramPacket (byToIn, byToIn.length, inAddr, myServerPort);
dgSocket.send (dgPacket);
} while (seqi<32);

dgSocket.close ();
}
}

I also have the Server side, but did not think this was relevant. If needed I will post that also.

Maybe the instructions will help all of you understand what I refer to as the Input.txt file...

Socket programming is one of the “backbones” in modern distributed computing. In this assignment, you will use the UDP protocol, realized via the Java Datagram API to send 32 integers from a client to a server.

As you know, UDP does not guarantee in-order segment delivery or guarantee that a segment will make it to its destination. In this assignment, you will use a UDP connection on the localhost. In this scenario, it is very unlikely that packets will get lost. So, you can ignore the possibility of packet loss. You will only need to handle the possibility of packet reordering.

The client will send 32 integers to the server. These 32 integers will be read from a file called DataInput.txt that is the same directory as the source of the client. Your assignment is to implement this delivery using Java UDP datagrams. The server will receive packets from the client and track which packets were received from the client. The server will track the order of segment delivered and reconstruct the correct order of the 32 integers and output it to the terminal.

I was researching the BufferedReader stdin = new BufferedReader( inStream );

and the following code was how they handled the bufferedReader reading of data.


message = stdin.readLine();

myVal = Integer.parseInt(message); // convert inData to int

From here I believe I can manipulate the value of myVal.

I used the variables from above but the code is very similar. Will this work?

Do you happen to have a solution to this problem?

I am sure I solved this some point.

I would have to go home and check my solution.

Would you please send me a message with source code?
I really appreciate it. I have been working on this for hours now..

public class MyClient {
      private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)
      private final String myServerAddr = "127.0.0.1";
   
       public static void main (String[] args) {
         try {
            new MyClient ().go ();
         } 
             catch (Exception e) {
               System.err.println (e);
            } 
      }
   
       private void go () 
       throws IOException, SocketException, UnknownHostException {
         DatagramSocket dgSocket = new DatagramSocket ();
         InetAddress inAddr = InetAddress.getByName (myServerAddr);
         BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
         String message;
      
         int seqi=0;                 //seqnumber 
         message = stdin.readLine();
         int myVal = Integer.parseInt(message); // convert message to int
      
         do {    // read a number from user and send it to the server
            byte byToIn[] = new byte[8];
            int j = 0;
            int k = 4;
				byte temp[] = new byte[8];
         // convert myVal into 4 bytes
         // convert seqi into 4 bytes
				temp[seqi] = byToIn[seqi];
            byToIn[seqi] = (byte)(myVal >> j);
            temp[seqi] = (byte)(myVal >> k);
				
         
            j = j + 8;
            k = k + 8;  
            seqi++;
         
         
            DatagramPacket dgPacket = new DatagramPacket (byToIn, byToIn.length, inAddr, myServerPort);
            dgSocket.send (dgPacket);
         } while (seqi<32);
      
         dgSocket.close ();
      }
   }
public class MyServer {
      private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)
   
       public static void main (String[] args)  {
         try {
            new MyServer().go ();
         } 
             catch (Exception e) {
               System.err.println (e);
            } 
      }
   
   
       private void go ()
       throws IOException, SocketException
      {
         byte[] byToIn = new byte[8];
         DatagramSocket dgSocket = new DatagramSocket (myServerPort);
         String message;
         int[] data;              // store newVal
         data = new int[32];      
         int[] seq;               // store sequence numbers
         seq = new int[32];       
         int[] array;
         array = new int[64];	    // store final array as newVal/sequence number in a linear array
         int x = 0;		    //starting byte point for NewVal
         int y = 4;		    //starting byte point for seqi
         int i = 0;
         do {
            DatagramPacket dgPacket = new DatagramPacket (byToIn, byToIn.length);
            dgSocket.receive (dgPacket);
            int newVal = 0;
         // generate newVal from bytes 0..3 of byToIn
            newVal = (0xff & byToIn[i])<< x;
            data[i] = newVal;
            int seqi;				
         // generate seq. number from bytes 4..7 of byToIn
            seqi = (0xff & byToIn[i])<< y;  
            seq[i] = seqi;
            y = y + 8;
            x = x + 8;   
            i++;
         } while (i<32);
      
      // rearrange the numbers received using seqnumbers
      
         int a = 0;
         int b = 1;
         int val = data[seq[i]];	//value of newVal in data array at sequence location
         int j = 0;
         do {
            array[a] = val;			//store newVal 
            array[b] = array[seq[i]];	//store sequence number with out sorting.
            a=a+2;
            b=b+2;
            j++;
				System.out.println("Current Seuence Number is" + array[b] + ", Current value at that sequence number is " +  array[a]);
         }	while (j<64);
      
         dgSocket.close ();
      }
   }
Be a part of the DaniWeb community

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