Results 1 to 2 of 2

Thread: Winsock cant keep connection alive

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    86

    Winsock cant keep connection alive

    I am trying to make a basic server/client program but I ran in a problem.

    Code:
    Public Class Main
    
        Private Sub startServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startServer.Click
            If Not portText.Text = "" Then
                sock.LocalPort = portText.Text
                sock.Listen()
                If sock.CtlState = MSWinsockLib.StateConstants.sckListening Then
                    writeLog("listening...")
                End If
            End If
        End Sub
    
        Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            portText.Text = 28920
        End Sub
    
        Private Sub sock_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles sock.ConnectionRequest
            writeLog("accepting new request id" + e.requestID)
            If sock.CtlState <> MSWinsockLib.StateConstants.sckConnected Then
                sock.Close()
            End If
            sock.Accept(e.requestID)
        End Sub
    
        Private Sub sock_Error(ByVal sender As System.Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles sock.Error
            writeLog(e.description)
        End Sub
    
        Private Sub stopServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopServer.Click
            sock.Close()
            writeLog("Stop listening ...")
        End Sub
    
    End Class
    This is the server part. writeLog writes some lines of whats going on to a textfield.

    the client
    Code:
    Public Class Main
    
        Dim connected As Boolean
    
        Private Sub connectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectButton.Click
            If Not ipText.Text = "" Then
                Try
                    sock.RemoteHost = ipText.Text
                    sock.RemotePort = CInt(portText.Text)
                    sock.Connect()
                    connectButton.Enabled = False
                Catch ex As Exception
                    writeLog(ex)
                End Try
            End If
        End Sub
    
        Private Sub sendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendButton.Click
            writeLog(sendText.Text)
            sock.SendData(sendText.Text)
            sendText.Text = ""
        End Sub
    
        Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
            sock.Close()
            connectButton.Enabled = True
            statusLabel.Text = "disconnected"
        End Sub
    
        Private Sub sock_ConnectEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles sock.ConnectEvent
            writeLog("connected")
        End Sub
    
        Private Sub sock_Error(ByVal sender As System.Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles sock.Error
            writeLog(e.description)
        End Sub
    
        Private Sub sock_DataArrival(ByVal sender As System.Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles sock.DataArrival
            Dim dat As String = Nothing
            sock.GetData(dat)
            If Not dat = Nothing Then
                writeLog(dat)
            End If
        End Sub
    
        Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ipText.Text = "192.168.1.100"
            portText.Text = "28920"
        End Sub
    
    End Class
    When I click connect in the GUI the first state of sock.ctlstate is 6 , connecting then the connected event is triggered and after that the sock.ctlstate is 8 , "Peer is closing the connection". But I dont understand why it does this. I should keep the connection alive to communicate.

    anyone sees my flaw of logic in the code? Please enlighten me!

    Thanks in advance

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    86

    Re: Winsock cant keep connection alive

    Still having this problem, tried different computers and networks.

    any suggestions ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width