Click to See Complete Forum and Search --> : Basic Method of Winsock Server
CAPHS
Apr 3rd, 2000, 08:24 PM
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.
compuGEEK
Apr 4th, 2000, 01:57 AM
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
CAPHS
Apr 4th, 2000, 04:04 AM
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
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:
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.