Simple messenger, server crashing, when one of the clients is killed in task manager

已鎖定 Simple messenger, server crashing, when one of the clients is killed in task manager

  • 27 mai 2011 08:01
     
     

    Hello

    So, I'm trying to modify messenger, which i found here

    http://www.csharphelp.com/2007/05/simple-messenger-developed-in-net-using-c/

     

    The problem is, when I kill one of the clients is killed in task manager (simulating situation that internet dies or something like that), and I try to send message from another client

     

    Here is the code

    http://pastebin.com/JR5vq22i

     

    So, what I have done: I tried to catch exception at server.SendTo, wo when it crashes I remove the client from iplist

     

    But then another exception appears here: receive = server.ReceiveFrom(data, ref klient);

    Which is kinda weird, because klient (client, I kinda mixed languages when naming :() was removed from listaip, so it shouldn't crash here...

    When I catch exception here, server console starts to SPAM "Waiting for message" text. It REALLY spams console.

    After that I can't send any message from another client.

    The exception is "An existing connection was forcibly closed by the remote host"

     

    Any suggestions?

    If you need client code too, just tell me, I could upload whole project somewhere if needed


    • Mutat de Aspen VJ 30 mai 2011 06:12 (From:Visual C# General)
    •  

Toate mesajele

  • 28 mai 2011 08:20
     
     

    Here are some notes which may help:

    1) Yes, you're removing klient from listaip but your ReceiveFrom() line is using a new EndPoint...not one from your list.  What you should do here is if there's an exception in ReceiveFrom(), catch it and use continue to go back to the beginning of the while loop...so that you can start listening for another packet.

    2) There's no reason to keep recreating the byte[1024] every time the while loop executes...that just generates garbage for the garbage collector.  You can simply call Array.Clear() at that point int the loop instead.

    3) UDP is an unreliable transport protocol which I assume you know.  For a chat room that seems odd because it would suck to drop packets (and conversation)...and coding up a send/acknowledge system in UDP may be more effort than just using TCP.

    4) A List<T> would probably be more useful in the long run than an ArrayList for the clients.  In addition, a foreach statement (at least in my opinion) would be cleaner than the second while loop.

    Without seeing the entire project and being able to test it out locally it's going to be hard to determine why it's spamming (if it still does after you fix #1).  If you decide to upload it somewhere I can take a look at it tomorrow...it's 4:20 AM here :)

    HTH,

    ShaneB

  • 30 mai 2011 06:11
     
     

    Hi Raston89,

    Based on your description, your issue is more related to Messenger Development. So I wll move this thread to Windows Live Messenger Client: Development. As that forum it talks about developer topics related to Messenger activities. Thanks for understanding.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 30 mai 2011 15:01
     
     

    Vin,

    This isn't related to Windows Live Messenger...it's a Sockets/C# question :)

    ShaneB

  • 31 mai 2011 08:54
     
     

    Hi again

    sorry, I was out whole weekend so couldn't answer you :)

     

    http://www.speedyshare.com/files/28731605/Messenger.zip

    here is whole project (trust me, no viruses :))

    ConsoleApplication is server

    WindowsForm is client

     

    I've used your tip #1, and it works. But in debug mode, after killing one client, server starts lagging (but not losing packets), but it works fine using exe files.

    I've found this project on the net, but it's bugged as hell, made a mistake of not writing my own code all by myself. Now I'm trying to make it work :)

    #2 advice, done

    It's a school project, there will be 5 clients max I think, so maybe udp won't fail :) if it won't work I will make it tcp

     

    Now I found new bug, which bothers me a bit.

    When I open client, but server is not running and I click join - client crashes.

    How can I check if server is running?

  • 31 mai 2011 18:23
     
     

    Since UDP is a connectionless protocol, I believe you'll have to trap the SocketException in your Receive method.  If it were TCP you could just check the Connected property.  Of course, to make things easier you may want to just use the UDPClient class...I'm sure you can find some good examples of using it on CodeProject.com :)

    ShaneB