Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: client/server socket

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default client/server socket

    does anyone know how to write the client/server program (using sockets) where the client can ask the server if the file does/does not exist; if it does then read the file contents from server and shows it...there must be two separate client and server windows


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: client/server socket

    Hello Java_dude_101.

    Welcome to the Java Programming Forums.

    Firstly you should concentrate on getting a client to connect to the server. This code will help:

    Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)

    Play around with that. When you are happy, post the code you have and we will help you with the file part.

    Thanks.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: client/server socket

    I was able to make the connection and send streams back and forth...here is an example of the client side. I chose to put the code for reading the entry in this actionListener since it was already performing that role.

    public Client(String host) {
            super("Client Window");
     
            chatServer = host; // set server to which this client connects
     
            enterField = new JTextField(); // create enterField
            enterField.setEditable(false);
            enterField.addActionListener(
                    new ActionListener() {
                        // send message to server
     
                        public void actionPerformed(ActionEvent event) {
                            sendData(event.getActionCommand());
                            enterField.setText("");
     
     
                            try // display contents of file
                            {
                                // create Socket to make connection to server
     
                                messoutput = new Formatter(client.getOutputStream());
                                // messoutput.flush(); // flush output to send header information
                                messinput = new Scanner(client.getInputStream());
     
                                String fileName = event.getActionCommand() + "\n";
                                messoutput.format(fileName);
                                messoutput.flush(); // flush output
                                String inputLine = messinput.nextLine(); // read input line
                                displayArea.setText(inputLine); // show input line in textarea
     
                                // if file exists, display file contents
                                if (inputLine.equals("The file is:")) {
                                    while (messinput.hasNextLine()) {
                                        inputLine = messinput.nextLine(); // read a new line
                                        displayArea.append('\n' + inputLine); // add line
                                    } // end while
                                } // end if
                            } // end try
                            catch (IOException ioException) {
                                ioException.printStackTrace();
                                System.exit(1);
                            } // end catch
                            finally {
                                try {
                                    input.close(); // close output
                                    output.close(); // close input
                                    client.close(); // close connection to server
                                } catch (IOException ioException) {
                                    ioException.printStackTrace();
                                    System.exit(1);
     
                                } // end method actionPerformed
                            } // end anonymous inner class
                        }
                    }); // end call to addActionListener
    //close the attempted file formatter scanner
            add(enterField, BorderLayout.NORTH);
     
            displayArea = new JTextArea(); // create displayArea
            add(new JScrollPane(displayArea), BorderLayout.CENTER);
     
            setSize(300, 150); // set size of window
            setVisible(true); // show window
        } // end Client constructor


    The server side...I have the get streams and process connections methods and I chose to use the process connection() for looking to see if the file actually there.
    private void processConnection() throws IOException
       {
          String message = "Connection successful";
          sendData( message ); // send connection successful message
     
          // enable enterField so server user can send messages
          setTextFieldEditable( true );
     
          do // process messages sent from client
          {
             try // read message and display it
             {
                message = ( String ) input.readObject(); // read new message
                displayMessage( "\n" + message ); // display message
      //Attempted code to read format of entry and determine if it is a file
                //start code
             connection = server.accept(); // accept connection
             messoutput = new Formatter( connection.getOutputStream() );
             messoutput.flush(); // flush output to send header information
             messinput = new Scanner( connection.getInputStream() );
             File file = new File( messinput.nextLine() ); // get file name
             String result; // result from checking file
     
             // file does exist
             if ( file.exists() )
             {
                Scanner fileInput = new Scanner( file ); // file scanner
                messoutput.format( "The file is:\n" ); // write header
                messoutput.flush(); // flush output
     
                while ( fileInput.hasNextLine() )
                {
                   result = fileInput.nextLine(); // read a line from file
                   messoutput.format( "%s\n", result ); // output line of file
                   messoutput.flush(); // flush output
                } // end while
             } // end if
             else // file does not exist
             {
                result = file.getName() + " does not exist\n";
                messoutput.format( result ); // write that file does not exist
                messoutput.flush(); // flush output
             } // end else
         // } // end try
             //end attempted code for reading format if entry
             } // end try
             catch ( ClassNotFoundException classNotFoundException )
             {
                displayMessage( "\nUnknown object type received" );
             } // end catch
     
          } while ( !message.equals( "CLIENT>>> TERMINATE" ) );
       } // end method processConnection

Similar Threads

  1. How to Create a server socket to listen for incoming connections?
    By JavaPF in forum Java Networking Tutorials
    Replies: 3
    Last Post: October 28th, 2011, 09:02 AM
  2. problem with closing connection to client socket
    By sunitha in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2010, 04:28 AM
  3. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  4. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM
  5. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM