Solved

C# Server Socket and Client Socket

Posted on 2010-09-20
4
1,687 Views
Last Modified: 2012-05-10
I have a client-server application that communicates each other over a socket connection.
My problem is: i don't know how can i signal the server that the client is stopped.
If i use clientSocket.Close() or clientSocket.Disconnect(true), the server is informed
after 1-1.5 minutes and this is to late. On the server side i start a thread for each client that gets connected to the server and i have to know when a client is disconnected. Thanks in advance
0
Comment
Question by:Borsec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 33715145
Did you try to send some data every 5 seconds? When the client is disconnected this should lead to an exception that can be handled.
0
 

Author Comment

by:Borsec
ID: 33715275
Thank you HugoHiasl, but i can't do this because the messages that are trasmitted betwen client and server are very important and don't want to create a standard message to check if the client is disconected. I want the client to notify the server that he is disconected.
0
 
LVL 4

Expert Comment

by:akhileshcoer
ID: 33722087
Before closing send a message to server [CLOSE]. Then you can close connection at your end and client can also.

The only overhead is you need to check this message in every message which is being sent.

-Hope this helps
0
 
LVL 8

Accepted Solution

by:
Volox earned 500 total points
ID: 33751385
If you are using TCP/IP I would suggest using the built-in keep alives that are a part of the TCP/IP stack.  To enable them within .Net you ought to be able to use the Socket.SetSocketOption() method (http://msdn.microsoft.com/en-us/library/1011kecd(v=VS.90).aspx)  However some people have reported issues with getting the option to turn on that way and claim you have to use the lower level Socket.IOControl() method (http://msdn.microsoft.com/en-us/library/8a3744sh(v=VS.90).aspx).  

I had the following samples in my notes from when I was doing some other socket work.  I have to admit that I don't know where I snagged them from (so I apologize that I can't pass on credit) and I've never had the need to implement them so I can't speak to their particular effectiveness.
Socket sck, server;

server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

server.Bind(new IPEndPoint(IPAddress.Any, 5000));
server.Listen(5);
sck = server.Accept();
 
sck.SetSocketOption(SocketOptionLevel.Tcp,SocketOptionName.KeepAlive,0);

// Other (harder) method

public static void SetTcpKeepAlive(Socket socket, uint keepaliveTime, uint keepaliveInterval) 
{ 
// Native structure for keep alive
/*
struct tcp_keepalive { 
    ULONG onoff; 
    ULONG keepalivetime; 
    ULONG keepaliveinterval; 
};
*/

 // marshal the equivalent of the native structure into a byte array 
 uint dummy = 0; 
 byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3]; 
 BitConverter.GetBytes((uint)(keepaliveTime)).CopyTo(inOptionValues, 0); 
 BitConverter.GetBytes((uint)keepaliveTime).CopyTo(inOptionValues, Marshal.SizeOf(dummy)); 
 BitConverter.GetBytes((uint)keepaliveInterval).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); 
 
 // write to Socket IOControl 
 socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); 
 }

Open in new window

0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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