Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

csharp:networking_mit_csharp

C# und Netzwerk

LinkHow to set up TCPListener to always listen and accept multiple connections
LinkBrokered Authentication: Kerberos
LinkInternetauthentifizierung (Kerberos und NTLM)
LinkNegotiateStream-Klasse
LinkWindows-Service mit Worker-Thread
ThreadpoolLinkWenn man schon mit Worker-Threads arbeiten will…

Auch noch wichtig


You are calling ReadToEnd on the receiver - however, the sender has not said that it has finished sending - the sender only closes the stream etc after the incoming message that will never come; basically, you've deadlocked yourself.

There are many ways to approach this type of problem, including:

    using a length-prefix to a message so the receiver knows how much data to expect in advance (and can limit itself accordingly)
    using a terminator to a message (maybe a cr/lf) so the receiver knows it has reached the end of a message; for a text protocol, ReadLine may then be useful
    closing the outbound stream

the last is simple if you are only sending one message; just use:

socket.Shutdown(SocketShutdown.Send);

after sending the message, and the receiver will know it isn't going to get any more data on the socket. Note also that you must clear down things like the crypto stream before doing this, to ensure the data is actually all sent (and not buffered and lost).

Personally, I tend to use the first option (length prefix), mainly because I'm usually dealing in multi-message binary protocols.

Quelle: http://stackoverflow.com/questions/9659893/c-sharp-send-receive-messages-via-networkstream-simple-code-but-does-not-wo

Offensichtlich bedeutet ein NegotiateStream.EndRead nicht, dass die Nachricht auch als abgeschlossen interpretiert wird, sondern lediglich, dass der Buffer soweit es geht gefüllt wird. Wenn man mit BeginRead loslegt, obwohl der Absender nix mehr schickt, landet man in einer Deadlock!

csharp/networking_mit_csharp.txt · Zuletzt geändert: 2016/09/20 23:25 von ronny