Results 1 to 9 of 9
  1. #1
    goagray is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Talking Need help passing a value for Sockets

    Hi there,
    I am having a problem passing an int value from one class to the other. The gist of the objective of this program is to compute a local sum at each client, and then once the local sums are computed.. to compute the overall sum from all the clients. My problem is that I'm having trouble figuring out how to pass each sum from each individual client in the class multicastSenderReceiver to the readThread class. I keep getting an error. Can anyone suggest the next step??
    I feel that I have to be able to pass the sum value from each client generated in multicastSenderReceiver to the other class, because that is the only way I'll accurately be able to sum up all the values.. Am I wrong? Any help is appreciated.
    Thanks!
    Java Code:
    package multicastsenderreceiver;
    
    import java.net.*;
    import java.io.*;
    import java.util.*;
    //import static multicastsenderreceiver.multicastSenderReceiver.numProcesses;
    
    class multicastSenderReceiver{
    
      static int numbers[];
      static int numProcesses;
      public static int sum;
    
      public static void main(String[] args){
    
       try{
             
         numProcesses = Integer.parseInt(args[0]);
    
         int processID = Integer.parseInt(args[1]);
    
         int arraySize = Integer.parseInt(args[2]);
    
         numbers = new int[arraySize];
    
         String filename = args[3];
         
         FileReader fr = new FileReader(filename);
         BufferedReader br = new BufferedReader(fr);
         
         for (int i = 0; i < arraySize; i++){
    
    	 numbers[i] = Integer.parseInt(br.readLine() );
         }
    
          for(int j = 0; j < numbers.length; j++){
              sum+=numbers[j];
          }
            
          // sum = multicastMessage.getBytes();
          int multicastPort = 9068;
          InetAddress group = InetAddress.getByName("225.4.5.6"); 
          MulticastSocket sendSocket = new MulticastSocket( );
          //launch the read thread
          readThread rt = new readThread(group, multicastPort);
          rt.start();
          System.out.println("Local ID: " + processID);
          System.out.println("The local sum is " + sum);
          String multicastMessage = "Message from Process#" + processID + ":" + 
                  "Local sum is " + sum;
          byte[] msg = multicastMessage.getBytes();
          //int vsum = sum;
          
          DatagramPacket packet = new DatagramPacket(msg, msg.length, group, 
                  multicastPort);
    	// wait for the read threads of all the processes to start, before sending the partial sum value
          System.out.print("Hit return to send message\n\n"); 
          BufferedReader ir = new BufferedReader(new InputStreamReader(System.in)); 
          ir.readLine( ); 
     
          sendSocket.send(packet);
          // send <key, value> as a string where key is the process ID and value is the partial sum value
    
      
    
    
        }
       
       catch(Exception e){e.printStackTrace();}
    
    }
      
    }
    class readThread extends Thread{
        InetAddress group;
        int multicastPort;
        int MAX_MSG_LEN = 1000;
        //multicastSenderReceiver vsum = new multicastSenderReceiver();
        //int vsum;
        readThread(InetAddress g, int port){
            group = g;
            multicastPort = port;
            
        }
        
        public void run(){
            try{
                MulticastSocket readSocket = new MulticastSocket(multicastPort);
                readSocket.joinGroup(group);
                while(true){
                    byte[] message = new byte[MAX_MSG_LEN];
                    DatagramPacket packet = new DatagramPacket(message,
                            message.length,group, multicastPort);
                    
                    readSocket.receive(packet);
                    String msg = new String(packet.getData());
                    System.out.println(msg);
                }
            
            }
            catch(Exception e){e.printStackTrace();}
        }
    }

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Need help passing a value for Sockets

    Please provide details of the error - error message, stack trace (if exists), etc.

  3. #3
    goagray is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: Need help passing a value for Sockets

    My apologies. I see that I have posted the code without my attempted fix. I am trying to add the int sum to my Datagram packet that is to be sent from my multicastSenderReceiver class to my readThread Class in this line of code here:
    Java Code:
    DatagramPacket packet = new DatagramPacket(message,
                            message.length,group, multicastPort, sum);
    I have both of these lines in each class, but I get the error:
    Java Code:
    no suitable constructor found for DatagramPacket(byte[],int,InetAddress,int,int)
        constructor DatagramPacket.DatagramPacket(byte[],int,SocketAddress) is not applicable
          (actual and formal argument lists differ in length)
        constructor DatagramPacket.DatagramPacket(byte[],int,InetAddress,int) is not applicable
          (actual and formal argument lists differ in length)
        constructor DatagramPacket.DatagramPacket(byte[],int,int,SocketAddress) is not applicable
          (actual and formal argument lists differ in length)
        constructor DatagramPacket.DatagramPacket(byte[],int,int,InetAddress,int) is not applicable
          (actual argument InetAddress cannot be converted to int by method invocation conversion)
        constructor DatagramPacket.DatagramPacket(byte[],int) is not applicable
          (actual and formal argument lists differ in length)
        constructor DatagramPacket.DatagramPacket(byte[],int,int) is not applicable
          (actual and formal argument lists differ in length)
    Below is my full code, somewhere I'm missing a simple step for this constructor to work.. I can't find it. What am I overlooking?
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    //import static multicastsenderreceiver.multicastSenderReceiver.numProcesses;
    
    class multicastSenderReceiver{
    
      static int numbers[];
      static int numProcesses;
      public static int sum;
    
      public static void main(String[] args){
    
       try{
             
         numProcesses = Integer.parseInt(args[0]);
    
         int processID = Integer.parseInt(args[1]);
    
         int arraySize = Integer.parseInt(args[2]);
    
         numbers = new int[arraySize];
    
         String filename = args[3];
         
         FileReader fr = new FileReader(filename);
         BufferedReader br = new BufferedReader(fr);
         
         for (int i = 0; i < arraySize; i++){
    
    	 numbers[i] = Integer.parseInt(br.readLine() );
         }
    
          for(int j = 0; j < numbers.length; j++){
              sum+=numbers[j];
          }
            
          // sum = multicastMessage.getBytes();
          int multicastPort = 9068;
          InetAddress group = InetAddress.getByName("225.4.5.6"); 
          MulticastSocket sendSocket = new MulticastSocket( );
          //launch the read thread
          
          
          System.out.println("Local ID: " + processID);
          System.out.println("The local sum is " + sum);
          String multicastMessage = "Message from Process#" + processID + ":" + 
                  "Local sum is " + sum;
          byte[] msg = multicastMessage.getBytes();
          //int vsum = sum;
          
          DatagramPacket packet = new DatagramPacket(msg, msg.length, group, 
                  multicastPort, sum);
    	// wait for the read threads of all the processes to start, before sending the partial sum value
          System.out.print("Hit return to send message\n\n"); 
          BufferedReader ir = new BufferedReader(new InputStreamReader(System.in)); 
          ir.readLine( ); 
        readThread rt = new readThread(group, multicastPort, sum);
          rt.start();
          sendSocket.send(packet);
          // send <key, value> as a string where key is the process ID and value is the partial sum value
    
      
    
    
        }
       
       catch(Exception e){e.printStackTrace();}
    
    }
      
    }
    class readThread extends Thread{
        InetAddress group;
        int multicastPort;
        int MAX_MSG_LEN = 10000;
       int sum;
        
        //multicastSenderReceiver vsum = new multicastSenderReceiver();
        //int vsum;
        readThread(InetAddress g, int port, int vsum ){
            group = g;
            multicastPort = port;
            sum = vsum;
            
        }
        
        public void run(){
            try{
                MulticastSocket readSocket = new MulticastSocket(multicastPort);
                readSocket.joinGroup(group);
                while(true){
                    byte[] message = new byte[MAX_MSG_LEN];
                    DatagramPacket packet = new DatagramPacket(message,
                            message.length,group, multicastPort, sum);
                    
                    readSocket.receive(packet);
                    String msg = new String(packet.getData());
                    System.out.println(msg);
                }
            
            }
            catch(Exception e){e.printStackTrace();}
        }
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help passing a value for Sockets

    Which DatagramPacket class constructor are you trying to use? The compiler can not find one that matches what you have coded.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    goagray is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: Need help passing a value for Sockets

    Ahh, I see there is no model that I can use.. so I guess originally I was using
    Java Code:
     DatagramPacket(byte[] buf, int length, InetAddress address, int port)
    but I tried to pass another int, sum, in there which is why I got the error. So I guess my question is.. How can I pass the local sums computed at each client in the multicastSenderReciever class to be computed for the overall sum of all the clients in the readThread class? Or should I develop a method in the multicastSenderReciever class to compute both the local sum and overall sum?? I'm so confused on how to get the overall sum for all the clients.. any suggestions will help.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help passing a value for Sockets

    How can I pass the local sums
    The values to pass are in the byte[] array: buf. An int value can be separated into 4 bytes to go into a byte array
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    goagray is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: Need help passing a value for Sockets

    Okay I understand. I passed the local sum within the byte array. I guess, the most important question is.. how can I extract each local sum from each client to compute the TOTAL sum and then broadcast that to all clients?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Need help passing a value for Sockets

    What site collects all the client's values? When it gets all the values from the clients, it can compute the sum and sent that sum to all the clients.

    What communications does the current code handle? Who can send what and who can receive what?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    goagray is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: Need help passing a value for Sockets

    I hope I am understanding your question correctly..
    I compute the local sum of a client with a for loop in the senderreceiver class, and then send the packet.
    The readThread class receives the packet and then displays the packet to the clients.
    So a client is able to send a message, but also able to receive the messages from each other client.

    For example, if there are 3 clients (#1, #2, #3). Each computes its local sum from randomly generated integers, and then it is displayed at that local client. Once all the local sums are computer for each client and displayed at their own local client, it waits for the user to hit enter at client window, #1, and then that action sends the process ID #1 and its local sum value to all the other clients. Then the user at client window #2 would do the same and so on.
    Once all local sum values are sent, each window takes those values and adds them up to show the overall sum.

    Am I making sense? If not, I can just post the assignment.

Similar Threads

  1. Sockets API
    By safwa_man in forum Networking
    Replies: 2
    Last Post: 04-27-2012, 12:11 AM
  2. Sockets
    By Maya in forum Networking
    Replies: 6
    Last Post: 04-20-2011, 12:52 AM
  3. Sockets
    By PhQ in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 04:36 AM
  4. Sockets NIO
    By aamp in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 11:56 AM
  5. Help with Sockets
    By Eric in forum Networking
    Replies: 3
    Last Post: 12-01-2007, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •