Results 1 to 13 of 13
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Client/ Server system that adds a stream of integers

    Hi all,
    i currently have a client/ server system that receives input from the client and can add two integers together that are inputted in the command prompt.
    However i cannot get the system to add more than two numbers that are inputted without declaring multiple variables.
    I now want the server to add together a stream of inputted integers that are entered in the command prompt. The code is as follows:

    Server main class:
    Java Code:
    package server;
    import java.io.*;
    import java.net.*;
    /**
     *
     * @author Billy
     */
    public class Main
    {
    private static void sendResponse(Socket socket) throws IOException, InterruptedException
    {
       InputStream instream = socket.getInputStream();
       InputStreamReader isr = new InputStreamReader(instream, "US-ASCII");
       BufferedReader inBuf = new BufferedReader(isr);
       OutputStream outstream = socket.getOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(outstream, "US-ASCII");
       PrintWriter out = new PrintWriter(osw);
       String s = inBuf.readLine();
       int num1 = Integer.parseInt (s);
       s = inBuf.readLine ();
       int num2 = Integer.parseInt (s);
       int sum = num1 + num2;
       out.println("Hello, " + num1 + "+" + num2 + "=" + sum);
       System.err.println("got message: " + s);
       Thread.sleep(500);
       out.close();
    }
    public static void main(String[] args) throws Exception
    {
       int port = 55555;
       ServerSocket serverSocket = new ServerSocket(port);
       System.err.println("server starting, using port " + port);
       for ( ; ; )
         {
            Socket socket = serverSocket.accept();
            System.err.println("socket accepted: " + socket.toString());
            socket.setSoLinger(true, 1);
            sendResponse(socket);
         }
    }
    }
    Client Main class:
    Java Code:
    package client;
    import java.io.*;
    import java.net.*;
    /**
     *
     * @author Billy
     */
    public class Main
    {
       public static void main(String[] args) throws Exception
       {
          String serverName = "localhost";
          int serverPort = 55555;
         String num1 = "";
         String num2 = "";
    
         if (args.length > 0)
         {
            serverName = args[0];
         }
            if (args.length > 1)
            {
                num1 = args[1];
                num2 = args[2];
            }
            InetAddress inetAddress = InetAddress.getByName(serverName);
            Socket socket = new Socket(inetAddress, serverPort);
            OutputStream outstream = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(outstream, "US-ASCII");
            PrintWriter out = new PrintWriter(osw);
            InputStream instream = socket.getInputStream();
            InputStreamReader isw = new InputStreamReader(instream, "US-ASCII");
            BufferedReader inBuf = new BufferedReader(isw);
            out.println(num1);
            out.println(num2);
            out.flush();
            socket.shutdownOutput();
            for (String s = inBuf.readLine(); s != null; s = inBuf.readLine())
              {
                 System.out.println(String.format("from server: %s", s));
              }
       }
    }
    If anyone could help me out that would be great
    Many Thanks - Billy
    Last edited by Billywizz; 01-26-2011 at 10:49 PM.

  2. #2
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Just to make myself clear,
    My main goal is to compute the sum of all values sent to the server
    Any ideas?

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You only need two variables: the sum, and the number you want to add to it. Your sum variable should be an instance variable that you initialize to zero. Then you can either do:
    Java Code:
            sum = sum + number;
    or more simply:
    Java Code:
            sum += number;
    -Gary-

  4. #4
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Thanks for your reply gary,
    So will that therefore read all the inputs from the client for example if i was to put in the comand prompt 3 5 2 4 7 8
    the server will compute the total sum of all these numbers even tho i have only declared num1 and num2?

    Sorry if im abit slow lol im a begginer :D

    Thanks - Billy

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Billywizz View Post
    Thanks for your reply gary,
    So will that therefore read all the inputs from the client for example if i was to put in the comand prompt 3 5 2 4 7 8
    the server will compute the total sum of all these numbers even tho i have only declared num1 and num2?

    Sorry if im abit slow lol im a begginer :D

    Thanks - Billy
    No.

    First of all, please go back and edit your first post, and put your code between CODE tags.

    [CODE]
    Your code goes here.
    [/CODE]

    Please make sure it's formatted and indented properly so that it's easy to read.

    Look where you are using parseInt() to read your num1 and num2 variables. You need a loop there that uses parseInt() to read number from the String, then immediately adds number to sum. Then go through the loop again and again until you don't have any more ints in your String.

    -Gary-

  6. #6
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Gary,
    i have now played around abit with what you said but still no joy and i think im doing it completly wrong to what you said :( i will show you what i have now:

    Server Main class:

    Java Code:
    package addingnumbersserver;
    import java.io.*;
    import java.net.*;
    /**
     * @author Billy
     */
    public class Main
     {
        private static void sendResponse(Socket socket) throws IOException,
        InterruptedException
          {
              InputStream instream = socket.getInputStream();
              InputStreamReader isr = new InputStreamReader(instream, "US-  
              ASCII");
              BufferedReader inBuf = new BufferedReader(isr);
              OutputStream outstream = socket.getOutputStream();
              OutputStreamWriter osw = new OutputStreamWriter(outstream, "US-
              ASCII");
              PrintWriter out = new PrintWriter(osw);
              String s = inBuf.readLine();
              while (true)
                {
                    int number = Integer.parseInt (s);
                    int sum = 0;
                    int finalsum = sum + number; 
                    out.println("Hello, " + sum + "+" + number + "=" + finalsum);
                    System.err.println("got message: " + finalsum);
                    Thread.sleep(500);
                    out.close();
                }
          }
     public static void main(String[] args) throws Exception
    {
        int port = 55555;
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("server starting, using port " + port);
        for ( ; ; )
        {
           Socket socket = serverSocket.accept();
           System.err.println("socket accepted: " + socket.toString());
           socket.setSoLinger(true, 1);
           sendResponse(socket);
        }
    }
    }
    Client Main class:

    Java Code:
    package addingnumbersclient;
    import java.io.*;
    import java.net.*;
    /**
     *
     * @author Billy
     */
    public class Main
    {
       public static void main(String[] args) throws Exception
       {
          String serverName = "localhost";
          int serverPort = 55555;
          String num1 = "";
          String num2 = "";
    
          if (args.length > 0)
          {
              serverName = args[0];
          }
    
          if (args.length > 1)
          {
             num1 = args[1];
             num2 = args[2];
          }
       InetAddress inetAddress = InetAddress.getByName(serverName);
       Socket socket = new Socket(inetAddress, serverPort);
       OutputStream outstream = socket.getOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(outstream, "US-
       ASCII");
       PrintWriter out = new PrintWriter(osw);
       InputStream instream = socket.getInputStream();
       InputStreamReader isw = new InputStreamReader(instream, "US-ASCII");
       BufferedReader inBuf = new BufferedReader(isw);
       out.println(num1);
       out.println(num2);
       out.flush();
       socket.shutdownOutput();
       for (String s = inBuf.readLine(); s != null; s = inBuf.readLine())
       {
         System.out.println(String.format("from server: %s", s));
        }
    }
    }
    Very Confussed :S
    -Billy

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, step away from the computer for a moment and think about how you would do this manually. Take a deck of cards and remove all the face cards and aces. Now grab a handful of cards, and add up all the numbers to get a total. Think about what you're doing, step by step. Try to write every step down. Then do it again, exactly following your written-down steps. Once you've done that, post your list of steps here.

    -Gary-

  8. #8
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Ok
    1) i would see the first card
    2) i would add the second card to the first card
    3) add the total of those two cards to the next card
    4) continue until the end of the pack

    I think i understand how it should work its just putting it into the programme like where to put the loop and how to use it and how to get the numbers from the client :S

    i am a very begginer but studying at uni and really want to learn :)

    whats the next stage then?

    Thanks - Billy

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Billywizz View Post
    1) i would see the first card
    2) i would add the second card to the first card
    3) add the total of those two cards to the next card
    4) continue until the end of the pack (let's say handful -- you're not necessarily getting a full pack)
    The problem with this is that you are treating the first card as special, and you are assuming that there will be at least two cards. Try again, but try to treat every card the same. Hint: start with a sum in your head or on a sheet of paper that starts at zero.

    -Gary-

  10. #10
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Ok

    1) starting with blank mind not seeing any card this would represent 0
    2) then add the first card to 0
    3) then add the next card to the sum of 0 + the first card
    4) then add the next card to the sum of the answer to the previous step
    5) continue
    6) add the last card to the cummulative sum
    7) No more cards therefore stop

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    OK, that's a little better (you're still trying to treat the first card as special, but we'll fix that). We want to write that in pseudocode which means plain language, but structured like code is structured, and thinking in terms of initialization and loops and checking conditions. So:
    Java Code:
            initialize sum to 0
            while there are more cards:
                    take a card
                    read the number on the card
                    add the number to sum
            until no more cards
            report sum
    Does that much make sense?

    -Gary-

  12. #12
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Yes it does thats exactly what i want it to do its just how to do it in the programme. Such as how do you put in the while loop whether there is another card? and where that loop should be placed?

    -Billy

  13. #13
    Debugger is offline Member
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    Something to consider would be taking advantage of args [], rather than setting what is in args to num1, num2, look into sending the whole array to the server, and then look into how to add up all the contents in the array.

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  2. Replies: 3
    Last Post: 01-14-2010, 04:22 AM
  3. TCP/IP Client program halts at the read method of the stream
    By PradeepBadiger in forum Networking
    Replies: 0
    Last Post: 03-26-2009, 03:14 PM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

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