I am beginner in Java and programming a chat-server application. The application facilitates chat among n number of clients
Current Problem
In my code below the Chatinterface class
is the client class. After I open my 2 instances of Chatinterface
class, when I try to communicate by entering a message in the text area. I do not receive the message in the another client instance window and vice versa. Either my socket is not communicating or there is some other problem.
Expected Output
Both the clients must communicate and the messages must be displayed in the respective window.
Could any one tell me what's going on wrong and where can I correct my code ?
Anyways I am pasting both my client and server code for a detailed analysis.
package stack;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
class Chatinterface implements ActionListener {
private JFrame frame;
JTextArea textArea_1;
JTextArea textArea_2;
JButton btnNewButton;
JButton btnNewButton_1;
Socket client;
BufferedReader read;
PrintWriter write;
String getmsg;
String gettext;
public Chatinterface(){
communicate();
displaygui();
}
private void communicate() {
// TODO Auto-generated method stub
try {
client = new Socket("localhost",3420);
read = new BufferedReader(new InputStreamReader(client.getInputStream()));
write = new PrintWriter(client.getOutputStream(),true);
if(getmsg!=null)
{
while(true)
{
textArea_2.setText(getmsg);
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void displaygui() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);
textArea_1 = new JTextArea();
springLayout.putConstraint(SpringLayout.NORTH, textArea_1, 171, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, textArea_1, 10, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textArea_1, -10, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textArea_1, 340, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(textArea_1);
textArea_2 = new JTextArea();
textArea_2.setWrapStyleWord(true);
textArea_2.setEditable(false);
springLayout.putConstraint(SpringLayout.NORTH, textArea_2, 10, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, textArea_2, 0, SpringLayout.WEST, textArea_1);
springLayout.putConstraint(SpringLayout.SOUTH, textArea_2, 160, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textArea_2, 0, SpringLayout.EAST, textArea_1);
frame.getContentPane().add(textArea_2);
btnNewButton = new JButton("Send");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, -76, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 5, SpringLayout.EAST, textArea_1);
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton, -27, SpringLayout.SOUTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnNewButton, 82, SpringLayout.EAST, textArea_1);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(this);
btnNewButton.setActionCommand("S");
btnNewButton_1 = new JButton("Logout");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton_1, 50, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, btnNewButton_1, -65, SpringLayout.EAST, btnNewButton);
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton_1, 108, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_1, 0, SpringLayout.EAST, btnNewButton);
frame.getContentPane().add(btnNewButton_1);
btnNewButton_1.addActionListener(this);
btnNewButton_1.setActionCommand("L");
frame.setVisible(true);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Chatinterface();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("S"))
{
String gettext2;
gettext2 = textArea_1.getText();
textArea_2.append(gettext2);
textArea_1.setText("");
write.println(gettext2);
}
if(arg0.getActionCommand().equals("L")){
try {
read.close();
write.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Here is the code for the server class
package stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
public class Server1 {
public static void main(String[] args) throws Exception {
Socket s;
ServerSocket server = new ServerSocket(3420);
while(true)
{
s = server.accept();
Handler handler1 = new Handler(s);
Thread t1 = new Thread(handler1);
t1.start();
}
}
}
Here is the handler class
package stack;
import java.io.*;
import java.net.*;
public class Handler implements Runnable {
Socket s;
String getmsg = null;
String sendmsg;
public Handler(Socket s) {
this.s = s;
}
public void run() {
try {
BufferedReader reader1;
PrintWriter writer1;
reader1 = new BufferedReader(new InputStreamReader(
s.getInputStream()));
writer1 = new PrintWriter(s.getOutputStream(), true);
sendmsg = reader1.readLine();
while (true) {
if (reader1 == null) {
s.close();
} else {
writer1.println(sendmsg);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}