You are on page 1of 6

2 WAY CHAT

Server program (FrameServer2.java)


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameServer2 extends JFrame implements ActionListener
{
JLabel answer = new JLabel("Message received");
JPanel pane = new JPanel(); // create panel object
JTextArea t1= new JTextArea("",10,25);
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String msg=null;
JButton send = new JButton("Send msg");
JTextField t2= new JTextField(15);

FrameServer2() // the constructor


{
super("2 way chat server");
setBounds(100,100,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane);
pane.add(answer);
pane.add(t1);
pane.add(t2);
pane.add(send);
send.addActionListener(this); // register button listener
setVisible(true); // make frame visible

try {
ServerSocket ss= new ServerSocket(8000);
echoSocket = ss.accept();
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));

} catch (UnknownHostException e) {
System.err.println("Don't know about host.");
System.exit(1);
}
catch (Exception e) {
System.err.println("error 1");
System.exit(1);
}
try{
do{
msg=in.readLine();
t1.append(msg+'\n');
} while (!msg.equals("quit")) ;
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O.");
System.exit(1);
}
catch (Exception e) {
System.err.println("error 2");
System.exit(1);
}

}///////////////////end of constructor
public void actionPerformed(ActionEvent event)
{
String userInput;
try {
userInput = t2.getText();
out.println(userInput);
t2.setText("");
t1.append("Sent:"+userInput+'\n');
} catch (Exception e) {
System.err.println("Couldn't get I/O.");
System.exit(1);
}

}/////////////////// end of action performed

public static void main(String args[]) {new FrameServer2();}

}///////////////// end of class

Client Program (FrameClient2.java)


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameClient2 extends JFrame implements ActionListener
{
JLabel answer = new JLabel("Messages received");
JPanel pane = new JPanel(); // create pane object
JTextArea t1= new JTextArea("",10,25);
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
String msg=null;
JButton send = new JButton("Send msg");
JTextField t2= new JTextField(15);

FrameClient2() // the constructor


{
super("2 Way chat client");
setBounds(400,100,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane);
send.addActionListener(this); // register button listener
pane.add(answer);
pane.add(t1);
pane.add(t2);
pane.add(send);

try {
echoSocket = new Socket("localhost", 8000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));

} catch (UnknownHostException e) {
System.err.println("Don't know about host.");
System.exit(1);
} catch (Exception e) {
System.err.println("error in client object ");
System.exit(1);
}

setVisible(true); // make frame visible

try{
do{
msg=in.readLine();
//if(msg==null) continue;
t1.append("Server:"+msg+'\n');
} while (!msg.equals("quit")) ;
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: taranis.");
System.exit(1);
}
catch (Exception e) {
System.err.println("error 2");
System.exit(1);
}
}// end of constructor

///////////////// here is the basic event handler


public void actionPerformed(ActionEvent event)
{
String userInput;
try {
userInput = t2.getText();
out.println(userInput);
t2.setText("");
t1.append("sent:"+userInput+'\n');
} catch (Exception e) {
System.err.println("Error in client output");
System.exit(1);
}

}/////////////// end of action performed

public static void main(String args[]) {new FrameClient2();}

}/////////////// end of class

OUTPUT