UDP Server

As from the UDP Client here is the UDP Server, it is similar to the TCP Server in that it makes connections with a remote client and sends and receives data, but once again this data is not checked to make sure that it has been received (the “fire and forget” setup again)

So to start with we need to create a server socket for the clients to connect to

DatagramSocket theSocket = new DatagramSocket(9999);

since we now have a place for the client to connect to we now need to wait for a client connection request

// create a place for the client to send data too
theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
// wait for a client to request a connection
theSocket.receive(theRecievedPacket);

once a client has requested some data, we can start the process of setting up the response, the response starts of with getting there details, there IP address and also the port number that they wish to talk on ( image attached below of the wireshark request and different port number)

// get the client details
clientAddress = theRecievedPacket.getAddress();
clientPort = theRecievedPacket.getPort();

and then just build up the response to send back and send it

String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
outBuffer = message.getBytes();
 
// send some data to the client
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
theSocket.send(theSendPacket);

here is the full java code

import java.io.*;
import java.net.*;
 
public class UDPServer {
 
	DatagramSocket theSocket = null;
	int serverPort = 9999;
 
	public UDPServer()
	{
		try {
			// create the server UDP end point
			theSocket = new DatagramSocket(serverPort);
 
			System.out.println("UDP Socket (end point) created");
		} catch (SocketException ExceSocket)
		{
			System.out.println("Socket creation error : "+ ExceSocket.getMessage());
		}
	}
 
	public void clientRequest()
	{
		DatagramPacket theRecievedPacket;
		DatagramPacket theSendPacket;
		InetAddress clientAddress;
		int clientPort;
		byte[] outBuffer;
		byte[] inBuffer;
 
		// create some space for the text to send and recieve data 
		outBuffer = new byte[500];
		inBuffer = new byte[50];
 
		try {
			// create a place for the client to send data too
			theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			// wait for a client to request a connection
			theSocket.receive(theRecievedPacket);
			System.out.println("Client connected");
 
			// get the client details
			clientAddress = theRecievedPacket.getAddress();
			clientPort = theRecievedPacket.getPort();
 
			String message = "Server - client sent : " + new String(theRecievedPacket.getData(),0, theRecievedPacket.getLength());
			outBuffer = message.getBytes();
 
			System.out.println("Client data sent ("+message+")");
			// send some data to the client
			theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, clientAddress, clientPort);
			theSocket.send(theSendPacket);
 
		} catch (IOException ExceIO)
		{
			System.out.println("Error with client request : "+ExceIO.getMessage());
		}
		// close the server socket
		theSocket.close();
	}
 
	public static void main(String[] args)
	{
		UDPServer theServer = new UDPServer();
		theServer.clientRequest();
	}
}

if you save that as UDPServer.java and then compile and run

javac UDPServer.java

and then run the java UDPserver program, I have inserted the “— waiting for the client to connect” line because that is where the server will stop waiting, once the client as connected the rest of the output will be outputted.

java UDPServer 
UDP Socket (end point) created
---- waiting for the client to connect
Client connected
Client data sent (Server - client sent : genux)

and then run the UDPClient to connect to the server

java UDPClient 
Client socket created
Message sending is : genux
Client - server response : Server - client sent : genux

Here is the image of a wireshark connection request over the UDP and with the clients port request number.

Request port number
Request port number

UDP Client

A UDP Client is very similar to a TCP Client in that it communicates with a server, but the main difference between the two is that UDP is more of a “fire and forget” setup than lets make sure that the data is sent across. A good example of this would be for a UDP server/client to be something like a radio station, when you are listening to the radio and if you miss part of the song then it does not really matter (apart from if you was singing along!!), but with TCP server/client you want to have all of the data associated with that file.

// create a socket to connect to the server with
DatagramSocket theSocket = new DatagramSocket();
int serverPort = 9999;
// the remote server, in this case I am using the local host but you could use any remote server as long as the UDP server is running on it
InetAddress theServer = InetAddress.getLocalHost();
// and connect the socket to the remote server and port number
theSocket.connect(theServer,serverPort);

and now we build up a message to send to the server, we are using bytes because that is what is sent over with the DatagramPacket’s (these are the packets of data within the wrapped up “letter” that is being sent from the client to the server, the “letter” in this case is the details included within the packet e.g. address to, address from more details are here)

byte[] outBuffer = new byte[50];
String message = "genux";
outBuffer = message.getBytes();

and then to send the information to the server we, since theSocket is already connected to the server then do not need to tell the DatagramPacket where to send.

// build up a packet to send to the server
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length);
// send the data
theSocket.send(theSendPacket);

and now to get a response (if the server gets, and the client does receive one)

// get the servers response within this packet
theReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
theSocket.receive(theReceivedPacket);
 
// the server response is...
System.out.println("Client - server response : "+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));

here is the full code

import java.io.*;
import java.net.*;
 
public class UDPClient {
 
	DatagramSocket theSocket = null;
	int serverPort = 9999;
 
	public UDPClient()
	{
		try {
			theSocket = new DatagramSocket();
 
			// but if you want to connect to your remote server, then alter the theServer address below
			InetAddress theServer = InetAddress.getLocalHost();
			theSocket.connect(theServer,serverPort);
 
			System.out.println("Client socket created");
		}catch (SocketException ExceSocket)
		{
			System.out.println("Socket creation error  : "+ExceSocket.getMessage());
		} 
		catch (UnknownHostException ExceHost)
		{
			System.out.println("Socket host unknown : "+ExceHost.getMessage());
		}
	}
 
	public void connectToServer()
	{
		DatagramPacket theSendPacket;
		DatagramPacket theReceivedPacket;
		InetAddress theServerAddress;
		byte[] outBuffer;
		byte[] inBuffer;
 
		// the place to store the sending and receiving data
		inBuffer = new byte[500];
		outBuffer = new byte[50];
		try {
			String message = "genux";
			outBuffer = message.getBytes();
 
			System.out.println("Message sending is : " + message);
 
			// the server details
			theServerAddress = theSocket.getLocalAddress();
 
			// build up a packet to send to the server
			theSendPacket = new DatagramPacket(outBuffer, outBuffer.length, theServerAddress, serverPort);
			// send the data
			theSocket.send(theSendPacket);
 
			// get the servers response within this packet
			theReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			theSocket.receive(theReceivedPacket);
 
			// the server response is...
			System.out.println("Client - server response : "+new String(theReceivedPacket.getData(), 0, theReceivedPacket.getLength()));
			theSocket.close();
		} catch (IOException ExceIO)
		{
			System.out.println("Client getting data error : "+ExceIO.getMessage());
		}
	}
 
	public static void main(String[] args)
	{
		UDPClient theClient = new UDPClient();
		theClient.connectToServer();
	}
}

if you save that as UDPClient.java and then to compile

javac UDPClient.java

the server is the next part of the puzzle, which is coming next.

TCP Client

As from the TCP Server, this is the client that will connect to the server and output the response from the server.

Since we are the client, we need to know where the server is to connect to, other wise we would not connect to anywhere!!!.. since within this tutorial I am using the local host for running the server as well (you could alter this to where you want to run the server on)

InetAddress theHost = InetAddress.getLocalHost();
// get the local hostname as well
String theHostName = theHost.getHostName();

and with this information you can create a new Socket to connect to the server on the port 9999 (which is the same port number that the server is listening on)

// open the socket connection with the server on port 9999
Socket theSocket = new Socket(theHostName, 9999);

to gain the server data, I am using the ObjectInputStream, with passing the socket from above connection to the server, then just read a line from the ObjectInputStream into a String local variable.

ObjectInputStream ois = new ObjectInputStream(theSocket.getInputStream());
String response = (String)ois.readLine();
// just output the response.
System.out.println("Response was : "+response);

and that is it, all very simple since most of the “interesting” stuff over the networking connections happening under the hood of java, which is just great.

Here is the full source code.

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
 
public class TCPClient {
	InetAddress theHost;
	Socket theSocket;
	int port = 9999;
 
	public TCPClient() {
		try {
			// the local host IP address - where you want to connect to.. 
			theHost = InetAddress.getLocalHost();
			// get the local hostname as well
			String theHostName = theHost.getHostName();
			// open the socket connection with the server on port 9999
			theSocket = new Socket(theHostName, port);
 
			System.out.println("Server connection opened, going to listen");		
 
			ObjectInputStream ois = new ObjectInputStream(theSocket.getInputStream());
			String response = (String)ois.readLine();
 
			System.out.println("Response was : "+response);
			theSocket.close();
		} catch (UnknownHostException ExecHost)
		{
			System.out.println("Unknown Host Error");
		}
		catch (IOException ExceIO)
		{
			System.out.println("Error creatin socket : "+ExceIO.getMessage());
		}
	}
 
	public static void main(String[] args) {
		new TCPClient();
	}
 
 
}

If you save as TCPClient.java and then to compile and run (of course you need to be running in another console the TCPServer since the client will have nothing to connect to !!!)

javac TCPClient.java
java TCPClient

and the output would be

Server connection opened, going to listen
Response was : Hi from the server

you could use wireshark to watch what is happening.

TCP Server

As a follow on from the IP Address in java, the next thing would be a server/client over the internet. This is a small demo on how to create TCP connections over IP Address (TCP basically means that the data sent over the connection will fully be sent, e.g. a linux cd image), shall do one in a UDP which is more of a send data and do not really care if data is sent across the network like radio, if you do miss part of a song it does not really matter.

So to start with, I am using the port number 9999, lets say that you are in a office with different phones running on different extension numbers but using the main telephone number to make calls on, so in this setup the IP address would be main office telephone number (one number to talk on) and the port number would be the different telephones extensions that you can talk on.

To create a server socket you need to pass in the port number as well.

ServerSocket theServerSocket = new ServerSocket(9999);

then to just wait for a connection to try and connect you

Socket sock = theServerSocket.accept();

which waits for a client connection (accept) , since the socket also has the clients IP address you can use that to make sure that you want to accept connections from that IP address if you wanted to, but to output the InetAddress you could use the “sock” from the above code

System.out.println("The client IP address is " + sock.getInetAddress());

the only other thing is that you can send data to the client which using a ObjectOutputStream with passing in the “sock” from the above code

ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeChars("Hi from the server");
oos.close();

Here is the full code

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
 
public class TCPServer {
	// the IP class as such
	InetAddress theTCPServer;
	// port to talk over
	int portNumber = 9999;
	// the socket for the communication to happen on
	ServerSocket theServerSocket;
 
	/* 
	 * Setup the server socket communication 
	 */
	public TCPServer() {
		try {
			// create the server socket on the port number
			theServerSocket = new ServerSocket(portNumber);
			System.out.println("Server created on port : "+portNumber);
		} catch (IOException ExecIO)
		{
			System.out.println("Error creating the server socket : "+ExecIO.getMessage());
		}
	}
 
 
	public void connections()
	{
		try {
			// accept a connection 
			Socket sock = theServerSocket.accept();
			System.out.println("Server accepted connection, send to handler");
 
			// print out the clients IP Address
			System.out.println("The client IP address is " + sock.getInetAddress());
 
			// send the message to the client
			ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
			System.out.println("Server socket opened");
			oos.writeChars("Hi from the server");
			oos.close();
 
			// close the socket
			sock.close();
 
		} catch  (IOException ExecIO)
		{
			System.out.println("Error creating connection : "+ExecIO.getMessage());
		}
	}
 
	public static void main(String[] args) {
		TCPServer theServer = new TCPServer();
		theServer.connections();
	}
}

save as TCPServer.java and then to compile, and run

javac TCPServer.java
java TCPServer

and the output would be something like

Server created on port : 9999
Server accepted connection, send to handler
The client address is /127.0.1.1
Server socket opened

you could use wireshark to watch what is happening.

Internet Address IP

With Java you can use the java.net package to find out details of your PC address or somewhere else (like www.google.com) with using the InetAddress class. The InetAddress is a implementation of the Internet Protocol basically.

To gain the local pc address you can use

InetAddress theHost = InetAddress.getLocalHost();
System.out.println("Local host address: "+theHost);

which would get the local PC address and display it to the screen, for me it would something like

Local host address: genux-laptop/127.0.1.1

you can get the individual parts of the above address, e.g. genux-laptop and 127.0.0.1

System.out.println("Local host name : " +theHost.getHostName());
System.out.println("Local address : "+theHost.getHostAddress());

and the output would be

Local host name :  genux-laptop
Local address : 127.0.1.1

you can also use the InetAddress to obtain remote addresses for example www.google.com so to pull details back from google you could use the getByName function that is within the InetAddress class as.

System.out.println("Host address of google.com : "+ InetAddress.getByName("www.google.com"));

Here is a full code that does the above in full

import java.io.*;
import java.net.*;
 
public class address {
		public static void main(String[] args)
		{
			// a inetaddress class holds details of internet protocol (IP) address
 
			InetAddress theHost = null;
			// you can get the local host (your PC) addresss
			try {
				theHost = InetAddress.getLocalHost();
				System.out.println("Local host address: "+theHost);
			}
			catch (UnknownHostException unknow)
			{
				System.out.println("Error : "+ unknow.toString());
				return;
			}
			// if there is no local host then, oppss!!.. you should really have a local host name
			// also you get can the local host name and address individual 
			String theHostName = theHost.getHostName();
			String theHostAddress = theHost.getHostAddress();
 
			System.out.println("The host name "+ theHostName);
			System.out.println("The host address " + theHostAddress);
 
			try {
				// you can a reverse look up as such by passing in the local name to get the IP address
				System.out.println("Host address by host name : "+ InetAddress.getByName(theHostName));
 
				// if you wanted to get a google host name then just pass in www.google.com
				System.out.println("Host address of google.com : "+ InetAddress.getByName("www.google.com"));
			}
			catch (UnknownHostException unknow)
			{
				System.out.println("Error : "+ unknow.toString());
			}
		}
}

and the output would be

Local host address: genux-laptop/127.0.1.1
The host name genux-laptop
The host address 127.0.1.1
Host address by host name : genux-laptop/127.0.1.1
Host address of google.com : www.google.com/216.239.59.105

Linux applications

When you move from one place to another you need to find out where your local stores are (Tesco’s, Sainburies etc) also it is the same when you move from one operating system to another.

I am going to add to this list where ever possible, the first is the windows application and the second is the Linux equivalent that I use.

Type Windows Application Linux alternative
MP3 player Media player (MP3) Amarok
Video player Media player (MP3 / Videos) Kaffeine
Developer tools Visual Studio
MonoDevelop
QTCreator
KDevelop
MonoDevelop
QTCreator
Text pad Notepad Kate
Office applications (Word, Spread sheets etc)

Office
Open Office
Open Office
FTP Filezilla Filezilla
Web browser Firefox
Opera
IE
Chrome
Firefox
Opera
Chrome

There is a couple of applications in there that also can be used within Windows Operating System as well, so you can try out open source applications without moving operating system to find out if you are able to use that application before moving over to Linux.

k/ubuntu update path

I am upgrading 9.10 to 10.4 Kubuntu (the 9.10 means 2009 and the month of October (10), so thus 10.4 means 2010 and the month of April).

It really could not be any easier just open up a quick run command

ALT + F2

and then type in

update-notifier-kde -d

And it will do the rest for you, update – upgrade your system..

Not much more to say really apart from once I have updated I shall have to say what I think of the 10.4 LTS (Long Term Support) version. Which the LTS has 2 year release cycle, 3 years support for the desktop and 5 years for a server, that is just great!.