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

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.