Following up on my previous post, we also had to demonstrate a sample Java TCP Server and TCP Client. They are pretty small and give you a good idea about how a TDP Server opens up a port, and then the TCP Client sends or receives data from that port.

This is a good page on the differences between TCP and UDP.

To compile these, install Java JDK to your system. Then compile the program with “javac TCPClient.java” - this will create a TCPClient.class. Execute the file with “java TCPClient” - leave off the .class, or you will get the error: “Exception in thread “main” java.lang.NoClassDefFoundError”.

Here is the sample code:

TCPServer.java

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

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
         }
      }
}

and the client:

TCPClient.java

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

class TCPClient
{
 public static void main(String argv[]) throws Exception
 {
  String sentence;
  String modifiedSentence;
  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
  Socket clientSocket = new Socket("localhost", 6789);
  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  sentence = inFromUser.readLine();
  outToServer.writeBytes(sentence + '\n');
  modifiedSentence = inFromServer.readLine();
  System.out.println("FROM SERVER: " + modifiedSentence);
  clientSocket.close();
 }
}

If you have any questions, please leave a comment!

For a class I am taking, we are testing out a simple UDP Server and UDP Client to demonstrate what each one does and how sockets work. They are pretty small and give you a good idea about how a UDP Server opens up a port, and then the UDP Client sends or receives data from that port.

To compile these, install Java JDK to your system. Then compile the program with “javac UDPClient.java” - this will create a UDPClient.class. Execute the file with “java UDPClass” - leave off the .class, or you will get the error: “Exception in thread “main” java.lang.NoClassDefFoundError”.

Here is the sample code:

UDPServer.java:

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

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
}

UDPClient.java:

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

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}

If you are looking for a great Java based IRC client, I would suggest PJIRC. It is really easy to integrate into your web pages; and includes the html code that you need to embed it onto a page. It is very flexible, for example you can tell it to automatically join a server that you specify, and have it automatically execute a command, such as joining a channel.

It’s a small download, and runs really quickly, which was always a downside of the other IRC Java applets I’ve tried. The GUI really isn’t too bad. It has tabs for different channels, a full scroll window, right click ability for whois, op, de-op, and more.

Download PJIRC

Technorati Tags: , ,

I know I’m a day late and a dollar short, but I wanted to throw in my thoughts on Apple, Inc.’s newly introduced iPhone. Besides pending trademark issue with the Cisco/Linksys iPhone, this looks like a great device. Even if they change its name - a rose by any other name would smell as sweet.

A few years back, I predicted that in several years we would see massive convergence of three devices - the mp3 player, the cell phone, and the camera. You could also add in video player and web/email browser. Now, I’m not talking about the cameras we see on cell phones today - even the 2 megapixel versions that are out are really not that good at taking shots. The full convergence will be successful when it does each of these tasks well.

I knew it had not taken place yet, because the mp3 player/cell phones that have come out so far are pitiful. I bought a Motorola V360 last year, which has memory card support along with a built in mp3 player. However, the interface to this mp3 player was horrible. Every time you launched the java mp3 player applet, it would take up to a minute to load all of your songs. From there it took 30 more seconds to find the song you wish to play. This is not an experience you want to have.

Motorola then ditched it’s mp3 applet in favor of iTunes for cell phones. I was able to install it to my V360 via a hack - it wasn’t pretty but it worked and greatly improved the mp3 playing ability of the cell phone. However the full experience was still missing something.

Enter the new phone from Apple. It plays music, video, photos… and oh yeah it can make calls too. Apple has completely redone the interface, and it is different than any phone currently on the market. The call interface actually looks pretty Skype-ish, which is a good thing. The way you navigate the phone is different as it is a gigantic touch screen. I think there will be some resistance to this type of navigation (as well as the touch keyboard) but it will grow on people and eventually will become the standard on all cell phones. Why keep the whole keyboard there if you only need it a fraction of the time?

I want this phone. I want it bad. Personally, it will be great to have this phone. However, as a business class device, there are a few features I am concerned about, because if it does not support them then it is DOA in the workplace.

The first is its e-mail client. During the demos, you can see it handles photos flawlessly. What about office attachments? Word and excel spreadsheets? Being an Apple device I would not expect these functions to be built in but I am hopeful that Apple has enough sense that it will open it’s OS for development. This is tied into my second reservation. The OS it is running is based off of OS X:

All the power and sophistication of the world’s most advanced operating system — OS X — is now available on a small, handheld device that gives you access to true desktop-class applications and software, including rich HTML email, full-featured web browsing, and applications such as widgets, Safari, calendar, text messaging, Notes, and Address Book. iPhone is fully multi-tasking, so you can read a web page while downloading your email in the background. This software completely redefines what you can do with a mobile phone. [Source]

OS X is based off of BSD - will this be the case with this phone? If so, it should help developers add any kind of applet they can dream up of and will greatly enhance the usability of the phone. I hope Apple takes this direction.

My third reservation is the technology the phone is using. It is not a 3G phone, so the data rates seen on it will not be as good as the Cingular 8525, which uses the 3G UMTS/HSDPA network that Cingular is rolling out. However, the technologies used in this phone might just be enough to outweigh this drawback. It is also possible Apple decided not to use this new technology for cost reasons as the device is already at the upper end of what people will be willing to pay for a phone (especially on a 2 year contract).

Apple has a real winner with the iPhone. Expect this type of phone to really flourish in the next year or two. Apple has conquered my first two devices - mp3 player and cell phone. Once they get a quality 5 or 6 megapixel camera integrated, along with their famous Apple polished interface - Apple will finally have won the integrated device battle.

  • Welcome to systemBash, a technology and system administration blog by David Drager. If you enjoy this sort of content, can can subscribe to the RSS using the link to the right.