Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Dayclient and Dayserver - implement the Unix 'daytime' protocol. Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

These two classes show an extremely simple example of java.net socket programming. They implement the Unix 'daytime' protocol, an extremely simple protocol that consists entirely of the server sending its current local time and date to the client as an ASCII string. The server, to keep it very simple, does not use multiple threads. This code requires JDK 1.1 or later, but can easily be adapted to JDK 1.0.

Code:

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

/**
 * Tiny socket client for the Unix 'daytime' protocol.
 */

public class Dayclient
{
    public static void main(String[] args)
    {
   Socket csocket;
   BufferedReader   timestream;
   if (args.length < 1) {
       System.err.println("Please supply a host name or IP address");
       System.exit(0);
   }
   try
       {
      csocket = new Socket(args[0],13);
      timestream = new BufferedReader(new InputStreamReader(csocket.getInputStream()));
      String thetime = timestream.readLine();
      System.out.println("it is "+ thetime + " at " + args[0]);

       
      csocket.close();
         
       }

   catch(IOException e)
       { System.err.println(e);}

    }
}
 

 
 



Code:



import java.util.Date;
import java.io.*;
import java.net.*;

/**
 * Tiny socket server for the Unix 'daytime' protocol.
 */

public class Dayserver
{
    public static void main(String[] args)
    {
   ServerSocket      theserver;
   Socket       asocket;
   PrintWriter          p;
   BufferedReader   timestream;
   Socket csocket;
 
   try
       {
      theserver = new ServerSocket(13);
      try
          {
         while(true)
             { 
            System.out.println("Waiting for customers...");
            asocket = theserver.accept();
            p =  new PrintWriter(asocket.getOutputStream());
            p.println(new Date());
         

            p.close();
            asocket.close();
            // csocket.close();
            // timestream.close();
             }
          }

      catch(IOException e)
          { theserver.close();
          System.err.println(e);}

       }

   catch(IOException e)
       { System.err.println(e);}
    }
}
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.io.BufferedReader

  java.net.Socket

  java.io.InputStreamReader

  java.io.IOException

  java.net.ServerSocket

  java.io.PrintWriter

  java.sql.Date




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy