Free cookie consent management tool by TermsFeed Policy Generator

Changeset 705 for trunk


Ignore:
Timestamp:
10/29/08 18:39:38 (16 years ago)
Author:
abeham
Message:

[TICKET #297] fixed some bugs in the TCP connection

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Communication.Data/SocketData.cs

    r704 r705  
    9595
    9696    public bool Connect() {
    97       if (tcpIn == null || !tcpIn.Connected) {
     97      if (!Alive(tcpIn)) {
    9898        if (tcpListener.Pending())
    9999          tcpIn = tcpListener.AcceptTcpClient();
    100100      }
    101       if (tcpOut == null || !tcpOut.Connected) {
     101      if (!Alive(tcpOut)) {
    102102        tcpOut = new TcpClient();
    103103        try {
     
    112112
    113113    public void Close() {
    114       tcpOut.Client.Close();
    115       tcpOut.Close();
     114      if (tcpOut != null && tcpOut.Connected) {
     115        tcpOut.Client.Close();
     116        tcpOut.Close();
     117      }
    116118      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      }
    120123      tcpIn = null;
    121124      tcpListener.Stop();
     
    124127
    125128    public void Write(string s) {
    126       if (tcpOut == null || !tcpOut.Connected) Connect();
     129      //if (tcpOut == null || !tcpOut.Connected) Connect();
    127130      NetworkStream outStream = tcpOut.GetStream();
    128131
     
    134137
    135138    public string Read() {
    136       if (tcpIn == null || !tcpIn.Connected) Connect();
     139      //if (tcpIn == null || !tcpIn.Connected) Connect();
    137140      NetworkStream inStream = tcpIn.GetStream();
    138141      byte[] receivedBytes;
     
    172175      if (messageEnd < count - 3)
    173176        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);
    177178    }
    178179
     
    183184      return -1;
    184185    }
     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    }
    185198  }
    186199}
  • trunk/sources/HeuristicLab.Communication.Operators/TcpNetworkInitiator.cs

    r591 r705  
    4646      socket.Initialize(config);
    4747      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      }
    4859
    4960      IVariableInfo info = GetVariableInfo("NetworkConnection");
Note: See TracChangeset for help on using the changeset viewer.