Results 1 to 4 of 4

Thread: Basic Method of Winsock Server

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    Hello,
    May someone please be able to show me some working code explaining how i can accept a connection, and create a new winsock control which listens on the same port, in a na array style (ie a true server app) any help is appreciated, thanks.
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  2. #2
    Hyperactive Member compuGEEK's Avatar
    Join Date
    May 1999
    Location
    Mpls,MN,USA
    Posts
    281

    From MSDN

    To create a TCP server

    Create a new Standard EXE project.


    Change the name of the default form to frmServer.


    Change the caption of the form to "TCP Server."


    Draw a Winsock control on the form and change its name to tcpServer.


    Add two TextBox controls to the form. Name the first txtSendData, and the second txtOutput.


    Add the code below to the form.

    Private Sub Form_Load()
    ' Set the LocalPort property to an integer.
    ' Then invoke the Listen method.
    tcpServer.LocalPort = 1001
    tcpServer.Listen
    frmClient.Show ' Show the client form.
    End Sub

    Private Sub tcpServer_ConnectionRequest _
    (ByVal requestID As Long)
    ' Check if the control's State is closed. If not,
    ' close the connection before accepting the new
    ' connection.
    If tcpServer.State <> sckClosed Then _
    tcpServer.Close
    ' Accept the request with the requestID
    ' parameter.
    tcpServer.Accept requestID
    End Sub

    Private Sub txtSendData_Change()
    ' The TextBox control named txtSendData
    ' contains the data to be sent. Whenever the user
    ' types into the textbox, the string is sent
    ' using the SendData method.
    tcpServer.SendData txtSendData.Text
    End Sub

    Private Sub tcpServer_DataArrival _
    (ByVal bytesTotal As Long)
    ' Declare a variable for the incoming data.
    ' Invoke the GetData method and set the Text
    ' property of a TextBox named txtOutput to
    ' the data.
    Dim strData As String
    tcpServer.GetData strData
    txtOutput.Text = strData
    End Sub


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    hmmm, no, that will just close any active connection on that port and accept the new one, i need to accept the connection, and listen on the same port again, while keeping all existing connections open by creating a new winsock control on the fly. I believe that it would also be better for me to use the winsock API, as this program will allow a lot of clients to connect
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  4. #4
    Guest
    Usa a control array of winsocks each time a connection comes load a new winsock and let that one accept the connection, when the connection closes discard the winsock.

    Example:
    Code:
    Private Sub Form_Load()
        Winsock1(0).LocalPort = 1666
        Winsock1(0).Listen
    End Sub
    
    Private Sub Winsock1_Close(Index As Integer)
        Unload Winsock1(Index)
    End Sub
    
    Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
        Unload Winsock1(Index)
    End Sub
    
    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        Dim iNewWinsock As Integer
        iNewWinsock = Winsock1.UBound + 1
        Load Winsock1(iNewWinsock)
        Winsock1(iNewWinsock).Close
        Winsock1(iNewWinsock).LocalPort = 0
        Winsock1(iNewWinsock).Accept requestID
        Debug.Print "Connection(" & iNewWinsock & ") accepted from: " & Winsock1(iNewWinsock).RemoteHostIP
    End Sub
    Hope it helps.


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