processing.net
Class Client

java.lang.Object
  extended by processing.net.Client
All Implemented Interfaces:
java.lang.Runnable

public class Client
extends java.lang.Object
implements java.lang.Runnable


Field Summary
 java.io.InputStream input
           
 java.io.OutputStream output
           
 
Constructor Summary
Client(PApplet parent, java.net.Socket socket)
           
Client(PApplet parent, java.lang.String host, int port)
           
 
Method Summary
 boolean active()
          Return true if this client is still active and hasn't run into any trouble.
 int available()
          Returns the number of bytes that have been read from serial and are waiting to be dealt with by the user.
 void clear()
          Ignore all the bytes read so far and empty the buffer.
 void dispose()
          Disconnect from the server: internal use only.
 java.lang.String ip()
          Returns the ip address of this feller as a String.
 int read()
          Returns a number between 0 and 255 for the next byte that's waiting in the buffer.
 byte[] readBytes()
          Return a byte array of anything that's in the serial buffer.
 int readBytes(byte[] outgoing)
          Grab whatever is in the serial buffer, and stuff it into a byte buffer passed in by the user.
 byte[] readBytesUntil(int interesting)
          Reads from the serial port into a buffer of bytes up to and including a particular character.
 int readBytesUntil(int interesting, byte[] outgoing)
          Reads from the serial port into a buffer of bytes until a particular character.
 char readChar()
          Returns the next byte in the buffer as a char.
 java.lang.String readString()
          Return whatever has been read from the serial port so far as a String.
 java.lang.String readStringUntil(int interesting)
          Combination of readBytesUntil and readString.
 void run()
           
 void stop()
          Disconnect from the server and calls disconnectEvent(Client c) in the host PApplet.
 void write(byte[] bytes)
           
 void write(int what)
          This will handle ints, bytes and chars transparently.
 void write(java.lang.String what)
          Write a String to the output.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

input

public java.io.InputStream input

output

public java.io.OutputStream output
Constructor Detail

Client

public Client(PApplet parent,
              java.lang.String host,
              int port)

Client

public Client(PApplet parent,
              java.net.Socket socket)
       throws java.io.IOException
Throws:
java.io.IOException
Method Detail

stop

public void stop()
Disconnect from the server and calls disconnectEvent(Client c) in the host PApplet.

Use this to shut the connection if you're finished with it while your applet is still running. Otherwise, it will be automatically be shut down by the host PApplet (using dispose, which is identical)


dispose

public void dispose()
Disconnect from the server: internal use only.

This should only be called by the internal functions in PApplet, use stop() instead from within your own applets.


run

public void run()
Specified by:
run in interface java.lang.Runnable

active

public boolean active()
Return true if this client is still active and hasn't run into any trouble.


ip

public java.lang.String ip()
Returns the ip address of this feller as a String.


available

public int available()
Returns the number of bytes that have been read from serial and are waiting to be dealt with by the user.


clear

public void clear()
Ignore all the bytes read so far and empty the buffer.


read

public int read()
Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there was no byte (although the user should first check available() to see if things are ready to avoid this)


readChar

public char readChar()
Returns the next byte in the buffer as a char. Returns -1, or 0xffff, if nothing is there.


readBytes

public byte[] readBytes()
Return a byte array of anything that's in the serial buffer. Not particularly memory/speed efficient, because it creates a byte array on each read, but it's easier to use than readBytes(byte b[]) (see below).


readBytes

public int readBytes(byte[] outgoing)
Grab whatever is in the serial buffer, and stuff it into a byte buffer passed in by the user. This is more memory/time efficient than readBytes() returning a byte[] array. Returns an int for how many bytes were read. If more bytes are available than can fit into the byte array, only those that will fit are read.


readBytesUntil

public byte[] readBytesUntil(int interesting)
Reads from the serial port into a buffer of bytes up to and including a particular character. If the character isn't in the serial buffer, then 'null' is returned.


readBytesUntil

public int readBytesUntil(int interesting,
                          byte[] outgoing)
Reads from the serial port into a buffer of bytes until a particular character. If the character isn't in the serial buffer, then 'null' is returned. If outgoing[] is not big enough, then -1 is returned, and an error message is printed on the console. If nothing is in the buffer, zero is returned. If 'interesting' byte is not in the buffer, then 0 is returned.


readString

public java.lang.String readString()
Return whatever has been read from the serial port so far as a String. It assumes that the incoming characters are ASCII. If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.


readStringUntil

public java.lang.String readStringUntil(int interesting)
Combination of readBytesUntil and readString. See caveats in each function. Returns null if it still hasn't found what you're looking for.

If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.


write

public void write(int what)
This will handle ints, bytes and chars transparently.


write

public void write(byte[] bytes)

write

public void write(java.lang.String what)
Write a String to the output. Note that this doesn't account for Unicode (two bytes per char), nor will it send UTF8 characters.. It assumes that you mean to send a byte buffer (most often the case for networking and serial i/o) and will only use the bottom 8 bits of each char in the string. (Meaning that internally it uses String.getBytes) If you want to move Unicode data, you can first convert the String to a byte stream in the representation of your choice (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.