Java Forum / General / October 2007
client server - client problem
solomon13000@gmail.com - 08 Oct 2007 12:43 GMT WhiteboardApplet.java --------------------------------
import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*;
import java.net.*; import java.io.*;
public class WhiteBoardApplet extends Applet implements ActionListener, AdjustmentListener,KeyListener, MouseListener, MouseMotionListener, WindowListener {
Socket skt; ObjectOutputStream oos; Vector ht;
ServerSocket ss2; Socket cs2; Vector ht2; ObjectInputStream ois2;
//declare variables Image offscreenImage; Graphics offscreenImageG; Graphics drawPanelG; int offscreeImage_width, offscreeImage_height; boolean isButtonPressed; int pre_x, pre_y; //the coordinates at the beginning of mouse dragged int x, y; //the coordinates in the end of mouse dragged int top_left_corner_x, top_left_corner_y; //the x and y coordonate of top-left corner //that a image is drawn with in the graphic context int scrollbarV_value; //to hold vertical scrollbar value int scrollbarH_value; //to hold herizonal scroll bar value String s; //to hold string input from keyboard /*************************** * init() * description: The entry point for the applet. * build window and initiate variables * ************************** */ public void init() { //build window buildWindow ( ); //initiate variables isButtonPressed=false; top_left_corner_x=0; top_left_corner_y=0; offscreeImage_width=2000; offscreeImage_height=2000;
drawPanelG = drawPanel.getGraphics ();
offscreenImage = this.createImage( offscreeImage_width, offscreeImage_height); offscreenImageG = offscreenImage.getGraphics(); offscreenImageG.setColor( Color.white ); offscreenImageG.fillRect( 0, 0, offscreeImage_width, offscreeImage_height); offscreenImageG.setColor( Color.black );
ht = new Vector(); ht2 = new Vector();
GandhiUpdate2(); }
/*************************** * update() * description: * update the container, call the method paint() * ************************** */ public void update( Graphics g ) { paint( g ); }
/*************************** * paint() * description: * drawPanelG draw offscreenImage, * begin from the top left corner * ************************** */
public void paint( Graphics g ) { drawPanelG.drawImage ( offscreenImage,top_left_corner_x, top_left_corner_y, this ); }
/**************Build Window****************/
//declare variables for buiding window Frame outerBox; //to place all visible components Panel topPanel; //to hold "clear" button Panel drawPanel; // draw area Panel bottomPanel; // to hold herizonal scrollbar and message area Scrollbar scrollbarV; //vertical scrollbar Scrollbar scrollbarH; //herizonal scrollbar TextField message; Button clearButton;
private void buildWindow ( ) {
// *** Instantiate objects outerBox = new Frame ( ); topPanel= new Panel(); drawPanel = new Panel ( ); bottomPanel = new Panel ( );
//***build button Button clearButton=new Button("Clear"); clearButton.setBackground(new Color(193, 211, 245)); clearButton.addActionListener(this);
// *** build the scrollbars and add addAdjustmentListener scrollbarV = new Scrollbar (Scrollbar.VERTICAL, 0, 100, 0, 1000 ); scrollbarV.addAdjustmentListener ( this ); scrollbarH = new Scrollbar (Scrollbar.HORIZONTAL, 0, 100, 0, 1000 ); scrollbarH.addAdjustmentListener ( this ); // *** build the message display area message = new TextField ( "Messages Displayed Here", 80 ); message.setEditable ( false ); //set the textField message as not editable
//build the top panel topPanel.setSize(600, 30); topPanel.add(clearButton);
//register envent on the draw panel(draw area) drawPanel.addKeyListener(this); drawPanel.addMouseListener(this); drawPanel.addMouseMotionListener(this);
// *** build the bottom panel bottomPanel.setLayout ( new BorderLayout ( ) ); bottomPanel.add ( "North", scrollbarH ); bottomPanel.add ( "South", message );
// *** Set colors topPanel.setBackground(Color.gray); drawPanel.setBackground (Color.white);
// *** build the window outerBox.setLayout (new BorderLayout ( ) ); outerBox.add ("North", topPanel); outerBox.add ("Center", drawPanel); outerBox.add ("South", bottomPanel); outerBox.add ("East", scrollbarV); outerBox.setSize (600, 500); outerBox.setBackground (Color.white); outerBox.addWindowListener (this); outerBox.setVisible ( true ); } // end buildWindow ()
//*********** Interface Methods ***********
//**** actionPerformed methods public void actionPerformed ( ActionEvent e ) { //call actionClear() actionClear(); }//end actionPerformed method
//actionClear method private void actionClear(){ //fill the offscreenImage all white offscreenImageG.setColor( Color.white ); offscreenImageG.fillRect( 0, 0, offscreeImage_width, offscreeImage_height); offscreenImageG.setColor( Color.black ); repaint(); }//end actionClear
//**** AdjustmentListener method public void adjustmentValueChanged ( AdjustmentEvent e ) { Object s = e.getSource(); // *** process Scrollbar actions if ( s instanceof Scrollbar ) { if ( s == scrollbarV ) { scrollbarV_value=scrollbarV.getValue(); top_left_corner_y = - scrollbarV_value; message.setText ( "Vertical ScrollBar value: " + scrollbarV_value ); repaint(); }
if ( s == scrollbarH ) { scrollbarH_value=scrollbarH.getValue(); top_left_corner_x = - scrollbarH_value; message.setText ( "Horlzontal ScrollBar value: " + scrollbarH_value); repaint(); } } // end process scrollbar actions } //end adjustmentValueChanged method
//*****KeyListener methods public void keyTyped( KeyEvent e ) { char c = e.getKeyChar(); if ( c != KeyEvent.CHAR_UNDEFINED ) { s = s + c; offscreenImageG.drawString( s, x+scrollbarH_value, y +scrollbarV_value); repaint(); e.consume(); } message.setText("Typing..."); }
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
//****MouseListener methods
public void mouseClicked(MouseEvent e){ // called after a press and release of a mouse button // with no motion in between // (If the user presses, drags, and then releases, there will be // no click event generated.) x = e.getX(); y = e.getY(); s = ""; offscreenImageG.drawString( s, x+scrollbarH_value, y +scrollbarV_value); repaint(); e.consume(); message.setText("Mouse clicked"); } //end mouseClicked
public void mousePressed(MouseEvent e){ //called after a mouse button is pressesd; isButtonPressed=true; message.setText("Mouse Pressed."); }
public void mouseReleased(MouseEvent e) { isButtonPressed=false;
try { skt = new Socket("localhost", 9000); oos = new ObjectOutputStream(skt.getOutputStream()); oos.writeObject(ht); oos.close(); ht.clear(); } catch (Exception x) { x.printStackTrace(); }
message.setText("Mouse released."); }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
//*****MouseMotionListener methods
public void mouseDragged(MouseEvent e){ if(isButtonPressed==true) { pre_x=e.getX()+scrollbarH_value; pre_y=e.getY()+scrollbarV_value; isButtonPressed=false; } x=e.getX()+scrollbarH_value; y=e.getY()+scrollbarV_value; offscreenImageG.drawLine(pre_x, pre_y, x, y); drawLines(pre_x + ":" + pre_y + ":" + x + ":" + y); pre_x=x; pre_y=y; repaint(); e.consume(); message.setText(pre_x + ":" + pre_y + ":" + x + ":" + y); } //end mouseDragged
public void drawLines(String m) { ht.add(m); }
public void GandhiUpdate2() { try { ss2 = new ServerSocket(8000); cs2 = ss2.accept();
while(true) { ois2 = new ObjectInputStream(cs2.getInputStream()); ht2 = (Vector)ois2.readObject(); //System.out.println(ht2); ois2.close(); cs2.close(); cs2= ss2.accept(); repaint(); } } catch (Exception e) { e.printStackTrace(); } }
public void mouseMoved(MouseEvent e) { message.setText("x: "+e.getX()+", y: "+e.getY()); }
//**** WindowListener methods public void windowActivated ( WindowEvent e ) { } public void windowDeactivated ( WindowEvent e ) { } public void windowOpened ( WindowEvent e ) { }
public void windowClosed ( WindowEvent e ) { }
public void windowClosing ( WindowEvent e ) { outerBox.setVisible ( false ); outerBox.dispose (); }
public void windowIconified ( WindowEvent e ) { }
public void windowDeiconified ( WindowEvent e ) { }
}//end WhiteBoardApplet
GandhiServer2.java ----------------------------
import java.net.*; import java.util.*; import java.io.*; public class GandhiServer2 { ServerSocket ss; Socket cs; Vector ht; ObjectInputStream ois;
Socket skt; ObjectOutputStream oos;
public GandhiServer2() { try { ss = new ServerSocket(9000); cs = ss.accept();
while(true) { ois = new ObjectInputStream(cs.getInputStream()); ht = (Vector)ois.readObject();
if(!ht.isEmpty()) { try { skt = new Socket("localhost", 8000); oos = new ObjectOutputStream(skt.getOutputStream()); oos.writeObject(ht); oos.close(); } catch (Exception e) { e.printStackTrace(); } }
System.out.println(ht);
ois.close(); ht.clear(); cs.close(); cs = ss.accept(); }
} catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { new GandhiServer2(); } }
The code above is a client server application. What the client does is that it sends the coordinates of what is drawn on a canvas to the server. The server will receive this coordinates and retransmit it to all the clients connected to the server and display it on the canvas. The client server concept work well, however after implementing this concept, I cant see what I have drawn on the canvas, but it does send the coordinates to the server.
How do I solve the problem.
Your help is kindly appreciated.
Regards.
Andrew Thompson - 08 Oct 2007 13:25 GMT >WhiteboardApplet.java (big snip)
>The code above is a client server application. ... Why did you repost the code from msg id: 1191607582.765015.316370@o3g2000hsb.googlegroups.com ?
That is the same code linked here, isn't it? http://groups.google.com/group/comp.lang.java.programmer/msg/bb94d2b9f3369314
That code was better, by the way, in that: - There was no line-wrap, so it probably compiled cleanly. - It did not take up over 400 lines worth of our bandwidth, to transmit the message to us.
>How do I solve the problem. Is that a question? Questions, in English, generally end with a single question mark - to make it easier for the reader to spot them.
 Signature Andrew Thompson http://www.athompson.info/andrew/
Gordon Beaton - 08 Oct 2007 13:41 GMT > The code above is a client server application. What the client does > is that it sends the coordinates of what is drawn on a canvas to the [quoted text clipped - 3 lines] > implementing this concept, I cant see what I have drawn on the > canvas, but it does send the coordinates to the server. At first glance, I see a number of problems with your networking code:
- the server is written to accept only one client at a time - only one client on "localhost" can bind a ServerSocket to port 9000 - you don't need to use two sockets between each client and the server, the socket already provides a channel in each direction. - you should be able to keep the connections open, there is no need to reconnect (twice!) for every message.
Also, it appears that your init() method will never finish.
I suggest you read the Java networking tutorial, and post shorter examples. See if you can make this work as an application, before making an applet.
/gordon
--
solomon13000@gmail.com - 08 Oct 2007 16:24 GMT > > The code above is a client server application. What the client does > > is that it sends the coordinates of what is drawn on a canvas to the [quoted text clipped - 22 lines] > > -- I did the changes as in to use a single connection:
GandhiServer2.java ------------------
import java.net.*; import java.util.*; import java.io.*; public class GandhiServer2 { ServerSocket ss; Socket cs; Vector ht; ObjectInputStream ois;
Socket skt; ObjectOutputStream oos;
public GandhiServer2() { try { ss = new ServerSocket(9000); cs = ss.accept();
while(true) { ois = new ObjectInputStream(cs.getInputStream()); ht = (Vector)ois.readObject();
if(!ht.isEmpty()) { try { oos = new ObjectOutputStream(cs.getOutputStream()); oos.writeObject(ht); oos.close(); } catch (Exception e) { e.printStackTrace(); } }
System.out.println(ht);
ois.close(); ht.clear(); cs.close(); cs = ss.accept(); }
} catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { new GandhiServer2(); } }
WhiteBoardApplet.java ---------------------
I included a portion of the code as the code is to long.
public void mouseReleased(MouseEvent e) { isButtonPressed=false;
try { skt = new Socket("localhost", 9000);
oos = new ObjectOutputStream(skt.getOutputStream()); oos.writeObject(ht);
ois2 = new ObjectInputStream(skt.getInputStream()); ht2 = (Vector)ois2.readObject();
System.out.println(ht2);
oos.close(); ht.clear();
ois2.close(); ht2.clear(); } catch (Exception x) { x.printStackTrace(); }
message.setText("Mouse released."); }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
//*****MouseMotionListener methods
public void mouseDragged(MouseEvent e){ if(isButtonPressed==true) { pre_x=e.getX()+scrollbarH_value; pre_y=e.getY()+scrollbarV_value; isButtonPressed=false; } x=e.getX()+scrollbarH_value; y=e.getY()+scrollbarV_value; offscreenImageG.drawLine(pre_x, pre_y, x, y); drawLines(pre_x + ":" + pre_y + ":" + x + ":" + y); pre_x=x; pre_y=y; repaint(); e.consume(); message.setText(pre_x + ":" + pre_y + ":" + x + ":" + y); } //end mouseDragged
public void drawLines(String m) { ht.add(m); }
Now I open two whiteboards. When I draw one whiteboard-1, the coordinates are send to the server. Now the server receives the coordinates and send's the coordinate back to whiteboard-1. The problem I am facing is it does not return the coordinate to whiteboard-2 as well. How do I solve the problem?
Lew - 08 Oct 2007 16:33 GMT > I did the changes as in to use a single connection: > [quoted text clipped - 16 lines] > public GandhiServer2() > { I notice that you ignored my advice to move the work out of the constructor. This is going to be a serious problem for you; your whole operation depends on the stability of an incompletely-constructed object. Do not do that!
> try > { > ss = new ServerSocket(9000); > cs = ss.accept(); You should move the accept() inside the loop, perhaps even with a for() loop:
for ( Socket cs; (cs = ss.accept()) != null; )
> while(true) > { > ois = new ObjectInputStream(cs.getInputStream()); > ht = (Vector)ois.readObject(); I notice that you ignored my question about why you used Vector instead of a more modern List class.
> if(!ht.isEmpty()) What if ht is null?
> { > try How about we use narrower indentation for Usenet posts?
> { > oos = new > ObjectOutputStream(cs.getOutputStream()); > oos.writeObject(ht); This will write the received object right back to the sender.
> oos.close(); > } [quoted text clipped - 5 lines] > > System.out.println(ht); Why is this statement here?
> ois.close(); > ht.clear(); You're about to throw ht away; why are you clearing it?
> cs.close(); > cs = ss.accept(); [quoted text clipped - 12 lines] > } > } Take that work out of the constructor.
If you ignore the advice you get here, why are you requesting it?
 Signature Lew
Gordon Beaton - 08 Oct 2007 16:48 GMT > Now I open two whiteboards. When I draw one whiteboard-1, the > coordinates are send to the server. Now the server receives the > coordinates and send's the coordinate back to whiteboard-1. The > problem I am facing is it does not return the coordinate to > whiteboard-2 as well. How do I solve the problem? Your client should be making one connection at initialisation, not one connection for every mouse event.
Your server needs to maintain more than one connection. Data it receives on any connection must presumably be sent to all of the others, but as it's currently written it can still handle only one client at a time.
Did you read and understand the Java networking tutorial? It shows how to make a simple server that can handle multiple clients.
The first step to achieving this is to separate the part of the server that accepts incoming connections from the part that actually handles the connected clients.
/gordon
--
solomon13000@gmail.com - 08 Oct 2007 17:44 GMT > > Now I open two whiteboards. When I draw one whiteboard-1, the > > coordinates are send to the server. Now the server receives the [quoted text clipped - 20 lines] > > -- I implemented threads for my server side.
KKMultiServer.java ------------------
import java.net.*; import java.io.*;
public class KKMultiServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true;
try { serverSocket = new ServerSocket(9000); } catch (IOException e) { System.err.println("Could not listen on port: 9000."); System.exit(-1); }
while (listening) new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close(); } }
KKMultiServerThread.java ------------------------
import java.net.*; import java.io.*; import java.util.*;
public class KKMultiServerThread extends Thread { private Socket socket = null; ObjectOutputStream oos1; ObjectInputStream oos2; Vector ht1;
public KKMultiServerThread(Socket socket) { super("KKMultiServerThread"); this.socket = socket; }
public void run() { try { oos1 = new ObjectOutputStream(socket.getOutputStream()); oos2 = new ObjectInputStream(socket.getInputStream());
ht1 = (Vector)oos2.readObject(); oos1.writeObject(ht1); } catch(Exception e) { e.printStackTrace(); } } }
However it does not send the coordinates to all the clients.
Lew - 08 Oct 2007 18:02 GMT >>> Now I open two whiteboards. When I draw one whiteboard-1, the >>> coordinates are send to the server. Now the server receives the [quoted text clipped - 90 lines] > > However it does not send the coordinates to all the clients. As I pointed out earlier today, and Gordon obliquely mentioned, you are doing the "writeObject()" call only to the client that sent the object. Where is the code that would send it to another client? Where is the code that even recognizes the client?
You still haven't answered why you are using Vector instead of, say, ArrayList. You can't need the synchronization feature, can you/
Not that it matters, but it piques my curiosity: why did you keep Sun's names for the server classes? <http://java.sun.com/docs/books/tutorial/networking/sockets/examples/KKMultiServe r.java>
You did put them in named packages, right?
I truly hope you start answering questions.
 Signature Lew
solomon13000@gmail.com - 08 Oct 2007 18:07 GMT > solomon13...@gmail.com wrote: > >>> Now I open two whiteboards. When I draw one whiteboard-1, the [quoted text clipped - 112 lines] > > - Show quoted text - I am more into programming using microsoft products and im trying my level best to learn java. The reason why im using vector is because I have seen many books using it as a sample. I would use ArrayList once I capture the concept of programming in Java.
Lew - 08 Oct 2007 18:22 GMT > I am more into programming using microsoft products and im trying my > level best to learn java. The reason why im using vector is because I > have seen many books using it as a sample. I would use ArrayList once > I capture the concept of programming in Java. Vector has been out of date since about 1999.
It has a feature you will not need - every method of Vector is synchronized.
You should use ArrayList.
 Signature Lew
Andrew Thompson - 08 Oct 2007 18:33 GMT >> solomon13...@gmail.com wrote: >>> Lew ...(trim over 100 lines)
>> > However it does not send the coordinates to all the clients.
>> As I pointed out earlier today, and Gordon obliquely mentioned, you are doing >> the "writeObject()" call only to the client that sent the object. Where is >> the code that would send it to another client? Where is the code that even >> recognizes the client?
>> You still haven't answered why you are using Vector instead of, say, >> ArrayList. You can't need the synchronization feature, can you/
>> Not that it matters, but it piques my curiosity: why did you keep Sun's names >> for the server classes? >> <http://java.sun.com/docs/books/tutorial/networking/sockets/examples/K...>
>> You did put them in named packages, right?
>> I truly hope you start answering questions.
>I am more into programming using microsoft products and im trying my >level best to learn java. While doing that, please learn how to post "in-line, with trimming", as opposed to bottom (or top) posting replies. Hopefully this will help you to identify questions, because..
>..The reason why im using vector is because I >have seen many books using it as a sample. I would use ArrayList once >I capture the concept of programming in Java. ..of the fours questions Lew asked, I see just one answer, the answer to 'Why use Vector?'.
I also asked you a question up-thread for which I am hoping to get an answer. Why repost the question?
Getting information from you seems as difficult as pulling teeth from a hen.
Andrew T.
solomon13000@gmail.com - 08 Oct 2007 20:51 GMT > >> solomon13...@gmail.com wrote: > >>> Lew [quoted text clipped - 36 lines] > > Andrew T. Due to the nature of the question (different), I decided to repost.
Lew - 08 Oct 2007 21:51 GMT > Due to the nature of the question (different), I decided to repost. As long as you have, I'm still curious about my earlier questions:
> ... you are doing the "writeObject()" call only to the client that sent the object. > Where is the code that would send it to another client? > Where is the code that even recognizes the [other] client? ...
> Not that it matters, but it piques my curiosity: why did you keep Sun's names for the server classes? > <http://java.sun.com/docs/books/tutorial/networking/sockets/examples/KKMultiServe r.java> > > You did put them in named packages, right?
 Signature Lew
solomon13000@gmail.com - 09 Oct 2007 02:04 GMT > solomon13...@gmail.com wrote: > > Due to the nature of the question (different), I decided to repost. [quoted text clipped - 12 lines] > -- > Lew I assume that the two questions you have asked is a solution to my problem. Since I know the path therefore I will work out the solution.
Lew - 09 Oct 2007 02:53 GMT Lew wrote:
>> I'm still curious about my earlier questions: >> [quoted text clipped - 5 lines] >>> <http://java.sun.com/docs/books/tutorial/networking/sockets/examples/K...> >>> You did put them in named packages, right?
> I assume that the two questions you have asked is a solution to my > problem. Since I know the path therefore I will work out the solution. I'm not sure how you could have concluded that, or why you would assume it against the evidence. To begin with, I asked four questions, not two.
1. Where is the code that would send it to another client? 2. Where is the code that even recognizes the [other] client? 3. Not that it matters, but it piques my curiosity: why did you keep Sun's names for the server classes? 4. You did put them [the Knock-Knock classes] in named packages, right?
Second, people usually ask questions because they want answers. Third, I said, "I'm still curious".
It is, of course, your right not to answer, but it seems strange. Especially so because you come here asking for help, but won't contribute to the conversation when people ask you questions in return. You also didn't respond to certain earlier questions or advice, such as to remove work from the class constructor.
I'm going to guess that the answers to #1 and #2 were, "There isn't any and there isn't any, and gosh, that's where the problem lies (in part)." I'm going to suggest that the answer to #4 should be, "Yes." As to #3, as I said, it doesn't really matter.
Good luck in your future endeavors.
 Signature Lew
solomon13000@gmail.com - 09 Oct 2007 06:02 GMT > Lew wrote: > >> I'm still curious about my earlier questions: [quoted text clipped - 37 lines] > -- > Lew The solution has been found and I am able to broadcast to multiple clients from the server. This was achieve using thread and synchronized.
Lew - 09 Oct 2007 13:52 GMT > The solution has been found and I am able to broadcast to multiple > clients from the server. This was achieve using thread and > synchronized. And thank you so much for your responsiveness and your willingness to share information.
 Signature Lew
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|