473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Client-Server connectivity through Java

26
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.
Mar 28 '09 #1
9 2731
JosAH
11,448 Expert 8TB
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
Mar 28 '09 #2
EYE4U
75
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, 149 views)
Mar 29 '09 #3
p vs np
26
@ JosAH and EYE4U
Thanks
Mar 30 '09 #4
p vs np
26
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.
Apr 1 '09 #5
p vs np
26
I have attached the screenshot of the exact error message, which showed up on the server-side.
Attached Images
File Type: jpg error.jpg (17.9 KB, 172 views)
Apr 1 '09 #6
p vs np
26
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.
Apr 2 '09 #7
r035198x
13,262 8TB
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.
Apr 2 '09 #8
p vs np
26
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.
Apr 2 '09 #9
r035198x
13,262 8TB
See the first parts of the article Doing Swing Right?
Apr 2 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Chris | last post by:
<apologies for cross-posting> Hi All, I am based in the UK and have been doing some private work for a client which involved setting up a database and scripts to search it and display results...
1
by: Justin Stockton | last post by:
I recently upgraded from ActivePython 2.2.2 to ActivePython 2.3.2 and I'm running into an issue importing the win32com.client module. Before installing the new version, I made sure to properly...
5
by: Paul H | last post by:
How do you folks get a reliable and complete brief of what is required before development starts? I am forever going back to a client once a project has started saying "Hang on, now that I've...
9
by: Harry Smith | last post by:
While reading the documentation on IsStartupScriptRegistered, there is a reference to "client startup script" as "Determines if the client startup script is registered with the Page object." What...
11
by: pshindle | last post by:
We have several machines currently running the DB2 V7 Run-time Client that we would like to actually be running the App Dev Client. To 'upgrade' (within the same version) this client software can...
2
by: J Huntley Palmer | last post by:
I am having a horrific time integrating uw-imap's c-client for imap support in php. The problem is a whole bunch of "Text relocation remains referenced against symbol" errors during linking....
11
by: Wayne | last post by:
I am a one man enterprise and have been asked by a prospective client what happens to their database regarding ongoing changes etc if I get hit by a bus. Obviously my databases are distributed...
3
by: rjha94 | last post by:
Hi I just installed the runtime client on my windows machine. when i go inside the SQLLIB\bin folder i can see db2.exe. is it possible to use this db2 bundled with runtime client for command line...
4
MMcCarthy
by: MMcCarthy | last post by:
http://bytes.com/images/howtos/projectscope_blocks.jpgAs a freelance IT consultant for over 10 years, I’ve come to appreciate well defined project scopes. A project scope is a common understanding...
11
by: Bill Davy | last post by:
I am trying to edit Contacts in Outlook. This is so I can transfer numbers from my address book which is an Excel spreadsheet to my mobile phone. I came across the following snippet of code which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.