Bonjour,

J'ai une application cliente:
- ouvrir la connection avec le server host/port
- envoyer une requete au server.
- attendre une réponse du server.

Tout marche bien sauf quand le server a un problem déconnection du réseau ou redémarrer de celui-ci:

Le client obtient un message d'erreur après plus de 5 minutes pourtant j'ai bien mis le ReceiveTimeout à 5s (voir code)


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

private const int BUFFERSIZE = 2048;
private TcpClient tcp;
private NetworkStream stream;

// Open TCP connection
public int Open(string host, int port, int timeOutTcp)
{
  try
  {
    tcp = new TcpClient(host, port);
    tcp.SendBufferSize = BUFFERSIZE;
    tcp.ReceiveBufferSize = BUFFERSIZE;
    tcp.NoDelay = true;
    tcp.ReceiveTimeout = 5000;  // 5 secondes
    tcp.SendTimeout = 5000;
  }
  catch (SocketException)
  {
    throw new MyException("Open: Impossible to connect to: " + host + ", on port: " + port);   
  }
  catch (ArgumentNullException)
  {
    throw new MyException("Open: hostname is null or invalid: " + host);
  }
  catch (ArgumentOutOfRangeException)
  {
    throw new MyException("Open: " + port + " is not between " +
                    System.Net.IPEndPoint.MinPort + " and " + System.Net.IPEndPoint.MaxPort);
  }

  try
  {
    stream = tcp.GetStream();
  }
  catch (ObjectDisposedException)
  {
    throw new MyException("Open: the connection has been closed");
   }
   catch (InvalidOperationException)
   {
     throw new MyException("Open:  is not connected to " + host);
   }
}

/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array.
public static byte[] ReadBytes(Stream stream, int size)
{
  byte[] buffer = new byte[BUFFERSIZE];

  try
  {
    int bytesRead = 0;
    int bytesReadThisTurn;
    while (true)
    {
      try
      {
        bytesReadThisTurn = stream.Read(buffer, bytesRead, Math.Min(size, buffer.Length - bytesRead));

      }
      catch (IOException e)
      {
        throw new MyException("ReadBytes: I/O read stream error, " + e.Message, e);
      }

      if (bytesReadThisTurn <= 0 || bytesRead >= size)
        break;

      bytesRead += bytesReadThisTurn;
      // If we've reached the end of our buffer, check to see if there's
      // any more information
      if (bytesRead >= (buffer.Length - 256) )
      {
        byte[] newBuffer = new byte[buffer.Length * 2];
        Buffer.BlockCopy(buffer, 0, newBuffer, 0, bytesRead);
        buffer = newBuffer;
      }
      if (bytesRead == size)
        break;
    }
    if (buffer.Length == bytesRead)
    {
      return buffer;
    }
    else
    {
      byte[] response = new byte[bytesRead];
      Buffer.BlockCopy(buffer, 0, response, 0, bytesRead);
      return response;
    }
  }
  catch (NotSupportedException e)
  {
    throw new MyException("ReadBytes: operation unsupported, " + e.Message, e);
  }
  catch (ArgumentNullException e)
  {
    throw new MyException("ReadBytes: Offset or length is negative, " + e.Message, e);
  }
  catch (ArgumentException e)
  {
    throw new MyException("ReadBytes: length buffer < offset, " + e.Message, e);
  }
  catch (ObjectDisposedException e)
  {
    throw new MyException("ReadBytes: Network connection closed, " + e.Message, e);
  }
}
Je ne sais pas où il faut mettre une valeur signalant une erreur si la connection avec le server n'est plus valide ou après ce timeout.


Merci