-
Nov 1st, 2000, 05:58 PM
#1
Thread Starter
Lively Member
I was wondering if anyone would have some code for a client and server that they have made using the winsock control that i could take a look at.....i mainly need to know how I can use winsock for a server to accept multiple client connection at once....it would be greatly appreciated if someone would help me out.
thanx
-KnIgHt
-ICQ 17743984
-
Nov 1st, 2000, 06:59 PM
#2
Addicted Member
Hi,
here is how I did mine:
1) Put a winsock control on a form with index set to 0
2) On connection request event it will give you the index of the winsock that wants to connect to your winsocks port.
3) load winsock(index)
there you go, your server has 1 more connection on it's winsock control at position : Index
Hope you have all the informations you need on how to use the winsock control because it's not funny at all !
Hope I helped a bit ( could help you more but I'am tired for today, maybe tomorrow...)
-
Nov 1st, 2000, 07:32 PM
#3
Thread Starter
Lively Member
ok...i'll try that but if anyone else could possibly email me a copy of a client and server that they've made please do that..
my email is b_fett@hotmail.com
-l8tz
-
Nov 1st, 2000, 07:50 PM
#4
Thread Starter
Lively Member
ok...this is the code i have so far
Option Explicit
Dim Index As Integer
Dim Buffer As String
Private Sub Form_Load()
Index = 0
Sock(Index).Listen
End Sub
Private Sub Sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Sock(Index).Accept requestID
Sock(Index).SendData Str(Index)
Index = Index + 1
Load Sock(Index)
End Sub
Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Sock (Buffer)
If Buffer = Right("/Name:") Then
lstNames.AddItem Right(Buffer, 7)
End If
End Sub
the part at the sock_dataarrival i'm having trouble with
how do you do a sock(index).getdata (buffer) if you don't know what number the (index) is supposed to be???
-
Nov 1st, 2000, 08:44 PM
#5
New Member
And here's the answer...
You need two winsock controls:
ListenSocket
CommSocket(0)
And here goes the code...
Private Sub Form_Load()
ListenSocket.LocalPort = 81
ListenSocket.Listen
CommSocket(0).LocalPort = 82
End Sub
Private Sub ListenSocket_ConnectionRequest(ByVal requestID as long)
CommSocket(CommSocket.UBound).Accept requestID
Load CommSocket(CommSocket.UBound + 1)
CommSocket(CommSocket.UBound).LocalPort = CommSocket(CommSocket.UBound - 1).LocalPort + 1
End Sub
-
Nov 1st, 2000, 08:58 PM
#6
Thread Starter
Lively Member
Re: And here's the answer...
ok now my code looks like this
Option Explicit
Dim Index As Integer
Dim Buffer As String
Private Sub Form_Load()
Index = 0
Sock(Index).Listen
End Sub
Private Sub Sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Sock.Accept requestID
Sock(Index).SendData Str(Index)
CommSocket(CommSocket.UBound).Accept requestID
Load sock2(sock2.UBound + 1)
Sock(sock2.UBound).LocalPort = sock2(sock2.UBound - 1).LocalPort + 1
End Sub
Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Sock.GetData (Buffer)
If Buffer = Right("/Name:") Then
lstNames.AddItem Right(Buffer, 7)
End If
End Sub
but what winsock control do i use to find out which client index is sending the data????
-
Nov 1st, 2000, 09:01 PM
#7
Thread Starter
Lively Member
sorry i made a mistake...now my code looks like this:
Option Explicit
Dim Index As Integer
Dim Buffer As String
Private Sub Form_Load()
Index = 0
Sock.Listen
End Sub
Private Sub Sock_ConnectionRequest(ByVal requestID As Long)
CommSocket(CommSocket.UBound).Accept requestID
Load sock2(sock2.UBound + 1)
Sock(sock2.UBound).LocalPort = sock2(sock2.UBound - 1).LocalPort + 1
End Sub
Private Sub Sock_DataArrival(ByVal bytesTotal As Long)
Sock.GetData (Buffer)
If Buffer = Right("/Name:") Then
lstNames.AddItem Right(Buffer, 7)
End If
End Sub
so what winsock is doing what???
-
Nov 1st, 2000, 11:39 PM
#8
Going in the wrong direction ....
KnlgHt,
You need to know the basic working of control arrays for using a single winsock control to accept more than one connection.
The MSDN has the necessary code for you, I shall outline what I did in my sample Client-Server program.
Put a winsock control on your form, which will act as the server. Name it tcpServer and it will have the index as 0.
Now remember one thing. You need to put this control i.e. tcpServer(0) in the Listening mode. tcpServer(0) will always stay in the listen mode. Whenever it receives a new connection request, it loads a new winsock into the array and passes on the request to this new winsock control. Remember, tcpServer(0) should never accept the connection request by itself. The connection request must be accepted by another control in the array.
Then you need to have a variable to track the index of the last winsock in the array. Every time you receive a new connection request on tcpServer(0), you increase this variable by 1 and that's the index of the new winsock.
-
Nov 2nd, 2000, 07:58 AM
#9
Addicted Member
... and we have a winner : Honneybee
You got it right, the server side winsock should be a collection and all clients should have a single winsock control wanting to connect to the server's port.
Just a little tip : Instead of using a varable with the last index for the server's connection, use an array with constant values.
For exemple :
Const WIN_NOT_LOADED = 0
Const WIN_LOADED = 1
Const WIN_CONNECTED = 2
const WIN_NOT_CONNECTED = 3
dim an array of values like : Dim lWinLoaded(10)
that you can REDIM later.
Set all you elements in the collection to WIN_NOT_LOADED
When you load a new instance (Accept a connection) of the control, scan your array for the next WIN_NOT_LOADED or WIN_NOT_CONNECTED element. Suppose the next available spot was 4 and that it had not been loaded yet then
' Do not load it again if it was WIN_NOT_CONNECTED
if lWinloaded(4) = WIN_NOT_LOADED then
Load Winsock(4)
lWinloaded(4) = WIN_LOADED
end if
Winsock(4).LocalPort = 0
Winsock(4).Accept requestID
lWinloaded(4) = WIN_CONNECTED
Now suppose your client connecte to element 4 of the Winsock sends you "Hello". This is what you should do in the DataArrival event:
Winsock(Index).Getdata ' Index is suplied by the event
Now in your Close event you should set your lWinloaded(Index) to WIN_NOT_CONNECTED and maybe redim your array...
Questions ?
Later...
-
Nov 2nd, 2000, 09:06 AM
#10
Frenzied Member
KnIgHt,
I have a small client/server demo that I created a while back to help answer questions such as yours. It also demonstrates how to pass UDTs via Winsock. Write me if you are interested: bsandlier@home.com
As for multiple connections, there is a section devoted to that in Using the Winsock Control
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|