473,412 Members | 2,599 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,412 software developers and data experts.

.net socket question

I've been messing around with sockets for a bit and i got most of it down (i
think).
The server side is an asynchronous tcp socket listener with sits and waits
for data.
The client side uses TcpClient to connect.
Connecting, sending and receiving data, error handling, ... all works fine.
The problem is when closing the connection.

Now as i understand it, closing the socket works as follows:
client -> tcp finish command
server -> acknowledges and sets its own finish command
client -> acknowledges again

What i do in code is just this
myTCPClient.close()
Which i think should handle all the internal commands to wrap up (right ?,
they are tpc sockets afterall). This results in the server socket going
into CLOSE_WAIT state, and the client socket going into FIN_WAIT_2 state.
They stay in that state until i close either the server or the client.
I tried making the async socket do a close command too, but that didnt
change anything at all.
I've searched through almost all the documentation i could find, but i must
still be missing something.

Could someone tell me what it is i still need to do to wrap up closing the
sockets ...
Nov 20 '05 #1
2 4257
> Could someone tell me what it is i still need to do to wrap up closing the
sockets ...
This is an issue with the TcpClient, not with windows sockets. What I did to
fix the problem, and still keep the functionality of the TcpClient, is I
inherited the TcpClient, and then exposed the underlying socket. The Close
method on the socket actually works, and it also allows you to check to see
if the socket is still connected. To disconnect, something like this should
close the connection 100%:

If TcpClientEx.IsConnected Then
TcpClientEx.Client.Shutdown(SocketShutdown.Both)
TcpClientEx.Client.Close()
TcpClientEx.Close()
End If
Here is the TcpClientEx Class:

Public Class TcpClientEx
Inherits Net.Sockets.TcpClient

Sub New()
Call MyBase.New()
End Sub

Sub New(ByVal LocalEP As Net.IPEndPoint)
Call MyBase.New(LocalEP)
End Sub

Sub New(ByVal HostName As String, ByVal Port As Integer)
Call MyBase.New(HostName, Port)
End Sub

Public Shadows Property Client() As Net.Sockets.Socket
Get
Return MyBase.Client
End Get
Set(ByVal Value As Net.Sockets.Socket)
MyBase.Client = Value
End Set
End Property

Public ReadOnly Property IsActive() As Boolean
Get
Return Me.Active
End Get
End Property

Private Function IsConnected() As Boolean
If Me.Client.Connected = False Then
Return False
Else
Dim bState As Boolean = Me.Client.Poll(1,
System.Net.Sockets.SelectMode.SelectRead)

If bState And (Me.Client.Available = 0) Then
Return False
Else
Return True
End If
End If
End Function
End Class
HTH,
Jeremy

"Silby" <Si***@nothanks.com> wrote in message
news:xf********************@phobos.telenet-ops.be... I've been messing around with sockets for a bit and i got most of it down (i think).
The server side is an asynchronous tcp socket listener with sits and waits
for data.
The client side uses TcpClient to connect.
Connecting, sending and receiving data, error handling, ... all works fine. The problem is when closing the connection.

Now as i understand it, closing the socket works as follows:
client -> tcp finish command
server -> acknowledges and sets its own finish command
client -> acknowledges again

What i do in code is just this
myTCPClient.close()
Which i think should handle all the internal commands to wrap up (right ?,
they are tpc sockets afterall). This results in the server socket going
into CLOSE_WAIT state, and the client socket going into FIN_WAIT_2 state.
They stay in that state until i close either the server or the client.
I tried making the async socket do a close command too, but that didnt
change anything at all.
I've searched through almost all the documentation i could find, but i must still be missing something.

Could someone tell me what it is i still need to do to wrap up closing the
sockets ...


Nov 20 '05 #2
This seems to do it, thanks .. I'd been struggling for days (and days to
come) !

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:AD*********************@twister.tampabay.rr.c om...
Could someone tell me what it is i still need to do to wrap up closing the sockets ...
This is an issue with the TcpClient, not with windows sockets. What I did

to fix the problem, and still keep the functionality of the TcpClient, is I
inherited the TcpClient, and then exposed the underlying socket. The Close method on the socket actually works, and it also allows you to check to see if the socket is still connected. To disconnect, something like this should close the connection 100%:

If TcpClientEx.IsConnected Then
TcpClientEx.Client.Shutdown(SocketShutdown.Both)
TcpClientEx.Client.Close()
TcpClientEx.Close()
End If
Here is the TcpClientEx Class:

Public Class TcpClientEx
Inherits Net.Sockets.TcpClient

Sub New()
Call MyBase.New()
End Sub

Sub New(ByVal LocalEP As Net.IPEndPoint)
Call MyBase.New(LocalEP)
End Sub

Sub New(ByVal HostName As String, ByVal Port As Integer)
Call MyBase.New(HostName, Port)
End Sub

Public Shadows Property Client() As Net.Sockets.Socket
Get
Return MyBase.Client
End Get
Set(ByVal Value As Net.Sockets.Socket)
MyBase.Client = Value
End Set
End Property

Public ReadOnly Property IsActive() As Boolean
Get
Return Me.Active
End Get
End Property

Private Function IsConnected() As Boolean
If Me.Client.Connected = False Then
Return False
Else
Dim bState As Boolean = Me.Client.Poll(1,
System.Net.Sockets.SelectMode.SelectRead)

If bState And (Me.Client.Available = 0) Then
Return False
Else
Return True
End If
End If
End Function
End Class
HTH,
Jeremy

Nov 20 '05 #3

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

Similar topics

2
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose only job is to read. Any data read (from recv call)...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
6
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
2
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.