hi, i have a problem with my client/server chat gui application. i have the client and server talking back to one another through the gui's and the text that is entered is displayed when it is sent. I have to 2 textfields in each frame, one for sent and then one for receive. when the client sends text to the server i want the client to see what they have sent in the receive textfield within the client frame. i also want this function on the server side as well i know you can append the textfield but i want to design a new thread for this. im using just one thread in my client and server code but i want to use another one, maybe design a new class and a new thread within this.
>>> could u help me out with some ex code
>>> i have included code this time!
import java.awt.* ;
import java.awt.event.*;
import java.io.* ;
import java.net.* ;
import java.util.* ;
public class ChatAppA extends Frame implements ActionListener, WindowListener
{
ServerSocket ss;
Socket s;
PrintWriter pw;
BufferedReader br;
private Panel p1 = new Panel();
private Label L1 = new Label("Send"
private TextArea t1 = new TextArea(5,40);
private Button b1 = new Button("Send"
private Panel p2 = new Panel();
private Label L2 = new Label("Receive"
private Label L3 = new Label("____________________________________________________________"
private TextArea t2 = new TextArea(5,40);
private Button b2 = new Button("Clear"
private Font f = new Font("TimesRoman", Font.BOLD, 14);
public ChatAppA(String str)
{
super(str);
setLayout(new FlowLayout());
setSize(500,400);
add(p1);
add(L1);
L1.setFont(f);
add(t1);
add(b1);
add(p2);
add(L3);
add(L2);
L2.setFont(f);
add(t2);
add(b2);
p1.setBackground(Color.white);
b1.addActionListener(this);
b2.addActionListener(this);
this.addWindowListener(this);
setBackground(Color.white);
setVisible(true);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();
if(target == b2)
{
t2.setText(""
}
if(target == b1)
{
String line = t1.getText();
pw.write();
pw.flush();
t1.setText(""
try
{
InetAddress inet = InetAddress.getByName("localhost" ;
try
{
s = new Socket("127.0.0.1", 1053);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
Thread th;
th = new Thread(this);
th.start();
Socket s = new Socket(inet, 2000) ;
OutputStream o = s.getOutputStream() ;
PrintWriter p = new PrintWriter(o) ;
p.println(""
p.println(""
p.close() ;
}
catch (IOException a)
{
System.out.println("Failed connection to server" ;
}
}
catch (UnknownHostException a)
{
System.out.println("Unknown Host " ;
a.printStackTrace();
}
}
}
public static void main(String args[])
{
ChatAppA CA = new ChatAppA("User A"
}
}
>>> could u help me out with some ex code
>>> i have included code this time!
import java.awt.* ;
import java.awt.event.*;
import java.io.* ;
import java.net.* ;
import java.util.* ;
public class ChatAppA extends Frame implements ActionListener, WindowListener
{
ServerSocket ss;
Socket s;
PrintWriter pw;
BufferedReader br;
private Panel p1 = new Panel();
private Label L1 = new Label("Send"
private TextArea t1 = new TextArea(5,40);
private Button b1 = new Button("Send"
private Panel p2 = new Panel();
private Label L2 = new Label("Receive"
private Label L3 = new Label("____________________________________________________________"
private TextArea t2 = new TextArea(5,40);
private Button b2 = new Button("Clear"
private Font f = new Font("TimesRoman", Font.BOLD, 14);
public ChatAppA(String str)
{
super(str);
setLayout(new FlowLayout());
setSize(500,400);
add(p1);
add(L1);
L1.setFont(f);
add(t1);
add(b1);
add(p2);
add(L3);
add(L2);
L2.setFont(f);
add(t2);
add(b2);
p1.setBackground(Color.white);
b1.addActionListener(this);
b2.addActionListener(this);
this.addWindowListener(this);
setBackground(Color.white);
setVisible(true);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void actionPerformed(ActionEvent e)
{
Object target = e.getSource();
if(target == b2)
{
t2.setText(""
}
if(target == b1)
{
String line = t1.getText();
pw.write();
pw.flush();
t1.setText(""
try
{
InetAddress inet = InetAddress.getByName("localhost" ;
try
{
s = new Socket("127.0.0.1", 1053);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
Thread th;
th = new Thread(this);
th.start();
Socket s = new Socket(inet, 2000) ;
OutputStream o = s.getOutputStream() ;
PrintWriter p = new PrintWriter(o) ;
p.println(""
p.println(""
p.close() ;
}
catch (IOException a)
{
System.out.println("Failed connection to server" ;
}
}
catch (UnknownHostException a)
{
System.out.println("Unknown Host " ;
a.printStackTrace();
}
}
}
public static void main(String args[])
{
ChatAppA CA = new ChatAppA("User A"
}
}