Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > client server - client problem

Reply
Thread Tools

client server - client problem

 
 
solomon13000@gmail.com
Guest
Posts: n/a
 
      10-08-2007
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.

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-08-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>WhiteboardApplet.java

(big snip)

>The code above is a client server application. ...


Why did you repost the code from msg id:
(E-Mail Removed) om
?

That is the same code linked here, isn't it?
http://groups.google.com/group/comp....94d2b9f3369314
>


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.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200710/1

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-08-2007
On Mon, 08 Oct 2007 04:43:50 -0700, (E-Mail Removed) wrote:
> 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.


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

--
 
Reply With Quote
 
solomon13000@gmail.com
Guest
Posts: n/a
 
      10-08-2007
On Oct 8, 8:41 pm, Gordon Beaton <(E-Mail Removed)> wrote:
> On Mon, 08 Oct 2007 04:43:50 -0700, (E-Mail Removed) wrote:
> > 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.

>
> 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
>
> --


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?

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-08-2007
(E-Mail Removed) wrote:
> 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()
> {


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();
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> }
>
> 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();
> }
>
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args)
> {
> new GandhiServer2();
> }
> }


Take that work out of the constructor.

If you ignore the advice you get here, why are you requesting it?

--
Lew
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-08-2007
On Mon, 08 Oct 2007 08:24:08 -0700, (E-Mail Removed) wrote:
> 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

--
 
Reply With Quote
 
solomon13000@gmail.com
Guest
Posts: n/a
 
      10-08-2007
On Oct 8, 11:48 pm, Gordon Beaton <(E-Mail Removed)> wrote:
> On Mon, 08 Oct 2007 08:24:08 -0700, (E-Mail Removed) wrote:
> > 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
>
> --


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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-08-2007
(E-Mail Removed) wrote:
> On Oct 8, 11:48 pm, Gordon Beaton <(E-Mail Removed)> wrote:
>> On Mon, 08 Oct 2007 08:24:08 -0700, (E-Mail Removed) wrote:
>>> 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
>>
>> --

>
> 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.


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/KKMultiServer.java>

You did put them in named packages, right?

I truly hope you start answering questions.

--
Lew
 
Reply With Quote
 
solomon13000@gmail.com
Guest
Posts: n/a
 
      10-08-2007
On Oct 9, 1:02 am, Lew <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Oct 8, 11:48 pm, Gordon Beaton <(E-Mail Removed)> wrote:
> >> On Mon, 08 Oct 2007 08:24:08 -0700, (E-Mail Removed) wrote:
> >>> 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

>
> >> --

>
> > 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.

>
> 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.
>
> --
> Lew- Hide quoted text -
>
> - 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-08-2007
(E-Mail Removed) wrote:
> 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.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
client/server smart client recommendation? Chris Bordeman ASP .Net 2 09-02-2007 09:00 AM
Blocking client and NIO server (Write back to the client issue) brieuc Java 5 03-20-2007 07:25 AM
Browse client Directory to a specific folder and ftp the file from server to client bindu_conacle@yahoo.com ASP .Net 1 02-26-2006 11:34 AM
Help! How server side client to access the client's variable information ? ABC ASP .Net 4 11-10-2005 03:07 AM
Server to server = Server client to server? - Java 2 07-29-2005 10:46 PM



Advertisments