- Timestamp:
- 10/29/08 18:39:38 (16 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Communication.Data/SocketData.cs
r704 r705 95 95 96 96 public bool Connect() { 97 if ( tcpIn == null || !tcpIn.Connected) {97 if (!Alive(tcpIn)) { 98 98 if (tcpListener.Pending()) 99 99 tcpIn = tcpListener.AcceptTcpClient(); 100 100 } 101 if ( tcpOut == null || !tcpOut.Connected) {101 if (!Alive(tcpOut)) { 102 102 tcpOut = new TcpClient(); 103 103 try { … … 112 112 113 113 public void Close() { 114 tcpOut.Client.Close(); 115 tcpOut.Close(); 114 if (tcpOut != null && tcpOut.Connected) { 115 tcpOut.Client.Close(); 116 tcpOut.Close(); 117 } 116 118 tcpOut = null; 117 while (tcpIn.Connected) ; 118 tcpIn.Client.Close(); 119 tcpIn.Close(); 119 if (tcpIn != null && tcpIn.Connected) { 120 tcpIn.Client.Close(); 121 tcpIn.Close(); 122 } 120 123 tcpIn = null; 121 124 tcpListener.Stop(); … … 124 127 125 128 public void Write(string s) { 126 if (tcpOut == null || !tcpOut.Connected) Connect();129 //if (tcpOut == null || !tcpOut.Connected) Connect(); 127 130 NetworkStream outStream = tcpOut.GetStream(); 128 131 … … 134 137 135 138 public string Read() { 136 if (tcpIn == null || !tcpIn.Connected) Connect();139 //if (tcpIn == null || !tcpIn.Connected) Connect(); 137 140 NetworkStream inStream = tcpIn.GetStream(); 138 141 byte[] receivedBytes; … … 172 175 if (messageEnd < count - 3) 173 176 buffer = Encoding.ASCII.GetString(receivedBytes, messageEnd + 3, count - messageEnd - 3); 174 string result = Encoding.ASCII.GetString(receivedBytes, 0, messageEnd); 175 if (result.Equals("CLOSING")) return null; 176 else return result; 177 return Encoding.ASCII.GetString(receivedBytes, 0, messageEnd); 177 178 } 178 179 … … 183 184 return -1; 184 185 } 186 187 private bool Alive(TcpClient c) { 188 if (c == null || !c.Connected) return false; // not connected if null or the property says so 189 try { 190 c.Client.Send(new byte[1], 0, 0); // if c != null && c.Connected make a zero byte send to check if still alive 191 } catch (SocketException e) { 192 return e.NativeErrorCode.Equals(10035); // 10035 == WSAEWOULDBLOCK, if true then connected, otherwise not connected 193 } catch (Exception) { // false regarding any other exception 194 return false; 195 } 196 return c.Connected; // return the property 197 } 185 198 } 186 199 } -
trunk/sources/HeuristicLab.Communication.Operators/TcpNetworkInitiator.cs
r591 r705 46 46 socket.Initialize(config); 47 47 while (!socket.Connect()) ; 48 try { 49 socket.Write("REQUEST_CONNECT"); 50 if (!socket.Read().Equals("REQUEST_CONNECT")) throw new InvalidOperationException("ERROR in TcpNetworkInitiator: Remote host answered with unknown response!"); 51 socket.Write("ACK"); 52 if (!socket.Read().Equals("ACK")) throw new InvalidOperationException("ERROR in TcpNetworkInitiator: Remote host answered with unknown response!"); 53 } catch (Exception) { 54 try { 55 socket.Close(); 56 } catch (Exception) { } 57 return new AtomicOperation(this, scope); 58 } 48 59 49 60 IVariableInfo info = GetVariableInfo("NetworkConnection");
Note: See TracChangeset
for help on using the changeset viewer.