I am attempting to write a very simple chat program to test out ServerSockets and Sockets, the code should work, but when the client make a connection to the server it does not move past the accept method. I know they connected because as soon as I close the client window it says it connected and then gives me errors that it cannot communicate with the client.
Server
package network;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Server {
private ServerSocket ss;
private Socket clientSock;
private BufferedReader read;
private PrintWriter write;
private String text = "";
private JTextArea conversation;
private String nl = "\n";
private JTextField messageField;
public Server(){
createSocket();
JFrame f = new JFrame("Server");
f.getContentPane().setPreferredSize(new Dimension(500,300));
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
conversation = new JTextArea(50,10);
conversation.setEditable(false);
JScrollPane convoPane = new JScrollPane(conversation);
JPanel bottomLayout = new JPanel();
JButton sendButton = new JButton ("Send");
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
writeMessage();
}
});
messageField = new JTextField (39);
bottomLayout.add(messageField);
bottomLayout.add(sendButton);
f.add(convoPane, BorderLayout.CENTER);
f.add(bottomLayout, BorderLayout.SOUTH);
f.setVisible(true);
connect();
chatProgram();
}
protected void writeMessage() {
// TODO Auto-generated method stub
String w = messageField.getText();
if (w != null){
//write.write(w);
text += w + nl;
}
}
private void chatProgram() {
// TODO Auto-generated method stub
while (clientSock.isConnected()){
read();
conversation.setText(text);
}
closeSockets();
}
private void read() {
// TODO Auto-generated method stub
try {
String r = read.readLine();
if (r != null){
text += r + nl;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void closeSockets() {
// TODO Auto-generated method stub
try {
read.close();
write.close();
ss.close();
clientSock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void connect() {
// TODO Auto-generated method stub
try {
text += "Waiting for Connection..." + nl;
conversation.setText(text);
clientSock = ss.accept();
text += "Client Connected" + nl;
write = new PrintWriter(clientSock.getOutputStream(), true);
read = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createSocket() {
// TODO Auto-generated method stub
int port = 0;
while(port == 0){
try{
port = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Port"));
}catch(java.lang.NumberFormatException e){
JOptionPane.showMessageDialog(null, "Invalid Port Number");
}
}
try {
ss = new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
new Server();
}
}
and the Client
package network;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client {
private Socket sock;
private JTextArea conversation;
private JTextField messageField;
private String text;
private BufferedReader read;
private PrintWriter write;
private String nl = "\n";
public Client(){
createSocket();
JFrame f = new JFrame("Client");
f.getContentPane().setPreferredSize(new Dimension(500,300));
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
conversation = new JTextArea(50,10);
conversation.setEditable(false);
JScrollPane convoPane = new JScrollPane(conversation);
JPanel bottomLayout = new JPanel();
JButton sendButton = new JButton ("Send");
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
writeMessage();
}
});
messageField = new JTextField (39);
bottomLayout.add(messageField);
bottomLayout.add(sendButton);
f.add(convoPane, BorderLayout.CENTER);
f.add(bottomLayout, BorderLayout.SOUTH);
f.setVisible(true);
connect();
chatProgram();
}
protected void writeMessage() {
// TODO Auto-generated method stub
String t = messageField.getText();
if (t != null){
write.write(t);
text += t + nl;
}
}
private void chatProgram() {
// TODO Auto-generated method stub
while(sock.isConnected()){
try {
String t = read.readLine();
if (t != null){
text += t + nl;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conversation.setText(text);
}
closeSockets();
}
private void closeSockets() {
// TODO Auto-generated method stub
try {
write.close();
read.close();
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void connect(){
try {
write = new PrintWriter(sock.getOutputStream());
read = new BufferedReader(new InputStreamReader(sock.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createSocket(){
String ip = JOptionPane.showInputDialog(null, "Enter the IP Address");
int port = Integer.parseInt(JOptionPane.showInputDialog("Enter the port"));
try {
sock = new Socket(ip, port);
sock.bind(InetSocketAddress.createUnresolved(ip, port));
//sock.connect(InetSocketAddress.createUnresolved(ip, port));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}
What could be creating the problem that I am having?
Thank you for your help.