Hi there all
I would like some help with my client and server program that uses gui to chat with each other. The program works fine the first time but somehow when i try to send a second message nothing gets displayed and the person object is empty. I dont understand the client and server that well to really figure out what the problem is,but i think it's when i am creating a new person object for another message,something i am doing wrong there,but cant figure it out.
here is the code for my client and server classes. Any help will be appreciated as i am writing exams soon and must get past this step to continue with file reading from the server.
Thanx in advance
/**
* @(#)ClientApp.java
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClientApp extends JFrame implements ActionListener
{
private Socket server;
private JTextField txtName,txtAge,txtMessage;
private JLabel lblName,lblAge,lblMessage;
private JButton btnSend,btnExit;
private ObjectOutputStream out;
private ObjectInputStream in;
private Person p;
/** Creates a new instance of ClientApp */
public ClientApp()
{
super("Client");
txtName = new JTextField(10);
txtAge = new JTextField(10);
txtMessage = new JTextField(10);
lblName = new JLabel("Enter Name");
lblAge = new JLabel("Enter Age");
lblMessage = new JLabel("Enter Message");
btnSend = new JButton("Send");
btnSend.addActionListener(this);
btnExit = new JButton("Exit");
btnExit.addActionListener(this);
this.setLayout(new GridLayout(4,1,10,10));
this.add(lblName);
this.add(txtName);
this.add(lblAge);
this.add(txtAge);
this.add(lblMessage);
this.add(txtMessage);
this.add(btnSend);
this.add(btnExit);
this.pack();
}//end of constructor
public void runClient()
{
try
{
makeConnection();
getStreams();
processServer();
}
catch ( EOFException eofException )
{
JOptionPane.showMessageDialog(this,"End of file","Error",JOptionPane.ERROR_MESSAGE);
} // end catch
catch ( IOException ioException )
{
JOptionPane.showMessageDialog(this,"Error reading files","File Error",JOptionPane.ERROR_MESSAGE);
} // end catch
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unknown error","Error",JOptionPane.ERROR_MESSAGE);
}
finally
{
closeConnections(); // close connection
dispose();
} // end finally
}
public void makeConnection() throws IOException
{
// Attempt to establish connection to server
server = new Socket("127.0.0.1", 12345);
// server = new Socket(InetAddress.getHostName(ChatServer), 12345);
//display message to let user know they are connected to a specific ip address
}
public void getStreams() throws IOException
{
out = new ObjectOutputStream(server.getOutputStream());
out.flush();
in = new ObjectInputStream(server.getInputStream());
//display message showing user that streams are created
JOptionPane.showMessageDialog(this,"Streams received","Information",JOptionPane.INFORMATION_MESSAGE);
}
public void processServer() throws IOException
{
do
{
try
{
p = new Person();
p = (Person)in.readObject();
displayMessage(p);
System.out.println(p.toString());
getStreams();
}
catch ( ClassNotFoundException classNotFoundException )
{
JOptionPane.showMessageDialog(this,"Class not found","Error",JOptionPane.ERROR_MESSAGE);
} // end catch
finally
{
System.out.println("Process engadged from client");
}
}while(!txtMessage.getText().equals(".exit"));
}
public void sendData(Person P)
{
try
{
out.writeObject(P);
out.flush();
//JOptionPane.showMessageDialog(this,"Message sent","Information",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException io)
{
JOptionPane.showMessageDialog(this,"Message sent","Sending Message",JOptionPane.INFORMATION_MESSAGE);
}
finally
{
System.out.println("Data send from server");
}
}
public void displayMessage(Person P)
{
txtName.setText(P.GetName());
txtAge.setText(P.GetAge());
txtMessage.setText(P.GetMessage());
}
public void closeConnections()
{
try
{
out.close();
in.close();
server.close();
dispose();
}
catch(IOException io)
{
JOptionPane.showMessageDialog(this,"Message sent","Sending Message",JOptionPane.ERROR_MESSAGE);
}
finally
{
System.out.println("Connection closed from client side");
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnSend)
{
txtName.setText(p.GetName());
txtAge.setText(p.GetAge());
txtMessage.setText(p.GetMessage());
p.SetName(txtName.getText());
p.SetAge(txtAge.getText());
p.SetMessage(txtMessage.getText());
sendData(p);
}
if(ae.getSource() == btnExit)
{
closeConnections();
}
}
public static void main(String[] args)
{
ClientApp client = new ClientApp();
client.setVisible(true);
client.setBounds(100,200,300,200);
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.runClient();
}
}
[B]And code for the personclass[/B]
import java.io.Serializable;
public class Person implements Serializable
{
private String Name;
private String Message;
private String Age;
public Person()
{
Name = "";
Message = "";
Age = "";
}
public Person(String N,String A,String M)
{
Name = N;
Age = A;
Message = M;
}
public void SetName(String N)
{
Name = N;
}
public void SetMessage(String M)
{
Message = M;
}
public void SetAge(String A)
{
Age = A;
}
public String GetName()
{
return Name;
}
public String GetMessage()
{
return Message;
}
public String GetAge()
{
return Age;
}
public String toString()
{
return String.format("\nName: %s \n Message: \n %s \n Age: %s \n",Name,Message,Age);
}
}
[B][U]Code for the server class[/U][/B]
/**
* @(#)ServerApp.java
*
*/
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ServerApp extends JFrame implements ActionListener
{
private ServerSocket listener;
private Socket client;
private JTextField txtName,txtAge,txtMessage;
private JLabel lblName,lblAge,lblMessage;
private JButton btnSend,btnExit;
private ObjectOutputStream out;
private ObjectInputStream in;
Person p;
int Counter=0;
/** Creates a new instance of ServerApp */
public ServerApp()
{
super("Server");
txtName = new JTextField(10);
txtAge = new JTextField(10);
txtMessage = new JTextField(10);
lblName = new JLabel("Enter Name");
lblAge = new JLabel("Enter Age");
lblMessage = new JLabel("Enter Message");
btnSend = new JButton("Send");
btnSend.addActionListener(this);
btnExit = new JButton("Exit");
btnExit.addActionListener(this);
this.setLayout(new GridLayout(4,2,10,10));
this.add(lblName);
this.add(txtName);
this.add(lblAge);
this.add(txtAge);
this.add(lblMessage);
this.add(txtMessage);
this.add(btnSend);
this.add(btnExit);
this.pack();
}
public void runServer()
{
makeConnection();
while(true)
{
try
{
listen();
createStreams();
processClient();
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(this,"Server Terminated Connection","Error",JOptionPane.ERROR_MESSAGE);
}
finally
{
closeConnections();
dispose();
}
}
}
public void makeConnection()
{
try
{
listener = new ServerSocket(12345, 10);
}
catch (IOException ioe)
{
System.out.println("IO Exception: " + ioe.getMessage());
}
finally
{
System.out.println("Connection made with client");
}
}
public void listen() throws IOException
{
client = listener.accept();
}
public void createStreams() throws IOException
{
out = new ObjectOutputStream(client.getOutputStream());
out.flush();
in = new ObjectInputStream(client.getInputStream());
JOptionPane.showMessageDialog(this,"Streams created","Creating Streams",JOptionPane.INFORMATION_MESSAGE);
}
public void processClient()
{
p = new Person("Aljay","22","Hi there");
sendData(p); //this sets the cleint to read so no error occurs
do
{
try
{
p = new Person();
p = (Person)in.readObject();
displayMessage(p);
System.out.println(p.toString());
createStreams();
}
catch (IOException ioe)
{
System.out.println("IO Exception: " + ioe.getMessage());
}
catch (ClassNotFoundException cnfe)
{
System.out.println("Class not found: " + cnfe.getMessage());
}
finally
{
System.out.println("Process number" + Counter);
Counter++;
}
}while(true);
}
public void sendData(Person P)
{
try
{
out.writeObject(P);
out.flush();
}
catch(IOException io)
{
JOptionPane.showMessageDialog(this,"Error io in sendData","Error",JOptionPane.ERROR_MESSAGE);
}
finally
{
System.out.println("data send to client");
}
}
public void displayMessage(Person P)
{
txtName.setText(P.GetName());
txtAge.setText(P.GetAge());
txtMessage.setText(P.GetMessage());
}
public void closeConnections()
{
try
{
out.close();
in.close();
client.close();
dispose();
}
catch (IOException ioe)
{
System.out.println("IO Exception: " + ioe.getMessage());
}
finally
{
System.out.println("Connection close from server side");
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnSend)
{
//p = new Person();
txtName.setText(p.GetName());
txtAge.setText(p.GetAge());
txtMessage.setText(p.GetMessage());
p.SetName(txtName.getText());
p.SetAge(txtAge.getText());
p.SetMessage(txtMessage.getText());
sendData(p);
}
if(ae.getSource() == btnExit)
{
closeConnections();
}
}
public static void main(String[] args)
{
// Create application
ServerApp server = new ServerApp(/*p.SetName(txtName.getText()),p.SetAge(txtAge.getText()),p.SetMessage(txtMessage.getText())*/);
server.setVisible(true);
server.setBounds(100,200,300,200);
server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Start waiting for connections
server.runServer();
}
}