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();
}
}

