0% found this document useful (0 votes)
108 views6 pages

Server Program (Frameserver2.Java) : 2 Way Chat

This document contains code for a 2-way chat client and server program written in Java. The server program creates a socket that listens for incoming connections on port 8000. It displays messages received from the client in a text area and allows the user to type and send responses. The client program connects to the server socket, displays messages from the server, and allows the user to type and send messages to the server. When either side types "quit", the connection closes.

Uploaded by

Abhiram Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
108 views6 pages

Server Program (Frameserver2.Java) : 2 Way Chat

This document contains code for a 2-way chat client and server program written in Java. The server program creates a socket that listens for incoming connections on port 8000. It displays messages received from the client in a text area and allows the user to type and send responses. The client program connects to the server socket, displays messages from the server, and allows the user to type and send messages to the server. When either side types "quit", the connection closes.

Uploaded by

Abhiram Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy