org.openflow.gui.net
Class SocketConnection

java.lang.Object
  extended by org.openflow.gui.net.SocketConnection
All Implemented Interfaces:
java.io.DataInput, java.io.DataOutput

public class SocketConnection
extends java.lang.Object
implements java.io.DataInput, java.io.DataOutput

Provides helper functions for setting up and sending and receiving binary and ASCII data over a TCP connection.

Author:
David Underihll

Field Summary
 CountingDataInputStream in
          input stream to read from the socket
 java.io.DataOutputStream out
          output stream to write to the socket
 java.net.Socket s
          the socket which connects us to the NOX server
 
Constructor Summary
SocketConnection(java.lang.String ip, int port)
          Connect to the client on the specified port.
 
Method Summary
 long getBytesRead()
           
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
 int readInt()
           
 java.lang.String readLine()
           
 long readLong()
           
 java.lang.String readNullTerminatedString()
          Reads to form a string.
static java.lang.String readNullTerminatedString(java.io.DataInput in)
          Reads from in to form a string.
 short readShort()
           
static java.lang.String readString(java.io.DataInput in, int bytesToRead)
          Reads the specified number of bytes from in to form a string.
 java.lang.String readString(int bytesToRead)
          Reads the specified number of bytes to form a string.
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 java.lang.String readUTF()
           
 int skipBytes(int n)
           
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 void writeBoolean(boolean v)
           
 void writeByte(int v)
           
 void writeBytes(java.lang.String s)
           
 void writeChar(int v)
           
 void writeChars(java.lang.String s)
           
 void writeDouble(double v)
           
 void writeFloat(float v)
           
 void writeInt(int v)
           
 void writeLong(long v)
           
 void writeShort(int v)
           
 void writeUTF(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

s

public final java.net.Socket s
the socket which connects us to the NOX server


out

public final java.io.DataOutputStream out
output stream to write to the socket


in

public final CountingDataInputStream in
input stream to read from the socket

Constructor Detail

SocketConnection

public SocketConnection(java.lang.String ip,
                        int port)
Connect to the client on the specified port.

Parameters:
ip - the IP to connect to
port - the TCP port to connect on
Method Detail

readString

public static java.lang.String readString(java.io.DataInput in,
                                          int bytesToRead)
                                   throws java.io.IOException
Reads the specified number of bytes from in to form a string. A zero byte will be interpreted as the end of the string and the remaining bytes will be ignored (still read from in, but ignored).

Parameters:
in - buffer to read from
bytesToRead - number of bytes to read
Returns:
the string formed from the read bytes
Throws:
java.io.IOException

readString

public java.lang.String readString(int bytesToRead)
                            throws java.io.IOException
Reads the specified number of bytes to form a string. A zero byte will be interpreted as the end of the string and the remaining bytes will be ignored (still read, but ignored).

Parameters:
bytesToRead - number of bytes to read
Returns:
the string formed from the read bytes
Throws:
java.io.IOException

readNullTerminatedString

public static java.lang.String readNullTerminatedString(java.io.DataInput in)
                                                 throws java.io.IOException
Reads from in to form a string. A zero byte will be interpreted as the end of the string.

Parameters:
in - buffer to read from
Returns:
the string formed from the read bytes
Throws:
java.io.IOException

readNullTerminatedString

public java.lang.String readNullTerminatedString()
                                          throws java.io.IOException
Reads to form a string. A zero byte will be interpreted as the end of the string.

Returns:
the string formed from the read bytes
Throws:
java.io.IOException

getBytesRead

public long getBytesRead()

readFully

public void readFully(byte[] b)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws java.io.IOException
Specified by:
readFully in interface java.io.DataInput
Throws:
java.io.IOException

skipBytes

public int skipBytes(int n)
              throws java.io.IOException
Specified by:
skipBytes in interface java.io.DataInput
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Specified by:
readBoolean in interface java.io.DataInput
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Specified by:
readByte in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedByte

public int readUnsignedByte()
                     throws java.io.IOException
Specified by:
readUnsignedByte in interface java.io.DataInput
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Specified by:
readShort in interface java.io.DataInput
Throws:
java.io.IOException

readUnsignedShort

public int readUnsignedShort()
                      throws java.io.IOException
Specified by:
readUnsignedShort in interface java.io.DataInput
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Specified by:
readChar in interface java.io.DataInput
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Specified by:
readInt in interface java.io.DataInput
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Specified by:
readLong in interface java.io.DataInput
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Specified by:
readFloat in interface java.io.DataInput
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Specified by:
readDouble in interface java.io.DataInput
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Specified by:
readLine in interface java.io.DataInput
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Specified by:
readUTF in interface java.io.DataInput
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Specified by:
write in interface java.io.DataOutput
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean v)
                  throws java.io.IOException
Specified by:
writeBoolean in interface java.io.DataOutput
Throws:
java.io.IOException

writeByte

public void writeByte(int v)
               throws java.io.IOException
Specified by:
writeByte in interface java.io.DataOutput
Throws:
java.io.IOException

writeShort

public void writeShort(int v)
                throws java.io.IOException
Specified by:
writeShort in interface java.io.DataOutput
Throws:
java.io.IOException

writeChar

public void writeChar(int v)
               throws java.io.IOException
Specified by:
writeChar in interface java.io.DataOutput
Throws:
java.io.IOException

writeInt

public void writeInt(int v)
              throws java.io.IOException
Specified by:
writeInt in interface java.io.DataOutput
Throws:
java.io.IOException

writeLong

public void writeLong(long v)
               throws java.io.IOException
Specified by:
writeLong in interface java.io.DataOutput
Throws:
java.io.IOException

writeFloat

public void writeFloat(float v)
                throws java.io.IOException
Specified by:
writeFloat in interface java.io.DataOutput
Throws:
java.io.IOException

writeDouble

public void writeDouble(double v)
                 throws java.io.IOException
Specified by:
writeDouble in interface java.io.DataOutput
Throws:
java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException
Specified by:
writeBytes in interface java.io.DataOutput
Throws:
java.io.IOException

writeChars

public void writeChars(java.lang.String s)
                throws java.io.IOException
Specified by:
writeChars in interface java.io.DataOutput
Throws:
java.io.IOException

writeUTF

public void writeUTF(java.lang.String s)
              throws java.io.IOException
Specified by:
writeUTF in interface java.io.DataOutput
Throws:
java.io.IOException