Recently I have made a simple chatting application(in Java).
It has two classes one for client and one for server.On compiling it is not giving any error but on running, it is not giving desired result.
So, It will be grateful if you come ahead and try out helping me in any way , solving/correcting my program
Client class is:
package com;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ChatClient {
JTextArea in;
JTextField out;
JButton button;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String ar[]) {
ChatClient client = new ChatClient();
client.go();
}
public void go() {
JFrame frame = new JFrame("Chat Application");
JPanel panel = new JPanel();
in=new JTextArea(15,50);
in.setLineWrap(true);
in.setWrapStyleWord(true);
in.setEditable(false);
JScrollPane scroller = new JScrollPane(in);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
out=new JTextField(20);
button=new JButton("SEND");
button.addActionListener(new ButtonListener());
panel.add(scroller);
panel.add(out);
panel.add(button);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,500);
frame.setVisible(true);
}
public void setUpNetworking() {
try {
sock = new Socket("127.0.0.1",5000);
InputStreamReader stream = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(stream);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Network Established");
}
catch(IOException e) {
e.printStackTrace();
}
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
try {
writer.println(out.getText());
writer.flush();
}
catch(Exception e) {
e.printStackTrace();
}
out.setText(" ");
out.requestFocus();
}
}
public class IncomingReader implements Runnable {
public void run() {
String message;
try {
while((message=reader.readLine())!=null) {
System.out.println("raed "+message);
in.append(message+ "\n");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
Server class is:
package com;
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
ArrayList clientOutputStream;
public static void main(String ar[]) {
new ChatServer().go();
}
public void go() {
clientOutputStream = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter (clientSocket.getOutputStream());
clientOutputStream.add(writer);
Thread t = new Thread (new ClientHandler(clientSocket));
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket) {
try {
sock=clientSocket;
InputStreamReader stream = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(stream);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
String message;
try {
while((message=reader.readLine())!=null) {
System.out.println("read "+message);
tellEveryOne(message);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public void tellEveryOne(String message) {
Iterator it = clientOutputStream.iterator();
while(it.hasNext()) {
try {
PrintWriter writer = (PrintWriter)it.next();
writer.println(message);
writer.flush();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
}