473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Forcing tcp client socket to close and not go into CLOSE_WAIT state

I'm trying to debug my network application ie I want to check my error
handling when the connection is broken.
Im using 127.0.0.1 as the connection address.
Unfortunately, the client socket goes into a CLOSE_WAIT state ad infinitum
and never closes fully until my client and server applications shut down.
Ive disabled lingeroptions.

Is there anything else easy that i can do to force the loopback to close
immediately?

thanks
Nov 17 '05 #1
8 41265
aSocket.Shutdown(SocketShutdown.Both);
aSocket.Close();
if (aSocket.Connected) {
Console.WriteLine("Winsock error: " +
Convert.ToString(System.Runtime.InteropServices.Ma rshal.GetLastWin32Error())
);
}

AFAIK
CLOSE_WAIT Indicates passive close. Server just received first FIN from a
client.

Also the issue may occur when the client properly closed the connection (FIN
has been sent), but the server still has its socket open. This could be the
result of one instance (among all threads or processes) of the socket not
being closed.

Also you can use 'sniffer' tool to see how your connection is being closed
in the low level...
--
Vadym Stetsyak aka Vadmyst

"Claire" <cz*********@community.nospam> wrote in message
news:uV**************@tk2msftngp13.phx.gbl...
I'm trying to debug my network application ie I want to check my error
handling when the connection is broken.
Im using 127.0.0.1 as the connection address.
Unfortunately, the client socket goes into a CLOSE_WAIT state ad infinitum
and never closes fully until my client and server applications shut down.
Ive disabled lingeroptions.

Is there anything else easy that i can do to force the loopback to close
immediately?

thanks

Nov 17 '05 #2
It is possible to similar to a System.Net.Sockets.TcpClient where Im unable
to get at the socket itself?
Im also using a System.Net.Sockets.NetworkStream attached to this.

private System.Net.Sockets.TcpClient myClient;

private System.Net.Sockets.NetworkStream myStream;

public void Disconnect()

{

if (myClient == null) return;

LogDebug("Disconnect", "Closing socket");

// closing stream also closes socket

myStream.Close();

myClient = null;

myStream = null;

}
Nov 17 '05 #3
In the code sample you miss myClient.Close();

In order to access the underlying socket object you have to inherit from
TcpClient class and override its Client property...

--
Vadym Stetsyak aka Vadmyst

"Claire" <cz*********@community.nospam> wrote in message
news:#0**************@TK2MSFTNGP10.phx.gbl...
It is possible to similar to a System.Net.Sockets.TcpClient where Im unable to get at the socket itself?
Im also using a System.Net.Sockets.NetworkStream attached to this.

private System.Net.Sockets.TcpClient myClient;

private System.Net.Sockets.NetworkStream myStream;

public void Disconnect()

{

if (myClient == null) return;

LogDebug("Disconnect", "Closing socket");

// closing stream also closes socket

myStream.Close();

myClient = null;

myStream = null;

}

Nov 17 '05 #4
> Unfortunately, the client socket goes into a CLOSE_WAIT state ad
infinitum
and never closes fully until my client and server applications shut down.
From the RFC http://iptables-tutorial.frozentux.n...er/rfc793.txt:

CLOSE-WAIT - represents waiting for a connection termination request
from the local user.

Looks like either your client or server application is not closing its
socket properly. You'd have to post a working sample for anyone to
examine why, though.
Ive disabled lingeroptions.

In a normal scenario, why change lingeroptions?

Greetings,
Wessel
Nov 17 '05 #5
Hi Wessel

Looks like either your client or server application is not closing its
socket properly. You'd have to post a working sample for anyone to
examine why, though.


private System.Net.Sockets.TcpClient myClient;

private System.Net.Sockets.NetworkStream myStream;

public TCPClientDriver(string Address, int PortNum)

{

string fnName = "Constructor";

LogDebug(fnName, "Attempting to create and open TCP client");

try

{

myClient = new System.Net.Sockets.TcpClient(Address, PortNum);

myStream = myClient.GetStream();

LogDebug(fnName, "Success");

InitializedOK = true;

}

catch(Exception e)

{

WinsockError = e.Message;

LogDebug(fnName, "Failed");

LogException(fnName, e);

}

}//public TCPClientDriver(string Address, int PortNum)

Ive disabled lingeroptions.

In a normal scenario, why change lingeroptions?

Only in a desperate random attempt to get the socket to close fully for
debug purposes.

Nov 17 '05 #6
> private System.Net.Sockets.TcpClient myClient;

private System.Net.Sockets.NetworkStream myStream;

public TCPClientDriver(string Address, int PortNum)

{

string fnName = "Constructor";

LogDebug(fnName, "Attempting to create and open TCP client");

try

{

myClient = new System.Net.Sockets.TcpClient(Address, PortNum);

myStream = myClient.GetStream();

LogDebug(fnName, "Success");

InitializedOK = true;

}

catch(Exception e)

{

WinsockError = e.Message;

LogDebug(fnName, "Failed");

LogException(fnName, e);

}

}//public TCPClientDriver(string Address, int PortNum)

That's the client part of creating the socket, right? Do you have a
sample of how you close the socket client-side? Are you in control of the
server too?
In a normal scenario, why change lingeroptions?

Only in a desperate random attempt to get the socket to close fully for
debug purposes.

Right. Should be possible without lingeroption, tho.

Greetings,
Wessel
Nov 17 '05 #7
> That's the client part of creating the socket, right? Do you have a
sample of how you close the socket client-side? Are you in control of the
server too? I'm not trying to close the server. It's the server's handling of loss of
network client (error handling) that Im trying to test during my file
transfer protocol.
The above code shows samples of both creating/opening and closing the client
socket. Thas is, I've included the constructor and the Disconnect function.
There's not much to go wrong really lol.
Yes. Im writing both sides of the code. At this early stage, the client runs
in the same process as the application start, no threading. The server
generates a new thread for each connection. Each thread synchronously
monitors whether data is available. Ive put no code in to disconnect the
server yet.

In a normal scenario, why change lingeroptions?

Only in a desperate random attempt to get the socket to close fully for
debug purposes.

Right. Should be possible without lingeroption, tho.


Fair enough :)

thank you wessel
Nov 17 '05 #8
> I'm not trying to close the server. It's the server's handling of loss of
network client (error handling) that Im trying to test during my file
transfer protocol.

Well that explains it. The RFC says:

WAIT_CLOSE represents waiting for a connection termination request
from the local user.

If you pull out the network connection, depending on how you do it, the
TCP endpoints may survive for when the connection comes back up. In your
case, the WAIT_CLOSE state says that one of the programs has initiated TCP
shutdown. The followup packet to acknowledge shutdown has not arrived,
but the server waits for approx 2 minutes for it.

This doesn't mean the server is "occupied" during this period. Other
clients can connect to the same server socket (but you knew that already,
of course. :P)

Anyways consider looking at the RFC:
http://iptables-tutorial.frozentux.net/other/rfc793.txt

Good luck,
Wessel
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: José Carlos | last post by:
Hi. I am starting a server socket program. I send data from client socket but the server don´t recived anything. This is my python code: Server: import socket
0
by: tom | last post by:
Hi all, I hope I can explain my problem clearly. I have a .NET c# windows application calling a localhost web service using asynchronous callback. While still waiting for the result, the user...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
5
by: Morten | last post by:
How do I detect if a client socket is no longer connected to the listen tcp socket ? I have tried with (just an example): --------------------- Socket tcpSocket; ...
0
by: amita | last post by:
I have a Client (a TcpClient object) established connection with the Server(a TcpListener). I want that when the Server goes down, the TcpClient detects this disconnection and when the Server is back...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
2
by: O.B. | last post by:
In the following code snippet, the thread successfully makes it to the line where it waits for data to be received. Then the client closes the connection. The thread wakes up and returns from the...
2
by: Sparky74 | last post by:
Hi. Can somebody please help me - I cannot get my TCP client application to close its socket one I attempt a send. It will close the socket fine if I don't call Send or BeginSend. Soon as I do, it...
1
by: Miroslav Endys | last post by:
I have two apps. Client and Server (both are windows console apps). Im using Client Socket of this definition AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp and Im using async...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.