-
whiteboard - client server problem
I have build a whiteboard application that consist of a client and a server. The client will send the coordinates of what is drawn on a whiteboard to a server. The server is capable of handling multiple clients. The server will receive the coordinates from the clients and broadcast it to all the other clients so that other clients can see what is drawn. The problem I am facing with the code is that my client keeps receiving the same coordinates send to the server and therefore the clients do not get updated. How do I solve the problem?. I attach also the link for the java files:
http://upload2.net/page/download/43V...board.zip.html
Your help is kindly appreciated. Also if you require any further information, I would be willing to provide.
Regards.
P.S Please take note that this is not a school homework.
-
I enclose herewith the simplified version of the code. The server side is the full code but the client side is a simplified version of code.
Server Side Code:
StartServ.java
--------------------
import java.net.*;
import java.io.*;
import java.util.*;
public class StartServ
{
int portnr = 9000;
public StartServ()
{
try
{
ServerSocket serv=new ServerSocket(portnr);
while(true)
{
Socket s=serv.accept();
System.out.println("Accepted from
client: "+s.getInetAddress());
Handler c=new Handler(s);
c.start();
}
}
catch(IOException e)
{
}
}
public static void main(String args[])
{
new StartServ();
}
}
Handler.java
------------------
import java.net.*;
import java.io.*;
import java.util.*;
class Handler extends Thread
{
private Socket socket = null;
ObjectOutputStream oos1;
ObjectInputStream oos2;
Vector ht1;
protected static Vector list=new Vector();
public Handler(Socket socket)
{
this.socket = socket;
try
{
oos1 = new ObjectOutputStream(socket.getOutputStream());
oos2 = new ObjectInputStream(socket.getInputStream());
}
catch(Exception e)
{
}
}
public void run()
{
try
{
list.addElement(this);
while(true)
{
try
{
ht1 = (Vector)oos2.readObject();
System.out.println(ht1.size());
}
catch(ClassNotFoundException cnfe)
{
System.out.println("class not found!");
}
broadcast(ht1);
}
}
catch(IOException e)
{
System.out.println("IOException...");
}
finally
{
list.removeElement(this);
System.out.println("User exiting...");
try
{
socket.close();
}
catch(IOException e)
{
}
}
}
protected static void broadcast(Vector ob)
{
synchronized(list)
{
Enumeration en=list.elements();
while(en.hasMoreElements())
{
Handler eh=(Handler) en.nextElement();
try
{
synchronized(eh.oos1)
{
eh.oos1.writeObject(ob);
System.out.println("Object sent...");
}
eh.oos1.flush();
}
catch(IOException e)
{
eh.stop();
}
}
}
}
}
Client Side:
WhiteBoard.java
------------------------
public void init()
{
ht = new Vector();
ht2 = new Vector();
try
{
skt = new Socket("localhost", 9000);
oos = new ObjectOutputStream(skt.getOutputStream());
ois2 = new ObjectInputStream(skt.getInputStream());
}
catch(Exception e)
{
}
new Thread(this).start();
}
public void mouseDragged(MouseEvent e)
{
drawLines(pre_x + ":" + pre_y + ":" + x + ":" + y);
}
public void drawLines(String m)
{
ht.add(m);
}
public void mouseReleased(MouseEvent e)
{
oos.writeObject(ht);
ht.clear();
}
Similar Threads
-
By forumname in forum Mobile
Replies: 1
Last Post: 10-18-2007, 02:50 PM
-
By rohan076 in forum Java
Replies: 1
Last Post: 03-30-2006, 03:11 AM
-
By shipra pandey in forum Database
Replies: 0
Last Post: 02-08-2006, 05:55 AM
-
Replies: 1
Last Post: 10-06-2005, 03:35 PM
-
Replies: 7
Last Post: 09-15-2000, 08:44 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks