1 9 package org.ozoneDB.DxLib.net; 10 11 import java.io.*; 12 import java.net.*; 13 import org.ozoneDB.DxLib.*; 14 import org.ozoneDB.io.stream.ResolvingObjectInputStream; 15 16 17 26 public class DxClient extends DxObject { 27 28 33 protected final static int buffSize = 8192; 34 35 protected Socket sock; 36 37 protected ObjectInputStream in; 38 39 protected ObjectOutputStream out; 40 41 42 public DxClient() { 43 } 44 45 46 public DxClient( String host, int port ) throws IOException { 47 sock = new Socket( host, port ); 48 init(); 49 50 onConnect(); 51 } 52 53 54 public DxClient( Socket s ) throws IOException { 55 sock = s; 56 init(); 57 58 onConnect(); 59 } 60 61 62 protected void init() throws IOException { 63 68 72 out = new ObjectOutputStream( new BufferedOutputStream( sock.getOutputStream(), buffSize ) ); 73 out.flush(); 75 76 in = new ResolvingObjectInputStream( new BufferedInputStream( sock.getInputStream(), buffSize ) ); 77 } 78 79 80 85 public void onConnect() throws IOException { 86 } 87 88 89 93 public void onDeconnect() throws IOException { 94 } 95 96 97 public void send( Object obj ) throws IOException { 98 try { 99 out.writeObject( obj ); 100 } 101 finally { 102 out.flush(); 103 out.reset(); 104 } 105 } 106 107 108 public Object receive() throws IOException, ClassNotFoundException { 109 return in.readObject(); 110 } 111 112 113 116 public boolean objectAvailable() { 117 try { 118 return in.available() > 0; 119 } catch (IOException e) { 120 return false; 121 } 122 } 123 124 125 public synchronized void close() throws IOException { 126 onDeconnect(); 127 in = null; 129 out = null; 132 sock.close(); 133 sock = null; 134 } 135 136 137 public ObjectInputStream inputStream() { 138 return in; 139 } 140 141 142 public ObjectOutputStream outputStream() { 143 return out; 144 } 145 } 146 | Popular Tags |