Fredd
02-02-2002, 12:21 PM
Hello everyone there,
I got couple of questions to ask regarding winsock connection.
1. I have come across the use of control array for multiple client connection to the server, so need you guys to enlighten me in this area.
Can I know how does actually the following codes works in allowing for multiple connection from Client?
What I understand - if there is already an existing connection made by one client, the code will create another new array of sockets, but why is it necessary to put if index is 0?
And for the last line in the procedure, I notice that each time I Close a connection from the client and connect again from the same client, the Client Port increase by 1, for example: before close 1065 but after close and connect, I got 1066, now why does the port number increase by 1 and what it means actually?
Notes: Some of this codes i picked up from internet.
Option explicit
Dim iSockets as Integer
Private Sub WinsockServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
iSockets = iSockets + 1
Load WinsockServer(iSockets)
WinsockServer(iSockets).LocalPort = 1001
WinsockServer(iSockets).Accept requestID
End If
End Sub
What about the following codes for to take care of the closing?
Private Sub WinsockServer_Close(Index As Integer)
WinsockServer(Index).Close
Unload WinsockServer(Index)
iSockets = iSockets - 1
End Sub
2.When I only have one Client and one host, how can I close the connection and reconnect again when needed. I have the following code with me but got one problem, after closing the connection it just refuse to reconnect again? Do I really need to use control array to do this? Is there any other way?
Following is server program:
Private Sub Form_load()
WinsockServer.LocalPort = 1001
WinsockServer.Listen
End Sub
Private Sub Form_Unload(Cancel As Integer)
WinsockServer.Close
End Sub
Private Sub WinsockServer_ConnectionRequest(ByVal requestID As Long)
If WinsockServer.State <> sckClosed Then
WinsockServer.Close
End If
WinsockServer.Accept requestID
End Sub
And the following are Client program:
Private Sub Form_load()
tcpClient.RemoteHost = "127.0.0.1"
tcpClient.RemotePort = 1001
End Sub
Private Sub cmdConnect_Click()
Dim strnow As Long
If tcpClient.State <> sckClosed Then
tcpClient.Close
Do Until tcpClient.State = sckClosed
DoEvents
Loop
End If
tcpClient.Connect
strnow = Timer 'read current time b4 entering the loop
Do While (tcpClient.State <> sckConnected)
DoEvents
If (Timer - strnow) > 3 Then 'prevent endless loop
Exit Sub
End If
Loop
End Sub
3. Can anyone tell me how can I use object.localport = 0? I understand that this allow for random assign port number instead of using a specific one however I failed to connect my client to the server by using the following code:
For server, I have
WinsockServer.LocalPort = 0
And for client, I have
TcpClient.LocalPort = 0
I guess that is all for now, sorry for the long question ;)
I got couple of questions to ask regarding winsock connection.
1. I have come across the use of control array for multiple client connection to the server, so need you guys to enlighten me in this area.
Can I know how does actually the following codes works in allowing for multiple connection from Client?
What I understand - if there is already an existing connection made by one client, the code will create another new array of sockets, but why is it necessary to put if index is 0?
And for the last line in the procedure, I notice that each time I Close a connection from the client and connect again from the same client, the Client Port increase by 1, for example: before close 1065 but after close and connect, I got 1066, now why does the port number increase by 1 and what it means actually?
Notes: Some of this codes i picked up from internet.
Option explicit
Dim iSockets as Integer
Private Sub WinsockServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
iSockets = iSockets + 1
Load WinsockServer(iSockets)
WinsockServer(iSockets).LocalPort = 1001
WinsockServer(iSockets).Accept requestID
End If
End Sub
What about the following codes for to take care of the closing?
Private Sub WinsockServer_Close(Index As Integer)
WinsockServer(Index).Close
Unload WinsockServer(Index)
iSockets = iSockets - 1
End Sub
2.When I only have one Client and one host, how can I close the connection and reconnect again when needed. I have the following code with me but got one problem, after closing the connection it just refuse to reconnect again? Do I really need to use control array to do this? Is there any other way?
Following is server program:
Private Sub Form_load()
WinsockServer.LocalPort = 1001
WinsockServer.Listen
End Sub
Private Sub Form_Unload(Cancel As Integer)
WinsockServer.Close
End Sub
Private Sub WinsockServer_ConnectionRequest(ByVal requestID As Long)
If WinsockServer.State <> sckClosed Then
WinsockServer.Close
End If
WinsockServer.Accept requestID
End Sub
And the following are Client program:
Private Sub Form_load()
tcpClient.RemoteHost = "127.0.0.1"
tcpClient.RemotePort = 1001
End Sub
Private Sub cmdConnect_Click()
Dim strnow As Long
If tcpClient.State <> sckClosed Then
tcpClient.Close
Do Until tcpClient.State = sckClosed
DoEvents
Loop
End If
tcpClient.Connect
strnow = Timer 'read current time b4 entering the loop
Do While (tcpClient.State <> sckConnected)
DoEvents
If (Timer - strnow) > 3 Then 'prevent endless loop
Exit Sub
End If
Loop
End Sub
3. Can anyone tell me how can I use object.localport = 0? I understand that this allow for random assign port number instead of using a specific one however I failed to connect my client to the server by using the following code:
For server, I have
WinsockServer.LocalPort = 0
And for client, I have
TcpClient.LocalPort = 0
I guess that is all for now, sorry for the long question ;)