Home arrow Java arrow Page 3 - Socket Programming in Java
JAVA

Socket Programming in Java


In a world which is wired, standalone applications are becoming obsolete. The facilitators for the connectivity of applications, at a low level, are sockets. Any language, whether high-level or low-level, must provide APIs to handle sockets if its creators want it to be widely accepted.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 196
April 17, 2007
TABLE OF CONTENTS:
  1. · Socket Programming in Java
  2. · Socket programming, step by step
  3. · Socket programming in the real world
  4. · Socket programming in the real world, continued

print this article
SEARCH DEVARTICLES

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()

     :
     :
 
}


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials

antalya escort escort antalya antalya escortlari antalya escort bayan