Sockets - Not closing
Sockets - Not closing
Sockets - Not closing
Sockets - Not closing
Sockets - Not closing
Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing
Sockets - Not closing Sockets - Not closing
Sockets - Not closing
Go Back  Xtreme Visual Basic Talk > > > Sockets - Not closing


Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2005, 04:17 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default Sockets - Not closing


Hi,

I've recently integrated sockets into my application, but am facing a few problems...

On both the client, which sends a message, and the remote which recieves the message, close the connection (client.close) when their tasks have been completed.

Upon loading netstat -a, it shows that they are still established, or sitting on close_wait. This seems to pile up until the remote is no longer able to listen!

I am running the client program as a service as I need it to send a message upon load, but also to recognise a shutdown/login event to send a goodbye message to the remote. Shutting down the server AND shutting down the PC seem to have no effect, and the port remains open on the client and remote(if I only shut the service down) or the remote should I shutdown the client.

Any ideas?

NOTE: Both the remote and client applications MUST stay active, it is just the connections that need to be killed.

Code:
server = "194.81.145.101"
        dateset = Day(Now) & "/" & Month(Now) & "/" & Year(Now)

        Dim sHostName As String
        Dim i As Integer
        sHostName = Dns.GetHostName()
        Dim ipE As IPHostEntry = Dns.GetHostByName(sHostName)
        Dim IpA() As IPAddress = ipE.AddressList
        For i = 0 To IpA.GetUpperBound(0)
            netad = IpA(i).ToString
        Next

filecheck:
        Dim sr As StreamReader = New StreamReader("c:\userinfo.txt")
        info = sr.ReadLine()
        
        message = machinename & "," & dateset & "," & info & "," & netad
        Dim client As New TcpClient(server, port)

        Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

        Dim stream As NetworkStream = client.GetStream()

        stream.Write(data, 0, data.Length)

        
        ' Close everything.
        client.Close()
Reply With Quote
  #2  
Old 11-18-2005, 04:23 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

Hmmm...
I always use sockets instead of TCPClient.
Is there shutdown method like there is in sockets?
Something like:
Code:
Socket.Shutdown(SocketShutdown.Both)
Reply With Quote
  #3  
Old 11-18-2005, 05:31 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default

Thanks for the info..

I'd rather keep the TCPClient as its taken me a long time to get to this point!

I've looked at adding a class like this to my project, but it seems not to work.

Code:
Imports System.Net.Sockets

Public Class socketkill
    ' This derived class demonstrates the use of three protected properties belonging to the TcpClient Class.
    Inherits TcpClient

    Public Sub New()
    End Sub 'New

    Public Sub UsingProtectedMethods()

        Me.Dispose(True)
        Me.Finalize()
        End Sub
End Class
I'd hoped that the dispose method would have worked.

Ideas?
Reply With Quote
  #4  
Old 11-18-2005, 05:57 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

I'm fresh out, sorry mate.
You would assume that .Close would work.
Reply With Quote
  #5  
Old 11-18-2005, 06:11 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Unhappy

Exactly!

Trouble is, if the connections stack up then it'll get worse if it got distributed out to more than the test area.

Its annoying as client.close is on both the listener and the sender.
Reply With Quote
  #6  
Old 11-18-2005, 06:20 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

Do the connections eventually close if you shut down the processes using them?
Reply With Quote
  #7  
Old 11-18-2005, 06:25 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default

Well they enter a close wait phase on the remote, which is the key as that is where it stacks up. I'm assuming they are still active on the port tho?
Reply With Quote
  #8  
Old 11-18-2005, 07:16 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default

The only thing I can think of is somehow killing the connections autonomously

I found gettcptable which I've put in to .net, but I can't really see a way of closing the connection.
Reply With Quote
  #9  
Old 11-18-2005, 07:54 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

Are you creating a new TCPListener for each connection you establish?
Reply With Quote
  #10  
Old 11-18-2005, 08:25 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default

Yes I am.

The idea is that multiple clients will hit the remote.

I had to restart the connection so that the remote would be ready for the next hit. I think I found that if I didn't, it wouldn't accept another connection.

Currently, when the remote recieves the connection, it will client.stop and server.stop, then loop to the top and re-create the listener.

Maybe if I move the loop so it only closes and reopens the client? Leaving the server open?

Code:
Dim port As Int32 = 13000
        Dim localAddr As IPAddress = IPAddress.Parse("194.81.145.101")
        Dim tod


starter:
        Dim server As New TcpListener(localAddr, port)

        ' Start listening for client requests.
        server.Start()

        ' Buffer for reading data
        Dim bytes(1024) As [Byte]
        Dim data As [String] = Nothing

        ' Enter the listening loop.
        While True
            'MsgBox("Waiting for a connection... ")

            ' Perform a blocking call to accept requests.
            ' You could also user server.AcceptSocket() here.
            Dim client As TcpClient = server.AcceptTcpClient()

            data = Nothing

            ' Get a stream object for reading and writing
            Dim stream As NetworkStream = client.GetStream()

            Dim i As Int32

            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)

            While (i <> 0)
                ' Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                'MsgBox([String].Format("Received: {0}", data))
                detect = Microsoft.VisualBasic.Left(data, 3)
                afterdet = Microsoft.VisualBasic.Mid(data, 5, 8)
                SqlConnection1.Open()

                If data <> "DIE" Then
                    If detect = "BYE" Then
                        SqlCommand1.CommandText = "DELETE FROM CATStest WHERE (information LIKE '%" + afterdet + "%')"
                        SqlCommand1.ExecuteNonQuery()
                    Else
                        Dim mins = Minute(Now)
                        If mins < 10 Then
                            mins = "0" & Minute(Now)
                        End If
                        tod = "," & Hour(Now) & ":" & mins
                        SqlCommand1.CommandText = "DELETE FROM CATStest WHERE (information LIKE '%" + afterdet + "%')"
                        SqlCommand1.ExecuteNonQuery()
                        SqlConnection1.Close()

                        SqlConnection1.Open()
                        SqlCommand1.CommandText = "INSERT INTO CATStest (information) VALUES ('" + data + tod + "')"
                        SqlCommand1.ExecuteNonQuery()
                        SqlConnection1.Close()

                        SqlConnection1.Open()
                        SqlCommand1.CommandText = "INSERT INTO CATSArchive (information) VALUES ('" + data + tod + "')"
                        SqlCommand1.ExecuteNonQuery()
                        SqlConnection1.Close()
                    End If
                    SqlConnection1.Close()
                Else
                    client.Close()
                    server.Stop()
                    Exit Sub
                End If

                ' Process the data sent by the client.

                data = data.ToUpper()

                

            End While

            ' Shutdown and end connection

            client.Close()


        End While
        
        server.Stop()

        GoTo starter
Reply With Quote
  #11  
Old 11-18-2005, 09:07 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

There's the problem then.
Only listen on one port and have all the clients connect to that same port.

Declare the listener as a module level variable and then setup a timer to check it every now and again (500 ms or so worked fine for me on my last project). If there are pending connections, accept them and do what you need to before closing the connection. Leave the tcplistener open as long as your program is running and only close it when the program closes (or you no longer want to accept any connections).
Reply With Quote
  #12  
Old 11-18-2005, 10:19 AM
mcslemon mcslemon is offline
Centurion
 
Join Date: Jun 2003
Location: Hampshire, England
Posts: 108
Default

Thanks for the info.

I'll give that ago.. tho its Friday and home time soon!!
Reply With Quote
  #13  
Old 12-15-2005, 03:34 PM
Zumwalt Zumwalt is offline
Contributor
 
Join Date: Sep 2003
Location: Tampa, FL
Posts: 474
Default

This might help:
http://www.microsoft.com/downloads/d...displaylang=en

Basically, there is a client / server tcp application in the mix that does what you want. You can read through to the code to see how its layed out, very basic, but supports unlimited connections and it cleans them properly.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Sockets - Not closing
Sockets - Not closing
Sockets - Not closing Sockets - Not closing
Sockets - Not closing
Sockets - Not closing
Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing Sockets - Not closing
Sockets - Not closing
Sockets - Not closing
 
Sockets - Not closing
Sockets - Not closing
 
-->