Connecting Tech Pros Worldwide Forums | Help | Site Map

Client-Server connectivity through Java

Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#1: Mar 28 '09
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's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Mar 28 '09

re: Client-Server connectivity through Java


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
Banned
 
Join Date: Oct 2008
Location: Lahore Pakistan
Posts: 82
#3: Mar 29 '09

re: Client-Server connectivity through Java


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

Regards
ARUZ
Attached Files
File Type: zip APP.zip (1.1 KB, 8 views)
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#4: Mar 30 '09

re: Client-Server connectivity through Java


@ JosAH and EYE4U
Thanks
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#5: Apr 1 '09

re: Client-Server connectivity through Java


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:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. class clientt
  4. {
  5.     public static void main(String args[]) throws SocketException,IOException
  6.     {
  7.         Socket s=new Socket("169.254.46.123",12345);
  8.         PrintStream ps=new PrintStream(s.getOutputStream());
  9.         ps.println("Hello from client side");
  10.         DataInputStream dis=new DataInputStream(s.getInputStream());
  11.         System.out.println(dis.readLine());
  12.         ps.close();
  13.     }
  14. }
The code for the server is :

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. class servert
  4. {
  5.     public static void main(String args[])throws SocketException,IOException
  6.     {
  7.         ServerSocket ss=new ServerSocket(12345);
  8.         Socket s=ss.accept();
  9.         DataInputStream dis=new DataInputStream(s.getInputStream());
  10.         System.out.println(dis.readLine());
  11.         PrintStream ps=new PrintStream(s.getOutputStream());
  12.         ps.println("Hello from server side");
  13.         ps.close();
  14.     }
  15. }
Need some help here.
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#6: Apr 1 '09

re: Client-Server connectivity through Java


I have attached the screenshot of the exact error message, which showed up on the server-side.
Attached Thumbnails
error.jpg  
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#7: Apr 2 '09

re: Client-Server connectivity through Java


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
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2.     import java.awt.event.*;
  3.     import java.applet.*;
  4.     import java.io.*;
  5.     import java.net.*;
  6.  /*
  7.      <applet code="client_button" width = 250 height = 150>
  8.      </applet>
  9.      */
  10.  
  11.  
  12.     public class server_gui extends Applet implements ActionListener
  13.     {
  14.         String msg= "";
  15.         Button yes;
  16.         Button reset;
  17.  
  18.     @Override
  19.         public void init()
  20.         {
  21.                 yes= new Button("Ready");
  22.                 reset= new Button("Reset");
  23.  
  24.                 add(yes);
  25.                 add(reset);
  26.  
  27.                 yes.addActionListener(this);
  28.                 yes.addActionListener(this);
  29.  
  30.         }
  31.  
  32.         public void actionPerformed(ActionEvent ae)
  33.         {
  34.             String str= ae.getActionCommand();
  35.             if(str.equals("Ready"))
  36.             {
  37.               try
  38.              {
  39.            // System.out.println("Done");
  40.             ServerSocket ss=new ServerSocket(4303);
  41.             System.out.println(" The server is ready and is listening intently");
  42.             Socket s=ss.accept();
  43.             //thread.run();
  44.             s.setSoTimeout(0);
  45.             DataInputStream dis=new DataInputStream(s.getInputStream());
  46.             msg=dis.readLine();
  47.  
  48.             System.out.println(msg);
  49.  
  50.             PrintStream ps=new PrintStream(s.getOutputStream());
  51.             ps.println(" Your buzz has been registered. Please wait for next question..");
  52.             //Thread.currentThread().sleep(3767);
  53.             //s.close();
  54.             ps.close();
  55.           }
  56.           catch( Exception e)
  57.           {
  58.               System.out.println(" Something is wrong with the connection.."+"\n"+e);
  59.               e.printStackTrace();
  60.  
  61.           }
  62.         }
  63.               else
  64.                   if(str.equals("Ready"))
  65.                   {
  66.  
  67.                   }
  68.  
  69.           //repaint();
  70.        }
  71.  
  72.  
  73.         public void paint(Graphics g)
  74.         {
  75.            g.drawString(msg,30,400);
  76.         }
  77. }
Client
[
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2.     import java.awt.event.*;
  3.     import java.applet.*;
  4.     import java.io.*;
  5.     import java.net.*;
  6.  /*
  7.      <applet code="client_button" width = 1000 height = 1000>
  8.      </applet>
  9.      */
  10.  
  11.  
  12.     public class client_button extends Applet implements ActionListener
  13.     {
  14.         String msg= "";
  15.         Button yes;
  16.  
  17.  
  18.  
  19.         public void init()
  20.         {
  21.             yes= new Button("\n\n                                                                                                                  Click Me!                                                                                                                                \n\n");
  22.             add(yes);
  23.             yes.addActionListener(this);
  24.         }
  25.  
  26.         public void actionPerformed(ActionEvent ae)
  27.         {
  28.             System.out.println("Clicked By CLeint");
  29.             String str= ae.getActionCommand();
  30.             if(str.equals("\n\n                                                                                                                  Click Me!                                                                                                                                \n\n"))
  31.             {
  32.                 //msg= "You have buzzed. Kindly wait for next question ";
  33.                 try
  34.                 {
  35.                 Socket s=new Socket("localhost",4303);
  36.  
  37.                 //s.setSoTimeout(0);
  38.                 PrintStream ps=new PrintStream(s.getOutputStream());
  39.                 ps.println("Team X has buzzed");
  40.                 DataInputStream dis=new DataInputStream(s.getInputStream());
  41.                 msg=dis.readLine();
  42.                 System.out.println(msg);
  43.  
  44.                 ps.close();
  45.                 }
  46.                 catch(Exception s)
  47.                 {
  48.                     System.out.println(" Something wrong with the connection "+"\n"+s);
  49.                 }
  50.  
  51.             }
  52.  
  53.             //repaint();
  54.         }
  55.  
  56.  
  57.         public void paint(Graphics g)
  58.         {
  59.             //String temp=" This is a neat buzzer-system. Have a nice time. Don't let you score slip below ";
  60.             setFont( new Font (" SansSerif", Font.BOLD, 15));
  61.  
  62.             //g.drawString(temp, 50, 220);
  63.             g.drawString(msg,50,100);
  64.  
  65.         }
  66.    }
  67.  
  68. }
  69.  
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(regarding 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.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: Apr 2 '09

re: Client-Server connectivity through Java


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.
Newbie
 
Join Date: Mar 2009
Location: India
Posts: 14
#9: Apr 2 '09

re: Client-Server connectivity through Java


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.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Apr 2 '09

re: Client-Server connectivity through Java


See the first parts of the article Doing Swing Right?
Reply


Similar Java bytes