2 * Ian's attempt at writing a socket module for the Spider GUI.
4 * @version 1.00 - 20010418.
6 * Copyright (C) 2001 Ian Norton.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public Licence as published by
10 * the Free Software Foundation; either version 2 of the Licence, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public Licence for more details.
18 * You should have received a copy of the GNU General Public Licence
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Contacting the author :
24 * i.norton@lancaster.ac.uk
25 * http://www.lancs.ac.uk/~norton/
31 public class Connection
38 private Socket socket ;
40 // Socket input and output streams.
41 private InputStream is ;
42 private OutputStream os ;
44 // Piped IO back up the chain.
45 private PipedInputStream pin ;
46 private PipedOutputStream pos ;
49 private ConnectionInput conin ;
50 private ConnectionOutput conout ;
53 public static final String encoding = "latin1"; // "ISO8859_1";
56 private boolean disconnected ;
58 // Default port to use if one isn't specified.
59 // private static final int DEFAULTPORT = 8000 ; // Use this for user client
60 private static final int DEFAULTPORT = 27754 ;
64 * @param PipedInputStream - Stream to read from
65 * @param PipedOutputStream - Stream to send data to
66 * @param Console - Where to send status alerts to.
68 public Connection(PipedInputStream i, PipedOutputStream o, Console c)
70 // Initialise the IO pipes.
74 // Yep, we're definately disconnected.
75 disconnected = false ;
77 // Initialise the Input and Output readers.
78 conin = new ConnectionInput(pos, this) ;
79 conout = new ConnectionOutput(pin, this) ;
84 * @param String - host to connect to. Port after a ':'.
86 public void connect(String s)
88 // Has the socket been initialised?
92 // Work out the hostname and port.
93 if(s.indexOf(":") > - 1)
97 port = Integer.valueOf(s.substring(s.indexOf(":") + 1, s.length())).intValue() ;
99 catch(NumberFormatException ex)
101 System.out.println("Number format exception - bad int in String.") ;
104 s = s.substring(0, s.indexOf(":")) ;
113 // Try and make the connection.
116 socket = new Socket(host, port) ;
118 catch(UnknownHostException ex)
120 System.out.println("Connection: UnknownHostException") ;
121 System.out.println(ex) ;
123 catch(IOException ex)
125 System.out.println("Connection: IOException") ;
126 System.out.println(ex) ;
129 // Get the streams from the connection.
132 is = socket.getInputStream() ;
133 os = socket.getOutputStream() ;
135 catch(IOException ex)
137 System.out.println("Connection: IOException getting the connection streams") ;
138 System.out.println(ex) ;
141 // Start the readers.
145 // Write a "Connected to " message to the multiplexor.
148 // Write disconnected to the PipedOutputStream.
149 String output = "\nConnected to " + host + ":" + port + "\n" ;
150 for(int i=0;i<output.length();i++)
152 pos.write(output.charAt(i)) ;
155 catch(IOException ex)
160 disconnected = false ;
164 * disconnect - disconnect the current connection.
166 public void disconnect()
172 disconnected = true ;
174 conout.disconnect() ;
176 // Write disconnected to the PipedOutputStream.
177 String output = "\nDisconnected from " + host + ":" + port + "\n" ;
178 for(int i=0;i<output.length();i++)
180 pos.write(output.charAt(i)) ;
185 if(socket != null) socket.close() ;
187 catch(IOException ex)
189 System.out.println("Connection: IOException closing socket") ;
190 System.out.println(ex) ;