All pastes #93897 Raw Edit

Miscellany

public text v1 · immutable
#93897 ·published 2006-07-20 21:07 UTC
rendered paste body
import java.net.*;
import java.io.*;

final class Packet
{
	private byte[] HEADER;
	private byte[] DATA;
	
	public Packet()
	{
		HEADER = new byte[3];
		DATA = null;
	}
	
	public Packet(byte ident, byte[] data)
	{
		HEADER = new byte[3];
		HEADER[0] = ident;
		
		setData(data);
	}

	public byte[] getData()
	{
		return DATA;
	}
	
	public void setData(byte[] data)
	{
		this.DATA = data;
		this.HEADER[2] = (byte)data.length;
	}
	
	public byte getIdent()
	{
		return this.HEADER[0];
	}
	
	public void setIdent(byte ident)
	{
		this.HEADER[0] = ident;
	}
	
	public boolean send(BufferedWriter outputStream)
	{		
		try
		{
			for (int i = 0; i < HEADER.length; i++)
				outputStream.write(HEADER[i]);
			
			for (int i = 0; i < DATA.length; i++)
				outputStream.write(DATA[i]);

			outputStream.flush();

			return true;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
	}
}

public class Chatter
{
	static final byte PACKET_CLIENT_HELLO = 0x01;
	static final byte[] PACKET_CLIENT_HELLO_DATA = {0x53, 0x6B, 0x69, 0x4C, 0x4C, 0x6F, 0x72, 0x5A};
	
	static final byte PACKET_SERVER_HELLO = (byte)0x81;
	static final byte[] PACKET_SERVER_HELLO_DATA = {0x52, 0x6F, 0x58, 0x58, 0x6F, 0x72, 0x5A, 0x5A};

	//byte[] nick; //{0x02,0x00,0x08,0x00,0x4F,0x00,0x74,0x00,0x74,0x00,0x6F};
	
	private Socket SOCKET;
	private BufferedReader INPUT_STREAM;
	private BufferedWriter OUTPUT_STREAM;
	
	public Chatter(String address, int port)
	{
		try
		{
			SOCKET = new Socket(address, port);

			this.INPUT_STREAM = new BufferedReader(new InputStreamReader(SOCKET.getInputStream()));
			this.OUTPUT_STREAM = new BufferedWriter(new OutputStreamWriter(SOCKET.getOutputStream()));
		}
		catch (UnknownHostException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Connects the client
	 */
	public boolean connect()
	{
		// send handshake packet
		Packet packetOut = new Packet(PACKET_CLIENT_HELLO, PACKET_CLIENT_HELLO_DATA);

		if (!packetOut.send(OUTPUT_STREAM))
			return false;
		
		//try{int c = INPUT_STREAM.read();System.out.println(c);}catch(IOException e){}
		
		// verify response
		Packet packetIn = getPacket();
		
		return (packetIn != null && packetIn.getIdent() == PACKET_SERVER_HELLO);
	}
	
	/**
	 * Disconnects the client
	 */
	public void disconnect()
	{
		if (!isConnected())
			return;
		
		try
		{
			// close streams
			INPUT_STREAM.close();
			OUTPUT_STREAM.close();
			SOCKET.close();
		}
		catch (IOException e)
		{
			// do nothing
		}
	}
	
	public boolean isConnected()
	{
		return SOCKET.isConnected();
	}
	
	private Packet getPacket()
	{
		char[] buffer = new char[256];
		
		try
		{
			if (INPUT_STREAM.read(buffer, 0, buffer.length) >= 3)	// read more than 3 new chars?
			{
				Packet packet = new Packet();

				// write packet header
				packet.setIdent((byte)buffer[0]);
				int length = (int)buffer[2];
				
				// write packet body
				byte[] data = new byte[length];

				for (int i = 0; i < length; i++)
					data[i] = (byte)buffer[i+3];
				
				packet.setData(data);

				return packet;
			}
			else
				return null;
		}
		catch (IOException e)
		{
			//e.printStackTrace();
			return null;
		}
	}
}