Netduino home hardware projects downloads community

Jump to content


Photo

Socket server stops accepting after 5 connections


  • Please log in to reply
1 reply to this topic

#1 marksmanaz

marksmanaz

    Member

  • Members
  • PipPip
  • 15 posts

Posted 12 September 2013 - 12:19 AM

Hello,

 

I have been working on this and I need some help. I have this web server setup but it stops accepting requests after 5 or 6 connections have been made. I tried using dispose in every way possible to make sure there are no lingering connections but I am thinking that it is a garbage collection problem I read somewhere. I don't know how to handle that. Most of the code I use has been converted from C#. It does everything I want to do but it stops accepting after several connections. It will respond to pings. It will send outbound request on sensor interrupt events after it stops accepting so the problem is solely with the listening socket. I have tried static and dynamic IP addresses as well. No debug errors come up other than a socket exception after the socket has stopped for 2 or 3 minutes.

 

My guess I need to make the listing socket a separate thread or class but I don't know how to do that in VB.

 

Any help is appreciated thank you.

    Public Class WebServer        Implements IDisposable        Dim Doorstate As Boolean = False        Private Shared iface As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces(0)        Private listeningSocket As Socket        Public Sub New()            Dim myDns As String = "192.168.123.1"            iface.EnableStaticIP("192.168.123.144", "255.255.255.0", "192.168.123.1")            setsocket()        End Sub        Protected Sub setsocket()            Dim listeningSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)            listeningSocket.Bind(New IPEndPoint(IPAddress.Any, 8081))            'Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()(0).IPAddress)            listeningSocket.Listen(10)            While True                Dim clientSocket As Socket = listeningSocket.Accept                ProcessClientRequest(clientSocket)            End While        End Sub        Public Sub ProcessClientRequest(ByVal m_clientSocket As Socket)            Dim clientIP As IPEndPoint = TryCast(m_clientSocket.RemoteEndPoint, IPEndPoint)            Dim clientEndPoint As EndPoint = m_clientSocket.RemoteEndPoint            Try                Const c_microsecondsPerSecond As Int32 = 1000000                Dim buffer() As Byte = New Byte((1024) - 1) {}                If m_clientSocket.Poll((20 * c_microsecondsPerSecond), SelectMode.SelectRead) Then                    If (m_clientSocket.Available = 0) Then                        'Return                    End If                    Dim bytesRead As Int32 = m_clientSocket.Receive(buffer, m_clientSocket.Available, SocketFlags.None)                    Dim request As New String(Encoding.UTF8.GetChars(buffer))                    Dim Msg As String = ""                    If (request.IndexOf("favicon.ico") > -1) Then                        'Debug.Print("favicon request terminated")                        Msg = "Favion request terminated"                        'do nothig                    Else                        'Debug.Print(request)                        Dim DoCmd As String                        Dim stringArray() As String = request.Split(CChar("?"))                        DoCmd = stringArray(1)                        'Debug.Print(DoCmd)                        Doorstate = GDsensor.Read()                        Select Case DoCmd                            Case "DoNothing"                                Msg = "But I want to do something!"                                GetResponse(clientIP, m_clientSocket, Msg)                            Case "OpenDoors"                                If Doorstate = False Then                                    Msg = "Opening The Garage Door"                                    GetResponse(clientIP, m_clientSocket, Msg)                                    Thread.Sleep(6000)                                    'Open the door                                    DoorOpener.Write(False)                                    Thread.Sleep(1000)                                    DoorOpener.Write(True)                                    'Debug.Print("Successfully Opened The Garage Door")                                ElseIf Doorstate = True Then                                    Msg = "The Garage Door Is Already Open - command aborted"                                    GetResponse(clientIP, m_clientSocket, Msg)                                Else                                    Msg = "Error Executing Command"                                    GetResponse(clientIP, m_clientSocket, Msg)                                End If                            Case "CloseDoors"                                If Doorstate = True Then                                    Msg = "Closing The Garage Door"                                    Call GetResponse(clientIP, m_clientSocket, Msg)                                    Thread.Sleep(6000)                                    'Close the door                                    DoorOpener.Write(False)                                    Thread.Sleep(1000)                                    DoorOpener.Write(True)                                    'Debug.Print("Successfully Closed The Garage Door")                                ElseIf Doorstate = False Then                                    Msg = "The Garage Door Is Already Closed - command aborted"                                    GetResponse(clientIP, m_clientSocket, Msg)                                Else                                    Msg = "Error Executing Command"                                    GetResponse(clientIP, m_clientSocket, Msg)                                End If                            Case "DoorsStatus"                                If Doorstate = True Then                                    Msg = "1"                                ElseIf Doorstate = False Then                                    Msg = "2"                                Else                                    Msg = "Unkown Error checking status"                                End If                                GetResponse(clientIP, m_clientSocket, Msg)                            Case Else                                Msg = "What'cha talkin' about Willis?!"                                GetResponse(clientIP, m_clientSocket, Msg)                        End Select                    End If                End If            Catch ex As Exception                Debug.Print(ex.ToString)            End Try        End Sub


#2 marksmanaz

marksmanaz

    Member

  • Members
  • PipPip
  • 15 posts

Posted 12 September 2013 - 04:01 AM

OK I finally found my problem. I got rid of the "Implements IDisposable" and the region and sub that went with it that I did not include in the code clip above. Then I added a close command and made the socket = nothing in the response sub that also did not include above. I now can send a post or get a thousand times without it shutting down :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.