Java
  Home arrow Java arrow Page 3 - Socket Programming in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Socket Programming in Java
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 80
    2007-04-17

    Table of Contents:
  • Socket Programming in Java
  • Socket programming, step by step
  • Socket programming in the real world
  • Socket programming in the real world, continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Socket Programming in Java - Socket programming in the real world


    (Page 3 of 4 )

    It's time to put theory to practice. The file server to be developed will provide the following services:

    1. List the files that can be downloaded.
    2. Send the selected file.
    3. Process each request in a separate thread.

    This example is from the solution to an exercise from Professor David Eck’s on-line textbook, published under an open content license at http://math.hws.edu/eck/cs124/javanotes4/c10/ex-10-4-

     

    answer.html.

     

     

    There are two classes that form the server:

     

    1. FileServer - sets up the server.
    2. ConnectionHandler - services the requests for sending files to clients.

    Let's look at the implementation. First comes the FileServer class. It does the following tasks:

    1. Checks the existence of the directory name specified.
    2. Sets up the server.
    3. Delegates the requests to be handled to an object of the ConnectionHandler class.

    the following is the implementation of the class:

    import java.net.*;
      import java.io.*;

      public class FileServer {

        static final int LISTENING_PORT = 3210;

        public static void main(String[] args) {

          File directory; // The directory from which the
                         
    // gets the files that it serves.
         
    ServerSocket listener; // Listens for connection
                                 // requests.
         
    Socket connection; // A socket for communicating
                             // with
    a client.

          /* Check that there is a command-line argument.
         
    If not, print a usage message and end. */

          if (args.length == 0) {
           
    System.out.println("Usage: java FileServer <directory>");
           
    return;
         
    }

          /* Get the directory name from the command line,
           and make
    it into a file object. Check that the
           file exists and
    is in fact a directory. */

          directory = new File(args[0]);
         
    if ( ! directory.exists() ) {
           
    System.out.println("Specified directory does not exist.");
           
    return;
         
    }
         
    if (! directory.isDirectory() ) {
           
    System.out.println("The specified file is not a directory.");
           
    return;
         
    }

          /* Listen for connection requests from clients. For
            
    each connection, create a separate Thread of
             type
    ConnectionHandler to process it. The
             ConnectionHandler
    class is defined below. The
             server runs until the
    program is terminated, for
             example by a CONTROL-C. */

          try {
           
    listener = new ServerSocket(LISTENING_PORT);
           
    System.out.println("Listening on port " + LISTENING_PORT);
           
    while (true) {
             
    connection = listener.accept();
             
    new ConnectionHandler(directory,connection);
           
    }
          
    }
         
    catch (Exception e) {
           
    System.out.println("Server shut down unexpectedly.");
            
    System.out.println("Error: " + e);
           
    return;
         
    }

        } // end main()

         :
         :
     
    }

    More Java Articles
    More By A.P.Rajshekhar


       · In this article I have discussed basics of sockets and how to program TCP sockets...
       · What version of JAVA did you used in this exampple. I've been trying to test the...
       · HiCan you tell me which OS are you using as the distro of Linux that I am using...
       · I assume you mean to do more than that
       · Here is 'more than...
       · Hi This is excellent article. I want to use Unix socket communication. How to do...
       · with java 1.5 instead of using TextReader you can also try BufferedReader class for...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT