Echo Server with GUI in JAVA :
Client Program:
client.java
______________________________________________________________________________
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class client extends JFrame {
JTextArea clientText;
JTextField msg;
JPanel clip;
JScrollPane clientScroll;
JButton closeButton = new JButton("Close");
JButton send = new JButton("Send");
JLabel label1 = new JLabel("Echo Client - Multithreaded");
Container cont;
client() {
cont = getContentPane();
setSize(250, 500);
setTitle("GUI Client");
clip = new JPanel();
msg = new JTextField(20);
clip.setLayout(new FlowLayout(FlowLayout.CENTER));
clientText = new JTextArea(20, 20);
clientScroll = new JScrollPane(clientText);
clip.add(label1);
clip.add(clientText);
clip.add(msg);
clip.add(send);
clip.add(closeButton);
cont.add(clip);
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Socket s = null;
try {
s = new Socket("localhost", 12111);
} catch (UnknownHostException uhe) {
clientText.setText("UnknownHost: + localhost");
s = null;
} catch (IOException ioe) {
clientText.setText("Cant connect to server at 12111.Make sure it is running");
s = null;
}
if (s == null) {
System.exit(-1);
}
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println(msg.getText());
out.flush();
String temp = clientText.getText();
if (temp.equalsIgnoreCase("quit")) {
System.exit(0);
}
msg.setText("");
clientText.append("\nServer says:" + in.readLine());
} catch (IOException ioe) {
clientText.setText("Exception during communication.Server probably closed connection.");
} finally {
try {
out.close();
in.close();
s.close();
} catch (Exception es) {
es.printStackTrace();
}
}
}
});
closeButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
client clientFrame = new client();
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientFrame.setVisible(true);
}
}
______________________________________________________________________________
______________________________________________________________________________
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class client extends JFrame {
JTextArea clientText;
JTextField msg;
JPanel clip;
JScrollPane clientScroll;
JButton closeButton = new JButton("Close");
JButton send = new JButton("Send");
JLabel label1 = new JLabel("Echo Client - Multithreaded");
Container cont;
client() {
cont = getContentPane();
setSize(250, 500);
setTitle("GUI Client");
clip = new JPanel();
msg = new JTextField(20);
clip.setLayout(new FlowLayout(FlowLayout.CENTER));
clientText = new JTextArea(20, 20);
clientScroll = new JScrollPane(clientText);
clip.add(label1);
clip.add(clientText);
clip.add(msg);
clip.add(send);
clip.add(closeButton);
cont.add(clip);
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Socket s = null;
try {
s = new Socket("localhost", 12111);
} catch (UnknownHostException uhe) {
clientText.setText("UnknownHost: + localhost");
s = null;
} catch (IOException ioe) {
clientText.setText("Cant connect to server at 12111.Make sure it is running");
s = null;
}
if (s == null) {
System.exit(-1);
}
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
out.println(msg.getText());
out.flush();
String temp = clientText.getText();
if (temp.equalsIgnoreCase("quit")) {
System.exit(0);
}
msg.setText("");
clientText.append("\nServer says:" + in.readLine());
} catch (IOException ioe) {
clientText.setText("Exception during communication.Server probably closed connection.");
} finally {
try {
out.close();
in.close();
s.close();
} catch (Exception es) {
es.printStackTrace();
}
}
}
});
closeButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
client clientFrame = new client();
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientFrame.setVisible(true);
}
}
______________________________________________________________________________
Server Program:
EchoServer.java
______________________________________________________________________________
import java.net.*;
import java.io.*;
public class EchoServer {
ServerSocket m_ServerSocket;
public EchoServer() {
try {
m_ServerSocket = new ServerSocket(12111);
} catch (IOException ioe) {
System.out.println("could not create server socket at 12111 Quitting");
System.exit(-1);
}
System.out.println("Listening for clients on 12111....");
int id = 0;
while (true) {
try {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
} catch (IOException ioe) {
System.out.println("Exception encountered on accept.Ignoring.StackTrace :");
ioe.printStackTrace();
}
}
}
public static void main(String args[]) {
new EchoServer();
}
class ClientServiceThread extends Thread {
Socket m_clientSocket;
int m_clientID = -1;
boolean m_bRunThread = true;
ClientServiceThread(Socket s, int clientID) {
m_clientSocket = s;
m_clientID = clientID;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
System.out.println("Accepted Client :ID - " + m_clientID + " :Address - " + m_clientSocket.getInetAddress().getHostName());
try {
in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));
while (m_bRunThread) {
String clientCommand = in.readLine();
System.out.println("Client says :" + clientCommand);
if (clientCommand.equalsIgnoreCase("quit")) {
m_bRunThread = false;
System.out.println("Stopping client thread for client :" + m_clientID);
System.exit(0);
} else {
out.println(clientCommand);
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
m_clientSocket.close();
System.out.println("...STOPPED");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
______________________________________________________________________________
No comments:
Post a Comment