Client Socket lingering after close

J

Jimbo

Hi Guys,
I'm sorry if you have heard this one before but searching the net
hasn't found a solution.

I am using Socket in a client to connect to a server. However, after
I've finished sending and recieving data and call the Close() method the
socket lingers for a couple of minutes. This is demonstrated by using
netstat -a.

I've tried an assortment of SocketOptions (Linger, and DontLinger), but
all with no success.

Is there a definitive answer for the lingering socket? Is it an OS
problem (I am running XP Home), Is it a .Net Framework bug? What do I do
to close connection?

Thanks in advance
James.
 
R

rawCoder

Just a hunch.
If your scenario permits, Have you tried closing the connection from server?

HTH
rawCoder
 
J

Jimbo

Unfortunately no. My client is calling web services on various different
servers.

Just one addition, I am also using the Shutdown method on the Socket :)

James.
 
J

Jimbo

Small update. I haven't solved the problem of lingering sockets. However
a bit of re-architecturing means that I now reuse sockets rather than
creating new ones. I now have one socket per server/port and use the
send and recieve functions on that socket.

The simplified code below shows this in action:

IPAddress ipa = IPAddress.Parse("192.168.0.10");
IPEndPoint ipe = new IPEndPoint(ipa,80);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 30000); s.Connect(ipe);
writeToSocket(s, ipe, "1");
writeToSocket(s, ipe, "2");
writeToSocket(s, ipe, "3");
s.Shutdown(SocketShutdown.Both);
s.Close();


Where writeToSocket contains my code for sending the data down the
socket and waiting for a response.

If anyone does solve the lingering socket problem then let me know.

Cheers
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top