Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

TcpClient TcpListener problem

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.
0
noulouk
Asked:
noulouk
  • 2
2 Solutions
 
cyberdevil67Commented:
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.

Cheers!
0
 
nouloukAuthor Commented:
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.
0
 
cyberdevil67Commented:
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

http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c6039/


0
 
sourabhkothariCommented:
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.

http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c7695/
http://www.codeguru.com/Csharp/Csharp/cs_network/sockets/article.php/c8781/


Skot
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now