TcpClient TcpListener problem

Posted on 2005-04-26
Last Modified: 2008-01-09
Hi Experts,

I build a little chat with TcpClient and TcpListener.
All works fine, but my problem is when I close my server or my client.
I use BeginRead to listen an incoming message and I get IO exception immediately after my form closing.

So, it seems that server and client always communicate with each other(always connected).
Could you tell me if I'm right ? If so, does  it use a lot of bandwith because I'd like to deploy my app from internet.

Is there a way to only listen incoming message ? I mean the client listens incoming message internally (sort of disconnected  communication): for example, the server sends a message to the client message box and the client only listens to incoming messages in message box without always asking to the server. I think it's possible to do.

Thanks in advance for helping me.
Question by:noulouk
    LVL 9

    Expert Comment

    Hi noulouk,

     You need to handshale properly, so that when you server the client knows that the connection has been broken. by sending status codes back and forth you can also display specefic error messages as well, like service not running or service not available or maintenance or anything you like.

    LVL 9

    Author Comment

    Thanks, I know how to catch my IO exception.

    My real problem is more about bandwith usage and something like disconnected listener to use the minimum bandwith.
    LVL 9

    Accepted Solution

    I am not talking about catching exceptions.

    Here is psuedo code.

    While(not exitcode)
       receive command
      if (command is quit)
        send quit command to client

    What I am saying is look at ftp, email etc. They are all based on a set of commands, you being a connection you close a connection. So your code will need to close the tcpconnection if the client closes, and tell the server that we are ending the session. And if the server closes down then you need to tell the client that the session has ended.

    With smtp or pop you open the connection and it stays open for a period of time before timing the connection out, so in this case it can accpet other comands until the quit command is received then closes the connection down.

    IOException only traps problems in the interim, your problem exists because you are not handling a session communication properly.

    For more detail on this try this link

    LVL 1

    Assisted Solution

    for the bandwidth problem u'll have to use asynchronous socket listener.
    both the articles below will help you in writing asychronous client and server.
    u can also close the socket before closing the client or server so that the other side receives an exception with specific code ( it's something like 10054)
    and whenever this errorcode is received u tell the user that the server or client is disconnected now.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now