10 public class TCP implements Runnable{
\r
11 private Socket $sock = null;
\r
12 private InputStream $in = null;
\r
13 private OutputStream $out = null;
\r
16 public TCP(String $server, int $port)throws Exception{
\r
17 $sock = new Socket($server, $port);
\r
18 $in = $sock.getInputStream();
\r
19 $out = $sock.getOutputStream();
\r
20 new Thread(this).start();
\r
25 while($sock != null){
\r
28 }catch(Exception $e){$e.printStackTrace();}
\r
31 public void send(String $msg){
\r
33 byte[] $len = Helper.int2ba($msg.length());
\r
36 $out.write($msg.getBytes());
\r
38 }catch(Exception $e){$e.printStackTrace();}
\r
41 public void receive()throws Exception{
\r
42 int $len = Helper.ba2int(Helper.readBA($in, 4));
\r
43 String $msg = new String(Helper.readBA($in, $len));
\r
44 System.out.println($msg);
\r
47 public void shutdown(){
\r
49 send("CLIENT shutting down...");
\r
54 }catch(Exception $e){$e.printStackTrace();}
\r