| | |
Client and server with objects
![]() |
•
•
Join Date: Sep 2009
Posts: 1
Reputation:
Solved Threads: 0
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
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();
}
}
And code for the personclass
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);
}
}
Code for the server class
/**
* @(#)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();
}
} Last edited by root59; Sep 5th, 2009 at 1:17 pm. Reason: wrong input of code
![]() |
Other Threads in the Java Forum
- Previous Thread: Java Encryption and Decryption
- Next Thread: age compute using calendar class [problem]
| Thread Tools | Search this Thread |
.net 6 2003 ad apache api applicaions application automation backup broadcast browser business c# c++ cache centro chatprogramusingobjects client cpu daniweb database dedicated dell disk dragging drive enterprise error examples exchange execution filename firefox forms freeze gigabyte google government gui hash hosting hp ie8 java keyword linux load management memory microsoft mozilla msqli_multi_query mysql netscape news open openssh opera os parsing patch permissions php problem proxy pygame pygtk python rdimm recourse redhat remote remoting rhel running... seamonkey security securitybulletin server small socket software source sql stop suse swing textfield textfields tkinter ui uk upgrade valid vmware win32 windowmanagers windows working






