net.petterroea.starterkit
Class ClientSideConnection

java.lang.Object
  extended by net.petterroea.starterkit.ClientSideConnection

public class ClientSideConnection
extends java.lang.Object

Represents the client side connection in a game if you want multiplayer ;)
This is beta code, so i cannot guarantee that it works

Author:
petterroea

Field Summary
private  java.net.Socket client
          The socket that connects to the server
 boolean connected
          True if we are connected
private  java.net.InetSocketAddress endPoint
          Server
 java.util.LinkedList<Packet> in
          Clear the list after reading from it.
(package private)  java.lang.Thread inListener
          The thread that listens
private  boolean listening
          False if packet listeners should stop
 java.util.LinkedList<Packet> out
          LinkedList of packets going out
(package private)  java.lang.Thread outListener
          The thread the sends
private  java.io.BufferedReader reader
          Server to client packet reader
static java.lang.Object synch
          Use this to synchronize
private  java.io.PrintWriter writer
          Client to server packet writer
 
Constructor Summary
ClientSideConnection(java.lang.String ip, int port)
          Constructor for a connection to a server.
 
Method Summary
 Packet getPacket(java.lang.String[] msg)
          Used to recognize packets by their prefix.
 void startListening()
          Starts listening.
 void stopListening()
          Stops listening for packets.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

synch

public static java.lang.Object synch
Use this to synchronize


in

public java.util.LinkedList<Packet> in
Clear the list after reading from it. This is a linkedList with incoming


out

public java.util.LinkedList<Packet> out
LinkedList of packets going out


client

private java.net.Socket client
The socket that connects to the server


listening

private boolean listening
False if packet listeners should stop


inListener

java.lang.Thread inListener
The thread that listens


outListener

java.lang.Thread outListener
The thread the sends


reader

private java.io.BufferedReader reader
Server to client packet reader


writer

private java.io.PrintWriter writer
Client to server packet writer


endPoint

private java.net.InetSocketAddress endPoint
Server


connected

public boolean connected
True if we are connected

Constructor Detail

ClientSideConnection

public ClientSideConnection(java.lang.String ip,
                            int port)
                     throws java.io.IOException
Constructor for a connection to a server. If the IP is localhost, the game will try to connect to a server on the same ip as the server itself

Parameters:
ip - The ip adress
port - The port to be used. REMEMBER TO TELL USERS WHAT PORT THE GAME USES FOR COMMUNICATION. http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers is a important reference. Also, here is a tip: Let the user choose port, but supply a standardport
Throws:
java.io.IOException - if something goes wrong while connecting
Method Detail

stopListening

public void stopListening()
                   throws java.lang.InterruptedException,
                          java.io.IOException
Stops listening for packets. Allways call this before disposing the object, or you will have a listener going forever(as long as the game does)

Throws:
java.lang.InterruptedException - If the listeners could not be joined
java.io.IOException - If unable to close the connection

startListening

public void startListening()
Starts listening. This uses synchronization to make sure nothing is breaking the game. ALL PARTS THAT ACESS THE TWO LINKEDLISTS MUST SYNCHRONIZE TO THE SYNCH OBJECT


getPacket

public Packet getPacket(java.lang.String[] msg)
Used to recognize packets by their prefix. For each new packet you add, you need to declare it here.

Parameters:
msg - The contents of the packet that has just arrived from the serverside
Returns:
A fresh, new packet object containing the data from the packet sendt over the internet