All pastes #454368 Raw Edit

Someone

public java v1 · immutable
·published 2007-04-23 15:43 UTC
rendered paste body
/** *SocketConnection.java */package gameFrameWork.communicationcontrol;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;public class SocketConnection implements Connection, Runnable{	//the port we gonna connect to	//this port must be open	//we should get this port from a config file/ as argument	int inputport;	int outputport;	//inputstream	InputStream is;	//outputstream	OutputStream os;	//the inetaddress we are connected to	InetAddress inetaddress;		BufferedReader reader;	MessageHandler handler;	boolean run;		//thread	Thread t;		/**	 * constructor	 * this constructor will wait till a client connection is made to this socket	 * @param inetaddress the inetaddress this connection should connecto to	 * @param handler the messagehanlder this connection should report to	 * @param inputport the port used for input	 * @param outputport the port used for output	 * @throws IOException	 */	public SocketConnection(MessageHandler handler, InetAddress ip,int inputport,int outputport) throws IOException {		System.out.println("starting socketconnection");		this.handler = handler;		this.inetaddress = inetaddress;		inputport = inputport;		outputport = outputport;		run = true;		t = new Thread(this);		t.start();		System.out.println("thread started, sleeping 10 seconds");		try {			Thread.sleep(1000);		} catch (InterruptedException e) {			System.out.println("sleep failed");			e.printStackTrace();		}		System.out.println("done with sleeping, ");		System.out.println("making new outputsocket");				os = new Socket(ip,outputport).getOutputStream();		System.out.println("done: socketconnection");	}	/**	 * @return the inputstream of this connection 	 */	  public InputStream getInputStream(  ){		  return is;	  }	  /**	   * @return the outputstream of this connection	   */	  public OutputStream getOutputStream(  ){		  return os;	  }	public InetAddress getIp() {		return inetaddress;	}	/**	 * is this really needed?	 */	public void setIp(InetAddress ip) {		if(!this.inetaddress.equals(ip)){			throw new UnsupportedOperationException("not implemented yet");		}			}	public void run() {		System.out.println("run started");		//open the serversocket		ServerSocket serversocket = null;		Socket socket = null;		try {			serversocket = new ServerSocket(inputport);			System.out.println("serversocket made");			System.out.println("serversocket is boud:" + serversocket.isBound());			try {				//getting a connectiong (this can take some time)				socket = serversocket.accept();				System.out.println("serversocket caught something");								try {					//get thie inputstream from the socket					is = socket.getInputStream();					//make the bufferedreader					reader = new BufferedReader(new InputStreamReader(is));										//listen to reader, and call handler.messageRecieved if something comes up					while (run){						try {							System.out.println("socket caught something, calling messagerecieved");							handler.messageReceived(reader.readLine());						} catch (IOException e) {							System.out.println("IOError in readerlistener\nThis should be a feature, not a bug");							System.out.println("closing socket");							run = false;						}					}									} catch (IOException e1) {					System.out.println("IOError when getting inputstream from clientsocket");					System.out.println("closing socket");					e1.printStackTrace();				}							} catch (IOException e1) {				System.out.println("IOError when getting clientsocket from serversocket");				System.out.println("closing socket");				e1.printStackTrace();			}					} catch (IOException e1) {			// TODO wat doen we met erros? handler verwittigen?			System.out.println("IOError when setting up serversocket; ");			System.out.println("closing socket");			e1.printStackTrace();		}finally{						//try to close the sockets			try {				serversocket.close();				socket.close();			} catch (IOException e) {				//do nothing			}					}		System.out.println("run: done");			}		/**	 * requests a stop	 * don't know if this will actually work	 * but we can always stop the runnable if we close the connection,	 * and we will get an IOExeption in the run	 */	public void requestStop(){		run = false;	}}