Client-Server connectivity through Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • p vs np
    New Member
    • Mar 2009
    • 26

    Client-Server connectivity through Java

    Hi.
    I plan to implement a simple project idea. It involves connecting 5-6 machines (laptops, preferably) through a local wi-fi connection with one of these machines being the master. The master machine should have a simple GUI which displays a number corresponding to the machine in which a mouse-click was done first.

    Its application could be seen in quiz shows, as a buzzer system, where the machines are connected via local wi-fi and each team has a mouse for a buzzer. The first team buzzing/clicking shall be displayed on the master machine's screen.

    Could some one help me out with the details on how to get started. I have no idea at all, on its implementation.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Keep it simple: have a ServerSocket at your server machine that listens for connecting Sockets from client machines. Start a Thread for each connected client. Each Thread waits for incoming data (that should represent a mouse click). The first Thread to register the 'click' wins. Read the API documentation for the classes I mentioned.

    kind regards,

    Jos

    Comment

    • EYE4U
      Banned
      New Member
      • Oct 2008
      • 76

      #3
      Attached is a simple code of server client application :)
      Add Multithreading and database yourself.

      Regards
      ARUZ
      Attached Files

      Comment

      • p vs np
        New Member
        • Mar 2009
        • 26

        #4
        @ JosAH and EYE4U
        Thanks

        Comment

        • p vs np
          New Member
          • Mar 2009
          • 26

          #5
          Well, I am facing problems trying to implement this.
          What I had in mind was communication in an ad-hoc network created between the laptops.
          Initially, i tried broadcasting over my machine (under 127.0.0.1) and it worked fine.
          But then, i try to do it between two machines connected through an ad-hoc wi-fi network, the server and the client are apparrantly connected, and after a while, because of the default time, a message appears on both sides saying the network has timed out.

          The code for the client is:

          Code:
          import java.io.*;
          import java.net.*;
          class clientt
          {
              public static void main(String args[]) throws SocketException,IOException
              {
                  Socket s=new Socket("169.254.46.123",12345);
                  PrintStream ps=new PrintStream(s.getOutputStream());
                  ps.println("Hello from client side");
                  DataInputStream dis=new DataInputStream(s.getInputStream());
                  System.out.println(dis.readLine());
                  ps.close();
              }
          }
          The code for the server is :

          Code:
          import java.io.*;
          import java.net.*;
          class servert
          {
              public static void main(String args[])throws SocketException,IOException
              {
                  ServerSocket ss=new ServerSocket(12345);
                  Socket s=ss.accept();
                  DataInputStream dis=new DataInputStream(s.getInputStream());
                  System.out.println(dis.readLine());
                  PrintStream ps=new PrintStream(s.getOutputStream());
                  ps.println("Hello from server side");
                  ps.close();
              }
          }
          Need some help here.

          Comment

          • p vs np
            New Member
            • Mar 2009
            • 26

            #6
            I have attached the screenshot of the exact error message, which showed up on the server-side.
            Attached Files

            Comment

            • p vs np
              New Member
              • Mar 2009
              • 26

              #7
              This part is solved i guess.
              There was a problem with the masking of the IPv6 addresses.

              The next problem i have encountered happens to be of a queer nature.
              I developed applets for the above code.

              Server
              Code:
              import java.awt.*;
                  import java.awt.event.*;
                  import java.applet.*;
                  import java.io.*;
                  import java.net.*;
               /*
                   <applet code="client_button" width = 250 height = 150>
                   </applet>
                   */
              
              
                  public class server_gui extends Applet implements ActionListener
                  {
                      String msg= "";
                      Button yes;
                      Button reset;
              
                  @Override
                      public void init()
                      {
                              yes= new Button("Ready");
                              reset= new Button("Reset");
              
                              add(yes);
                              add(reset);
                         
                              yes.addActionListener(this);
                              yes.addActionListener(this);
                         
                      }
              
                      public void actionPerformed(ActionEvent ae)
                      {
                          String str= ae.getActionCommand();
                          if(str.equals("Ready"))
                          {
                            try
                           {
                         // System.out.println("Done");
                          ServerSocket ss=new ServerSocket(4303);
                          System.out.println(" The server is ready and is listening intently");
                          Socket s=ss.accept();
                          //thread.run();
                          s.setSoTimeout(0);
                          DataInputStream dis=new DataInputStream(s.getInputStream());
                          msg=dis.readLine();
              
                          System.out.println(msg);
                          
                          PrintStream ps=new PrintStream(s.getOutputStream());
                          ps.println(" Your buzz has been registered. Please wait for next question..");
                          //Thread.currentThread().sleep(3767);
                          //s.close();
                          ps.close();
                        }
                        catch( Exception e)
                        {
                            System.out.println(" Something is wrong with the connection.."+"\n"+e);
                            e.printStackTrace();
              
                        }
                      }
                            else
                                if(str.equals("Ready"))
                                {
              
                                }
                      
                        //repaint();
                     }
              
                  
                      public void paint(Graphics g)
                      {
                         g.drawString(msg,30,400);
                      }
              }
              Client
              [
              Code:
              import java.awt.*;
                  import java.awt.event.*;
                  import java.applet.*;
                  import java.io.*;
                  import java.net.*;
               /*
                   <applet code="client_button" width = 1000 height = 1000>
                   </applet>
                   */
              
              
                  public class client_button extends Applet implements ActionListener
                  {
                      String msg= "";
                      Button yes;
              
                      
              
                      public void init()
                      {
                          yes= new Button("\n\n                                                                                                                  Click Me!                                                                                                                                \n\n");
                          add(yes);
                          yes.addActionListener(this);
                      }
              
                      public void actionPerformed(ActionEvent ae)
                      {
                          System.out.println("Clicked By CLeint");
                          String str= ae.getActionCommand();
                          if(str.equals("\n\n                                                                                                                  Click Me!                                                                                                                                \n\n"))
                          {
                              //msg= "You have buzzed. Kindly wait for next question ";
                              try
                              {
                              Socket s=new Socket("localhost",4303);
              
                              //s.setSoTimeout(0);
                              PrintStream ps=new PrintStream(s.getOutputStream());
                              ps.println("Team X has buzzed");
                              DataInputStream dis=new DataInputStream(s.getInputStream());
                              msg=dis.readLine();
                              System.out.println(msg);
                              
                              ps.close();
                              }
                              catch(Exception s)
                              {
                                  System.out.println(" Something wrong with the connection "+"\n"+s);
                              }
              
                          }
                        
                          //repaint();
                      }
              
                    
                      public void paint(Graphics g)
                      {
                          //String temp=" This is a neat buzzer-system. Have a nice time. Don't let you score slip below ";
                          setFont( new Font (" SansSerif", Font.BOLD, 15));
                          
                          //g.drawString(temp, 50, 220);
                          g.drawString(msg,50,100);
                         
                      }
                 }
              
              }
              Now the problems are:

              1. The text received by the client from server and shown on the GUI flickers and is not comprehensible.

              2. There is an exception saying "Bind Exception: Address already in use" in the server side after the message(regardi ng which team buzzed) is received from the client and printed on console! and as a result, its not being printed on server GUI.

              Please help me out.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                1.) Don't roll off IO related tasks from the EDT. Roll them out in their own separate thread so that the EDT continues to update the interface properly.
                2.) That means the port you are trying to use is already in use by another application. It could have been left open by your application too! You must close all sockets after use and make sure you close them even if your code throws an exception at some point.

                Comment

                • p vs np
                  New Member
                  • Mar 2009
                  • 26

                  #9
                  Could you please point at the code which needs to be modified, with some more details provided.

                  I am not very comfortable with the terminology and methods as this is the first time i am trying my hand at socket programming.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    See the first parts of the article Doing Swing Right?

                    Comment

                    Working...