Welcome to Dream.In.Code
Become a Java Expert!

Join 150,375 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,654 people online right now. Registration is fast and FREE... Join Now!




Java server client problem

 
Reply to this topicStart new topic

Java server client problem, trying to pass text file contents

royjm248
3 Feb, 2008 - 12:19 AM
Post #1

New D.I.C Head
*

Joined: 18 Jan, 2008
Posts: 18



Thanked: 1 times
My Contributions
Here is the plan: the client requests a file, the server is to open, read, and pass the contents of a simple text file. The problem is that I either pass nothing to the client, or the best I got was an error stating that I am trying to pass a corrupted object.

CODE

// Set up a server that will receive a connection from a client, send
// a string to the client, and close the connection.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class Server extends JFrame
{

    private JTextArea displayArea; // display information to user
    private ObjectOutputStream output; // output stream to client
    private ObjectInputStream input; // input stream from client
    private ServerSocket server; // server socket
    private Socket connection; // connection to client
    private int counter = 1; // counter of number of connections


       // set up GUI
   public Server()
   {
      super( "Server" );

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

      setSize( 300, 150 ); // set size of window
      setVisible( true ); // show window
   } // end Server constructor

   // set up and run server
   public void runServer()
   {
      try // set up server to receive connections; process connections
      {
         server = new ServerSocket( 12345, 100 ); // create ServerSocket

         while ( true )
         {
            try
            {
               waitForConnection(); // wait for a connection
               getStreams(); // get input & output streams
               processConnection(); // process connection
            }
            catch ( EOFException eofException )
            {
               displayMessage( "\nServer terminated connection" );
            }
            finally
            {
               closeConnection(); //  close connection
               counter++;
            }
         }
      }
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
   } // end method runServer

   // wait for connection to arrive, then display connection info
   private void waitForConnection() throws IOException
   {
      displayMessage( "Waiting for connection\n" );
      connection = server.accept(); // allow server to accept connection
      displayMessage( "Connection " + counter + " received from: " +
         connection.getInetAddress().getHostName() );
   } // end method waitForConnection

   // get streams to send and receive data
   private void getStreams() throws IOException
   {
      // set up output stream for objects
      output = new ObjectOutputStream( connection.getOutputStream() );
      output.flush(); // flush output buffer to send header information

      // set up input stream for objects
      input = new ObjectInputStream( connection.getInputStream() );

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

   // process connection with client
   private void processConnection() throws IOException
   {
      String message = "Connection successful";


      do // process messages sent from client
      {
          //alter in here to accept, check and read
          //if's to read and print
          //check to see what if  i.e save or open file
         try // read message and display it
         {
            message = ( String ) input.readObject(); // read new message

                 try
                 {
                            File name = new File( actionEvent.getActionCommand() );
                 }
finally
{
    closeConnection();
}

         } // end try
         catch ( ClassNotFoundException classNotFoundException )
         {
            displayMessage( "\nUnknown object type received" );
         } // end catch

      } while ( !message.equals( "CLIENT>>> TERMINATE" ) );
   } // end method processConnection

   // close streams and socket
   private void closeConnection()
   {
      displayMessage( "\nTerminating connection\n" );

      try
      {
         output.close(); // close output stream
         input.close(); // close input stream
         connection.close(); // close socket
      } // end try
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
   } // end method closeConnection


   // manipulates displayArea in the event-dispatch thread
   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run() // updates displayArea
            {
               displayArea.append( messageToDisplay ); // append message

            } // end method run
         } // end anonymous inner class
      ); // end call to SwingUtilities.invokeLater
   } // end method displayMessage

} // end class Server

User is offlineProfile CardPM
+Quote Post

royjm248
RE: Java Server Client Problem
3 Feb, 2008 - 08:41 PM
Post #2

New D.I.C Head
*

Joined: 18 Jan, 2008
Posts: 18



Thanked: 1 times
My Contributions
Ok, I have tried the following, to no avail.

CODE


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class Server extends JFrame
{

    private JTextArea displayArea; // display information to user
    private ObjectOutputStream output; // output stream to client
    private ObjectInputStream input; // input stream from client
    private ServerSocket server; // server socket
    private Socket connection; // connection to client
    private int counter = 1; // counter of number of connections
    private ObjectOutputStream fileOutput;

       // set up GUI
   public Server()
   {
      super( "Server" );

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

      setSize( 300, 150 ); // set size of window
      setVisible( true ); // show window
   } // end Server constructor

   // set up and run server
   public void runServer()
   {
      try // set up server to receive connections; process connections
      {
         server = new ServerSocket( 12345, 100 ); // create ServerSocket

         while ( true )
         {
            try
            {
               waitForConnection(); // wait for a connection
               getStreams(); // get input & output streams
               processConnection(); // process connection
            }
            catch ( EOFException eofException )
            {
               displayMessage( "\nServer terminated connection" );
            }
            finally
            {
               closeConnection(); //  close connection
               counter++;
            }
         }
      }
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
   } // end method runServer

   // wait for connection to arrive, then display connection info
   private void waitForConnection() throws IOException
   {
      displayMessage( "Waiting for connection\n" );
      connection = server.accept(); // allow server to accept connection
      displayMessage( "Connection " + counter + " received from: " +
         connection.getInetAddress().getHostName() );
   } // end method waitForConnection

   // get streams to send and receive data
   private void getStreams() throws IOException
   {
      // set up output stream for objects
      output = new ObjectOutputStream( connection.getOutputStream() );
      output.flush(); // flush output buffer to send header information

      // set up input stream for objects
      input = new ObjectInputStream( connection.getInputStream() );

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

   // process connection with client
   private void processConnection() throws IOException
   {
      BufferedReader in;
      String read;
      String message = "Connection successful";


      do // process messages sent from client
      {

         try // read message and display it
         {
            String fileName = ( String ) input.readObject(); // read new message
           // String fileName = message.toString();

            File file = new File (fileName);

             if ( (! file.exists()) || file.isDirectory() )
                {
                 // (Note:  Don't try to send a directory, which
                 // shouldn't be there anyway.)
                 displayMessage("error\n");
                }
            else
                {
                in = new BufferedReader (new FileReader(fileName));
                read = in.readLine();
                displayMessage(read);
                fileOutput = new ObjectOutputStream( connection.getOutputStream() );
                fileOutput.flush();
                }
            displayMessage(fileName);

         } // end try
         catch ( ClassNotFoundException classNotFoundException )
         {
            displayMessage( "\nUnknown object type received" );
         } // end catch

      } while ( !message.equals( "CLIENT>>> TERMINATE" ) );
   } // end method processConnection

   // close streams and socket
   private void closeConnection()
   {
      displayMessage( "\nTerminating connection\n" );

      try
      {
         output.close(); // close output stream
         input.close(); // close input stream
         connection.close(); // close socket
      } // end try
      catch ( IOException ioException )
      {
         ioException.printStackTrace();
      } // end catch
   } // end method closeConnection


   // manipulates displayArea in the event-dispatch thread
   private void displayMessage( final String messageToDisplay )
   {
      SwingUtilities.invokeLater(
         new Runnable()
         {
            public void run() // updates displayArea
            {
               displayArea.append( messageToDisplay ); // append message

            } // end method run
         } // end anonymous inner class
      ); // end call to SwingUtilities.invokeLater
   } // end method displayMessage

} // end class Server


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month