TCP home server test client

With the previous post setup I’ve written a very basic example of how to test the server. At the moment, there are no back and forth communications, just a simple send of a 4 byte header.

Run the server program first, and then run the test. These can actually be run in the same order from Eclipse.

The good news is, this also works over the internet. The ‘Global’ in the code hold a string of my web server address and also the port that my router has open.

So, all working as expected.

package com.wlgfx.client;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class WLGfxClientTest {

    public static void main(String[] args) throws IOException {
		String header = "HEAD";
		byte[] bytes = header.getBytes();
		
		InetAddress address = InetAddress.getByName(Global.server_address);
		String ip = address.getHostAddress();
		
		System.out.println("Server at: " + ip);
		Socket socket = new Socket(address, Global.server_port);
		
		OutputStream os = socket.getOutputStream();
		os.write(bytes, 0, bytes.length);
		os.close();
		
		socket.close();
	}
}

The output from the server will be:

WLGfx Home Server - Starting up
Shutdown hook attached
TCP Server thread started...
TCP Incoming socket handler
Got header: HEAD

Leave a Reply

Your email address will not be published.