Hi,
Here I developed Instant Messenger Program. but in ClientTests.java is giving some runtime error. can anybody help me plz.

Here I am listing all my four programs.

1. Server.java

package com.app;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Server extends JFrame 
{
   private JTextField enterField; 
   private JTextArea displayArea; 
   private ObjectOutputStream output; 
   private ObjectInputStream input; 
   private ServerSocket server; 
   private Socket connection; 
   private int counter = 1; 

   
   public Server()
   {
      super( "Server" );

      enterField = new JTextField(); 
      enterField.setEditable( false );
      enterField.addActionListener(
         new ActionListener() 
         {
            
            public void actionPerformed( ActionEvent event )
            {
               sendData( event.getActionCommand() );
               enterField.setText( "" );
            } 
         } 
      ); 

      add( enterField, BorderLayout.NORTH );

      displayArea = new JTextArea(); 
      add( new JScrollPane( displayArea ), BorderLayout.CENTER );

      setSize( 300, 150 ); 
      setVisible( true ); 
   } 

   
   public void runServer()
   {
      try 
      {
         server = new ServerSocket( 12345, 100 ); 

         while ( true ) 
         {
            try 
            {
               waitForConnection(); 
               getStreams(); 
               processConnection(); 
            } 
            catch ( EOFException eofException ) 
            {
               displayMessage( "\nServer terminated connection" );
            } 
            finally 
            {
               closeConnection(); 
               counter++;
            } 
         } 
      } 
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } 
   } 

   
   private void waitForConnection() throws IOException
   {
      displayMessage( "Waiting for connection\n" );
      connection = server.accept(); 
      displayMessage( "Connection " + counter + " received from: " +
         connection.getInetAddress().getHostName() );
   } 

   
   private void getStreams() throws IOException
   {
     
      output = new ObjectOutputStream( connection.getOutputStream() );
      output.flush(); 

      
      input = new ObjectInputStream( connection.getInputStream() );

      displayMessage( "\nGot I/O streams\n" );
   } 

   
   private void processConnection() throws IOException
   {
      String message = "Connection successful";
      sendData( message ); 

      
      setTextFieldEditable( true );

      do 
      { 
         try 
         {
            message = ( String ) input.readObject(); 
            displayMessage( "\n" + message ); 
         } 
         catch ( ClassNotFoundException classNotFoundException ) 
         {
            displayMessage( "\nUnknown object type received" );
         } 

      } while ( !message.equals( "TERMINATE" ) );
   } 

   
   private void closeConnection() 
   {
      displayMessage( "\nTerminating connection\n" );
      setTextFieldEditable( false ); 

      try 
      {
         output.close(); 
         input.close(); 
         connection.close(); 
      } 
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } 
   } 

   
   private void sendData( String message )
   {
      try 
      {
         output.writeObject( "SERVER>>> " + message );
         output.flush(); 
         displayMessage( "\nSERVER>>> " + message );
      } 
      catch ( IOException ioException ) 
      {
         displayArea.append( "\nError writing object" );
      } 
   } 

   
   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater(
         new Runnable() 
         {
            public void run() 
            {
               displayArea.append( messageToDisplay ); 
            } 
         } 
      ); 
   } 

   
   private void setTextFieldEditable( final boolean editable )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run() 
            {
               enterField.setEditable( editable );
            } 
         }  
      ); 
   } 
}

2. ServerTest.java

package com.app;

import javax.swing.JFrame;

public class ServerTest
{
   public static void main( String args[] )
   {
      Server application = new Server(); 
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.runServer(); 
   } 
}

3. Client.java

package com.app;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Client extends JFrame 
{
   private JTextField enterField; 
   private JTextArea displayArea; 
   private ObjectOutputStream output;
   private ObjectInputStream input; 
   private String message = ""; 
   private String chatServer; 
   private Socket client; 

   
   public Client( String host )
   {
      super( "Client" );
		
      chatServer = host; 

      enterField = new JTextField(); 
      enterField.setEditable( false );
      enterField.addActionListener(
         new ActionListener() 
         {
            
            public void actionPerformed( ActionEvent event )
            {
               sendData( event.getActionCommand() );
               enterField.setText( "" );
            } 
         } 
      ); 

      add( enterField, BorderLayout.NORTH );

      displayArea = new JTextArea(); 
      add( new JScrollPane( displayArea ), BorderLayout.CENTER );

      setSize( 300, 150 ); 
      setVisible( true ); 
   } 

   
   public void runClient() 
   {
		
      try 
      {
		
         connectToServer(); 
         getStreams(); 
         processConnection(); 
      } 
      catch ( EOFException eofException ) 
      {
         displayMessage( "\nClient terminated connection" );
      } 
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } 
      finally 
      {
         closeConnection(); 
      } 
   } 

   
   private void connectToServer() throws IOException
   {      
	
      displayMessage( "Attempting connection\n" );

      
      client = new Socket( InetAddress.getByName( chatServer ), 12345 );

      
      displayMessage( "Connected to: " + 
         client.getInetAddress().getHostName() );
		 
   } 

   
   private void getStreams() throws IOException
   {
     
	  
      output = new ObjectOutputStream( client.getOutputStream() );      
      output.flush(); 

      
      input = new ObjectInputStream( client.getInputStream() );

      displayMessage( "\nGot I/O streams\n" );
   } 

   
   private void processConnection() throws IOException
   {
     
      setTextFieldEditable( true );

      do 
      { 
         try 
         {
            message = ( String ) input.readObject(); 
            displayMessage( "\n" + message ); 
         } 
         catch ( ClassNotFoundException classNotFoundException ) 
         {
            displayMessage( "\nUnknown object type received" );
         } 

      } while ( !message.equals( "TERMINATE" ) );
   } 

   
   private void closeConnection() 
   {
      displayMessage( "\nClosing connection" );
      setTextFieldEditable( false ); 

      try 
      {
         output.close(); 
         input.close(); 
         client.close(); 
      } 
      catch ( IOException ioException ) 
      {
         ioException.printStackTrace();
      } 
   } 

   
   private void sendData( String message )
   {
      try 
      {
         output.writeObject( "CLIENT>>> " + message );
         output.flush(); 
         displayMessage( "\nCLIENT>>> " + message );
      } 
      catch ( IOException ioException )
      {
         displayArea.append( "\nError writing object" );
      } 
   } 

   
   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run() 
            {
               displayArea.append( messageToDisplay );
            } 
         }  
      ); 
   } 

   
   private void setTextFieldEditable( final boolean editable )
   {
      SwingUtilities.invokeLater(
         new Runnable() 
         {
            public void run() 
            {
               enterField.setEditable( editable );
            } 
         } 
      ); 
   } 
}

4. ClientTests.java

package com.app;



import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class UserTest extends JFrame
{
	private JTextField userName;
	private JPasswordField password;
	private JLabel userLabel,passLabel;
	private JButton submit;
	
	public UserTest()
	{
		super("User Creation");
		setLayout(new FlowLayout());
		userLabel = new JLabel("User Name : ");
		add(userLabel);
		userName = new JTextField(20);
		add(userName);
		passLabel = new JLabel("Password : ");
		add(passLabel);
		password = new JPasswordField(20);
		add(password);
		submit = new JButton("submit");
		add(submit);
		ButtonHandler handler = new ButtonHandler();
		userName.addActionListener(handler);
		submit.addActionListener(handler);
		setSize( 300, 150 ); 
		setVisible( true );
	}

private class ButtonHandler implements ActionListener 
   {
      
      public void actionPerformed( ActionEvent event )
      {
    	String uName="";
    	BufferedReader reader;
    	String fileUser;
    	int flag=0;
    	uName = userName.getText();
		try
		{
        File file = new File("c:\\test.txt");
 		reader = new BufferedReader(new FileReader(file));
 		
 		while((fileUser=reader.readLine())!=null)
 		{
 			if(uName.equalsIgnoreCase(fileUser.substring(0, fileUser.indexOf("@"))))
 			{		
 				flag = 1;
 				break;
 			}
 		}
 		if(flag==1)
 		{
 			Client application = new Client("127.0.0.1");
 	   	 	application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	   	 	application.runClient();	
 		}
 		else
 		{
 			JOptionPane.showMessageDialog(null, "Invalid User");
 		}
 		
		}
		catch(FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	  } 
  } 
	
}




public class ClientTests
{
   public static void main( String args[] )
   {
      UserTest usr = new UserTest();
	  usr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
   } 
}

after executing ServerTest, when i execute ClientTests, it is not working properly.

Hi there, welcome to daniweb buddy. Try using code tags when you post, it makes your code much easier to read. Tell us, what is the runtime error message you are getting?

Hi there, welcome to daniweb buddy. Try using code tags when you post, it makes your code much easier to read. Tell us, what is the runtime error message you are getting?

HI,

It is not runtime error, means in the client window i am unable to see the messages which are sent by server. when i terminate the server window then it is showing all the messages in client window. means internally the application is running.

So the client isn't displaying messages until you close the server? Almost sounds like the client isn't updated after you've appended a message.

This article has been dead for over six months. Start a new discussion instead.