Need some advice2...

hi, i have this code of mine.. it works as two separate running program..

the one is the server named Strider.java and the other one is Hiryu.java

as the client..

my question is, how can i make the clients, connected to the server,

communicate with each other.. because at this moment, only the server and the client can communicate with one another..

kindly, need an advice or idea...

import java.io.*;

import java.net.*;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

publicclass Striderextends JFrameimplements ActionListener

{

ObjectOutputStream output;

ObjectInputStream input;

String msg;

JTextField txtfld;

JTextArea txtarea;

public Strider()

{

Container c = getContentPane();

c.setLayout(new FlowLayout());

txtfld =new JTextField(10);

txtarea =new JTextArea(10,20);

txtfld.addActionListener(this);

c.add(txtfld);

c.add(new JScrollPane(txtarea));

setSize(230,250);

show();

}

publicvoid actionPerformed(ActionEvent e)

{

sendData(e.getActionCommand());

}

publicvoid runStrider()

{

ServerSocket server;

Socket connection;

int counter = 1;

try{

server =new ServerSocket(5000, 100);

while(true){

txtarea.setText("Waiting for someone to connect");

connection = server.accept();

txtarea.append("Connection" + counter +"received from:" + connection.getInetAddress().getHostName());

output =new ObjectOutputStream(connection.getOutputStream());

output.flush();

input =new ObjectInputStream(connection.getInputStream());

txtarea.append("\n Got I/O Streams \n");

msg ="Server>> Connection Successful";

output.writeObject(msg);

output.flush();

do{

try{

msg = (String) input.readObject();

txtarea.append("\n" + msg);

txtarea.setCaretPosition(txtarea.getText().length());

}

catch(ClassNotFoundException cnfex){

txtarea.append("\nUnknown Object type received");

}

}while(!msg.equals("Client>> Terminate"));

txtarea.append("\nUser Terminated connection");

output.close();

input.close();

connection.close();

++counter;

}

}

catch(EOFException eof){

System.out.println("Client terminated connection");

}

catch(IOException io){

io.printStackTrace();

}

}

publicvoid sendData(String s)

{

try{

output.writeObject("Server>>" + s);

output.flush();

txtarea.append("\nServer>>" + s);

}

catch(IOException cnfex){

txtarea.append("\nError writing Object");

}

}

publicstaticvoid main(String[] args)

{

Strider app =new Strider();

app.addWindowListener(

new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

app.runStrider();

}

}

[b]//Hiryu.java[/b]

import java.io.*;

import java.net.*;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

publicclass Hiryuextends JFrameimplements ActionListener

{

ObjectOutputStream output;

ObjectInputStream input;

String msg;

JTextField txtfld;

JTextArea txtarea;

public Hiryu()

{

Container c = getContentPane();

c.setLayout(new FlowLayout());

txtfld =new JTextField(10);

txtarea =new JTextArea(10,20);

txtfld.addActionListener(this);

c.add(txtfld);

c.add(new JScrollPane(txtarea));

setSize(230,250);

show();

}

publicvoid actionPerformed(ActionEvent e)

{

sendData(e.getActionCommand());

}

publicvoid runClient()

{

Socket client;

try{

txtarea.setText("Attempting Connection\n");

client =new Socket(InetAddress.getByName("127.168.2.109"), 5000);

txtarea.append("Connected to:" + client.getInetAddress().getHostName());

output =new ObjectOutputStream(client.getOutputStream());

output.flush();

input =new ObjectInputStream(client.getInputStream());

txtarea.append("\n Got I/O Streams \n");

msg ="Server>> Connection Successful";

output.writeObject(msg);

output.flush();

do{

try{

msg = (String) input.readObject();

txtarea.append("\n" + msg);

txtarea.setCaretPosition(txtarea.getText().length());

}

catch(ClassNotFoundException cnfex){

txtarea.append("\nUnknown Object type received");

}

}while(!msg.equals("Server>> Terminate"));

txtarea.append("\nUser Terminated connection");

output.close();

input.close();

client.close();

//++counter;

}

catch(EOFException eof){

System.out.println("Client terminated connection");

}

catch(IOException io){

io.printStackTrace();

}

}

publicvoid sendData(String s)

{

try{

output.writeObject("Client>>" + s);

output.flush();

txtarea.append("\nClient>>" + s);

}

catch(IOException cnfex){

txtarea.append("\nError writing Object");

}

}

publicstaticvoid main(String[] args)

{

Hiryu app =new Hiryu();

app.addWindowListener(

new WindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

app.runClient();

}

}

[10898 byte] By [strider_hiryu007a] at [2007-11-27 8:17:22]
# 1

Here is my two cents.

You can do this two ways:

1) Have them communicate with each other through the server.

2) Have them communicate with each other directly.

Both involve different steps.

1) In order to talk through the server, we need a few things. First each client needs to know the other clients connected to the server. So you need a way that the client can ask the server for all connected clients. Then you need a way for a client to send a message to another client through the server. So you need a client to be able to say "hey server, I'd like to talk to client B", then you can use the server to proxy messages back and forth from those clients.

2) In this case, you again need to pass connected clients to each client, but you will have the client attempt to connect to another client directly. As opposed to going through the server.

Either approach you take, it will take a bit of work to make this happen.

It's not a small modification to your program.

If you need any further help with getting this going, feel free to ask.

bryanoa at 2007-7-12 20:02:32 > top of Java-index,Java Essentials,New To Java...
# 2
yes... kindly.. if they.. coz i don't know where should i start..
strider_hiryu007a at 2007-7-12 20:02:32 > top of Java-index,Java Essentials,New To Java...
# 3

Okay, looking over your code you're going to need to do some multi threading. Because as you've got it written, only one client can connect to the server at a time.

You need to spawn a new thread once a client has connected from the server and then go back to listening to the server socket for incoming connections.

So I'd start there.

I would create an inner class to the server that extends Thread and pass it the connected socket and tell it to start running ( I.E. call the start() method ). Then that thread can communicate back and forth with the client while the server goes back to listeneing for incoming connections.

Once you have that done, you can then go on to the next step.

Post back if you have any questions about that.

bryanoa at 2007-7-12 20:02:32 > top of Java-index,Java Essentials,New To Java...
# 4
thank you sir... for the idea.. it really helps me a lot. thank you again
strider_hiryu007a at 2007-7-12 20:02:32 > top of Java-index,Java Essentials,New To Java...