TweetFollow Us on Twitter

Java Net Classes
Volume Number:12
Issue Number:10
Column Tag:Java Tech

Java Net Classes

Writing Java code to access the Internet is a snap

By Christopher Evans

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

A large part of Java’s appeal is that much of what of we used to spend those days, nights and weekends doing is now taken care of for us. Memory management, threading and networking are trivial endeavors in Java. In this article, I show exactly how simple it is to use the networking classes that come with Java. Collectively, these are referred to as the java.net package, and they contain things like URL content handlers, and stream and datagram socket classes.

Getting a URL

Since much of the Java class library was written to facilitate the creation of the Hot Java Web browser, accessing a URL is pretty simple. Here’s how to get the text content of a URL in Java; you must admit, things don’t come much easier than this:

public static String GetTextURL(String url_address)
{
 URL  url = new URL(url_address);
 return (String)url.getContent();
}

If you were to call this method with a hard-coded URL, it would return a string containing the text content of that URL. This string could then be dumped to standard out, or it could be parsed as part of a larger Web access application. Here, we just do the former:

String roaster_info = GetTextURL(“http://www.roaster.com”);
System.out.println(roaster_info);

It’s this easy for almost any type of data for which there is a content handler. By default, JDK 1.0.2 comes with content handlers for text and images (GIF or JPEG), but you can write your own content handlers by subclassing java.net.ContentHandler and converting the MIME data type into an object that can be understood by the application or applet.

A Simple Syslog Server...

Creating Java applications or applets that do more on the Net isn’t much more complicated. Traditionally, network programming, particularly on the Macintosh, has been reserved for a few stalwart engineers - the guys who sat in the corner, surrounded by routers, deferred to by those not educated in the black art of protocol stacks and socket streams. The java.net package makes it possible for more developers to take advantage of the really cool things you can do once you begin communicating over a network.

Since Java was designed to work over the Internet, the only network protocol supported is TCP/IP; but you can be pretty sure that this will be available on just about any machine you can buy these days.

The following example shows how to create a very basic server. This server implements the Unix syslog program. It opens a UDP (User Datagram Protocol) socket on port 514 and waits for some data to come in. When data arrives on the port, it is received and dumped into a text file. The real syslog allows you to have different levels of messages that go to different files, but I think this gives you a pretty good idea of what is involved in creating a basic UDP server application.

Note that UDP is known as an unreliable protocol, because you are not guaranteed to receive data that is sent to your port. If reliability is important, then you should use the TCP protocol, which makes sure that if a packet is sent and it’s possible for it to be received, then it will be received. For a simple message-logging program like this, though, UDP is sufficient.

JavalogServer.java
/*
    JavalogServer.java
    
    A very basic implementation of syslog in Java using UDP datagram
    sockets
    
    By Christopher Evans
    Copyright © 1996 Natural Intelligence, Inc.
*/
import java.net.*;
import java.io.*;

/**
Implements a basic syslog server receiving data on port 514 and
logging it to a file called syslog.log
*/ 

public class JavalogServer
{
 
 public static void main(String args[]) throws Exception
 {
    //Create the buffer to store the data as it comes in
 byte[] log_buffer = new byte[2048];
 
 int    received_messages = 0;
 
    //Open the file for writing the log messages
 File   out_file = new File(“syslog.log”);
 
    //Create the output stream so we can dump the data
 FileOutputStream syslog_file =
 new FileOutputStream(out_file);
 
    //Create a DatagramPacket to receive the incoming log data
 DatagramPacket packet = 
 new DatagramPacket(log_buffer, log_buffer.length);
 
    //Create a socket that listens on the net
 DatagramSocket socket = new DatagramSocket(514);
 
 while(received_messages < 5) 
 {
    //Wait until some data arrives. Aren’t threads great?
 socket.receive(packet);
 
    //Increment the message count
 received_messages++;
 
    //Build a string of the packet data
 String packet_string = 
 new String(log_buffer, 0, 0, packet.getLength());
 
    //Put the packet data after a bit of header so we can see where it comes from
 String out_string = “<syslog from “ + \
 packet.getAddress().getHostName() + “>” + \
 packet_string + “\n”;
 
    //Print the message to the standard out window
 System.out.println(out_string);
 
    //Convert the message to an array of bytes so it can be sent to the file
 int msg_len = out_string.length();
 
 byte[] out_buffer = new byte[msg_len];
 
 out_string.getBytes(0, out_string.length(), 
 out_buffer, 0);

    //Write the name of the host where the data came from to the file
 syslog_file.write(out_buffer, 0,
 out_string.length());
 
 }
 socket.close();
 } 
}

This Java application starts by creating a 2K buffer to store the messages in as they arrive. Then it creates a file called syslog.log in the same directory as the application, and opens an output stream to that file. Next, it creates a DatagramPacket object, giving it the buffer created earlier as its place to store the network packets as they come in, and a DatagramSocket object, passing it the port where it should be listening.

After all of this initialization is done, the JavalogServer calls socket.receive and passes it the DatagramPacket object. The DatagramSocket object will wait until some data has arrived in the port before returning. When data does arrive, that data is immediately written out to the file, and socket.receive is called again. For this example, our server quits after receiving five messages, but in the real world, syslog continually waits for the next message to arrive.

Since Java is running on its own virtual machine, and since that virtual machine is preemptively scheduled, the machine can do other things while the JavalogServer is waiting for a message to arrive.

When you run this on some of the applet runners on the Mac, you will find that there is no way to quit the server process. It would not be hard to add a menu with AWT that would allow you to quit; for now, it will quit when it receives the fifth message.

...And a Simple Syslog Messenger

What good is a server without a client? Next, I am going to show how easy it is to send data across a network. JavalogClient gets its target host machine, and the message to send, from the command line; then it sends the message to the host.

JavalogClient.java
/*
    JavalogClient.java
    
    A very basic implementation of a syslog message dispatcher in Java 
    using UDP datagram sockets
    
    By Christopher Evans
    Copyright © 1996 Natural Intelligence, Inc.
*/


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

/**
    Implements a basic syslog client, sending data to port 514 on
    the machine whose name is passed in as a command line argument
*/ 

public class JavalogClient {

 public static void main(String args[]) throws Exception
 {
 if(args.length != 2) {
 System.out.println(“Usage: JavalogClient <host> \
   <message>”);
 System.exit(0);
 }
 
    //Create an InetAddress object and initialize it from the first argument
    //which should be a host name like natural.natural.com
 InetAddress address = InetAddress.getByName(args[0]);
 
    //Find out how long the message is, then copy it from a java.lang.String
    //into an array of bytes to be sent.
 int msg_len = args[1].length();
 
 byte[] message = new byte[msg_len];
 
 args[1].getBytes(0,msg_len, message,0);
 
    //Create a DatagramPacket object with the data that the user wants to send
 DatagramPacket packet = 
 new DatagramPacket(message, msg_len, address, 514);
 
    //Create a new datagram socket to send the packet
 DatagramSocket socket = new DatagramSocket();
 
    //Now actually send the data across the network
 socket.send(packet);
 
    //Clean up the socket so it isn’t left open
 socket.close();
 
 }
}

The client is even smaller than the server. The user sends a message with command line arguments like this:

host.natural.com “This is a test of the emergency Javalog system”

This would result in a line in the log file that would look something like this:

<syslog from evans.natural.com.> This is a test of the emergency Javalog 
system

First we create an InetAddress object based on the host name passed in as the first command-line argument (“host.natural.com”, in the example). Then we take the second command-line argument and convert it from a java.lang.String object into an array of bytes, just as we did in the JavalogServer example. (In case you are wondering, this is necessary since Java strings are stored as Unicode strings where each character is two bytes. If you want to send straight ASCII across the network or store the string in a text file, you must first convert it to an array of bytes.)

After the message is created, we create a java.net.DatagramPacket object containing everything we need to know in order to send the data, including the host name, the socket on that host, and the message data itself. Then we create the java.net.DatagramSocket object, which actually opens the TCP/IP socket for sending the data. The final steps are to send the packet and to close the socket.

The client could also be cleaned up with a nice AWT interface allowing you to enter the host name in one text field and the message in another, but I wanted to make it clear how simple it is to implement basic networking in Java.

Further Reading

For more information on the java.net classes, you might want to check out the following sources:

Lemay, Laura, and Charles Perkins, with Timothy Webster. Teach Yourself Java For Macintosh in 21 Days. Hayden Books. 1996.

David Flanagan. Java in a Nutshell. O’Reilly & Associates, Inc. 1996.

JavaSoft WWW:

http://java.sun.com/java.sun.com/products/JDK/1.0.2/api/javaf.htm

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.