-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmServer.frm
357 lines (348 loc) · 14.3 KB
/
frmServer.frm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmServer
Caption = "Sample-Application(Server)"
ClientHeight = 3195
ClientLeft = 2115
ClientTop = 1545
ClientWidth = 5520
Icon = "frmServer.frx":0000
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 5520
StartUpPosition = 3 'Windows-Standard
Begin VB.CommandButton cmdClose
Caption = "Close(Kick Client)"
Enabled = 0 'False
Height = 435
Left = 120
TabIndex = 10
Top = 2220
Width = 1215
End
Begin VB.ListBox lst
Height = 2790
Left = 3120
TabIndex = 4
Top = 60
Width = 2295
End
Begin VB.CommandButton cmdSend
Caption = "&Send Data"
Default = -1 'True
Enabled = 0 'False
Height = 435
Left = 1680
TabIndex = 3
Top = 780
Width = 1215
End
Begin VB.TextBox txtIndex
Height = 285
Left = 1080
TabIndex = 2
Text = "Index(not 0)"
Top = 360
Width = 975
End
Begin VB.TextBox txtData
Height = 285
Left = 1080
TabIndex = 1
Text = "Data"
Top = 60
Width = 1515
End
Begin VB.CommandButton cmdListen
Caption = "Start &Listening"
Height = 435
Left = 120
TabIndex = 0
Top = 780
Width = 1215
End
Begin MSWinsockLib.Winsock sck
Index = 0
Left = 2700
Top = 300
_ExtentX = 741
_ExtentY = 741
_Version = 393216
End
Begin VB.Label lbl
BackStyle = 0 'Transparent
Caption = "Index-TextBox"
Height = 255
Index = 1
Left = 0
TabIndex = 9
Top = 360
Width = 1035
End
Begin VB.Label lbl
BackStyle = 0 'Transparent
Caption = "Data-TextBox"
Height = 255
Index = 0
Left = 0
TabIndex = 8
Top = 60
Width = 1035
End
Begin VB.Label lbl
BackStyle = 0 'Transparent
Caption = "With this Button the text in the Data-TextBox is sent to the winsock with the index in the Index-TextBox"
Height = 1455
Index = 3
Left = 1680
TabIndex = 7
Top = 1320
Width = 1275
End
Begin VB.Label lbl
BackStyle = 0 'Transparent
Caption = "Press this button to make this program let clients connect"
Height = 855
Index = 2
Left = 180
TabIndex = 6
Top = 1320
Width = 1155
End
Begin VB.Label lblStatus
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "Status"
BeginProperty DataFormat
Type = 2
Format = ""
HaveTrueFalseNull= 0
FirstDayOfWeek = 0
FirstWeekOfYear = 0
LCID = 1031
SubFormatType = 9
EndProperty
Height = 195
Left = 120
TabIndex = 5
Top = 2940
Width = 450
End
End
Attribute VB_Name = "frmServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'**********'
' ThFabba's Winsock Sample Application V1.15 (for SP5 of the control) - Server
' Copyright © 2001-2002 Thomas Faber
'
' last edit: 17th Mar 2002
'
' this program shows how to make a server that allows _
multiple(i think up to 32766) connections
' the information is written into a listbox
' start/stop listening, sending/receiving data(text) _
and kicking clients are supported
'
' okay... i made this sample some time ago and just made the code _
a bit clearer in this version.
' Now I don't use the Winsock Control myself in my programs...
' I use Winsock api and if you want to make big server applications _
this will be really necessary cause i heard Winsock control is _
not very stable. If you want to learn using the winsock api _
i suggest the very good tutorial by Oleg Gdalevich at http://www.vbip.com
'
' now something about my coding style...
' Option Explicit:
' I always use this cause it saves memory, is faster and _
helps to avoid errors due to mis-spelling
' Comments
' At the end of nearly every line in this program it put comments _
to make this code easier to understand
' If there is a comment on a line without code, it _
describes the following lines or the follwing function
' Indenting
' to make my code more readable i put tab characters _
before lines in blocks(like if/do/open and in this _
sample also function/sub)
' Variables
' I always try to give varibles names that are easy to _
understand and show what they are for. I also use the _
hungarian notation mostly to indicate the type of a variable
' prefixes i use for vb data types (including controls) in this code:
' int - Integer
' str - String
' bol - Boolean
' txt - TextBox
' lst - ListBox
' sck - Winsock
' cmd - CommandButton
' lbl - Label
' ByVal/ByRef
' I alwyas write which of them to use although ByRef is standard.
' If i don't need to change the value of an argument i use ByVal _
(except for strings, there i always use byref)
'
' you may do everything(change, copy, ...) with this code but: _
- you are not allowed to make anyone pay for this code _
- if u copy the code you have to credit me as the original creator _
- you are not allowed to remove/change this information
'
' I hope you can improve your winsock-programming skills _
with the help of this code :)
'
' please give me comments + suggestions!
'
' special thanks to:
' hmm! looks like i did this all on my own :)
' send me improvements or new features u added _
so thatll change :)
'
' sorry for all grammatical and orthographical mistakes _
and for my terrible non-apostrophe and non-capital-letter english :)
'
'
' Thomas Faber
' Email me: Th-Faber@gmx.net
'
'**********'
'*** Begin of code ***
Option Explicit
Option Base 0
Option Compare Text
Private bolWinsockIsUsed() As Boolean 'is that winsock used?
'this function returns a free index for a new client and prepares a winsock
Public Function GetFreeWinSock() As Integer
Dim intIndex As Integer 'counter variable. will store the index of the new winsock control
For intIndex = 1 To UBound(bolWinsockIsUsed) 'loop thru the array
If bolWinsockIsUsed(intIndex) = False Then 'is that index used?
'this index is not used so we can use it now
GoTo Prepare 'prepare the winsock
End If
Next intIndex
intIndex = UBound(bolWinsockIsUsed) + 1 'if all current indexes are used - add a new one
ReDim Preserve bolWinsockIsUsed(intIndex) 'redim the array for the new index
Prepare: 'prepare the winsock
Load sck(intIndex) 'add a new winsock control to the form
sck(intIndex).Close 'close the winsock to prepare it for a Connection
sck(intIndex).LocalPort = 0 'give that winsock a random free local port
bolWinsockIsUsed(intIndex) = True 'that index is used now
GetFreeWinSock = intIndex 'return the index
End Function
'This function closes the winsock with index I
Public Sub CloseWinsock(ByVal intIndex As Integer)
sck(intIndex).Close 'close the socket
bolWinsockIsUsed(intIndex) = False 'the winsock is not used now
Unload sck(intIndex) 'we wont need that winsock for now
lst.AddItem "Client(" & CStr(intIndex) & ") disconnected." 'add to the listbox
End Sub
'the close button was clicked. "kick" the selected client
Private Sub cmdClose_Click()
On Local Error GoTo TheError 'the app shouldn't stop if u typed something wrong into one of the text-boxes
CloseWinsock CInt(txtIndex.Text) 'close selected winsock
Exit Sub 'thats it. dont execute the code of the error handler since there was no error
TheError: 'error handler
MsgBox Err.Description, vbMsgBoxHelpButton Or _
vbExclamation, "Error " & Err.Number, Err.HelpFile, Err.HelpContext 'show the error
End Sub
'listen button was clicked
Private Sub cmdListen_Click()
Dim intIndex As Integer 'counter variable
If cmdListen.Caption = "Start &Listening" Then 'we have to start listening
sck(0).Close 'close the first winsock for a Connection
sck(0).LocalPort = "1234" 'any number but multiple servers cannot use the same port
'SAME PORT FOR SERVER AND CLIENTS!
sck(0).Listen 'the first winsock (0) listens and gives all clients to the other winsocks
lblStatus.Caption = "Listening" 'show that we are listening now
cmdListen.Caption = "Stop &Listening" 'change the caption of the button
ElseIf cmdListen.Caption = "Stop &Listening" Then 'we have to stop listening
For intIndex = 0 To UBound(bolWinsockIsUsed) 'loop thru all winsocks and close them
If bolWinsockIsUsed(intIndex) = True Then
sck(intIndex).Close 'close the winsock(stop the connection)
bolWinsockIsUsed(intIndex) = False 'the winsock isn't used now
End If
Next intIndex
cmdSend.Enabled = False 'disable send button since there cant be a client to send to
cmdClose.Enabled = False 'disable close button since there cant be a client we could "kick"
lblStatus.Caption = "Server closed" 'show that the server is closed now
cmdListen.Caption = "Start &Listening" 'change the caption of the button
End If
End Sub
'send button was pressed
Private Sub cmdSend_Click()
On Local Error GoTo TheError 'the app shouldn't stop if u typed something wrong into one of the text-boxes
sck(txtIndex.Text).SendData txtData.Text 'send the data to the winsock with the index in the textbox
txtData.Text = vbNullString 'clear textbox now
Exit Sub 'thats it. dont execute the code of the error handler since there was no error
TheError: 'error handler
MsgBox Err.Description, vbMsgBoxHelpButton Or _
vbExclamation, "Error " & Err.Number, Err.HelpFile, Err.HelpContext 'display error
End Sub
'form loads. initialize all the things we need
Private Sub Form_Load()
ReDim bolWinsockIsUsed(0) 'initialize the array
bolWinsockIsUsed(0) = False 'first winsock (index 0) listens but you cant send data thru it
End Sub
'an error in the winsock occured
Private Sub sck_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)
MsgBox Description, vbMsgBoxHelpButton Or _
vbExclamation, "Error " & Number, HelpFile, HelpContext 'display error
End Sub
'the text of the index textbox changed. lets check if its valid
Private Sub txtIndex_Change()
Dim intIndex As Integer 'index that was entered
'checks if the current text of this textbox is a valid index
On Local Error GoTo AnErrorOcured 'input is invalid
intIndex = CInt(txtIndex.Text) 'save index written to the textbox in the variable
If bolWinsockIsUsed(intIndex) = True Then 'input is valid. so enable the buttons
cmdSend.Enabled = True 'enable send button
cmdClose.Enabled = True 'enable close button
Exit Sub 'thats it. dont execute the code of the error handler since there was no error
End If
AnErrorOcured: 'error handler
'input is invalid. so disble the buttons
cmdSend.Enabled = False 'disable send button
cmdClose.Enabled = False 'disable close button
End Sub
'a key was pressed in the port textbox
Private Sub txtIndex_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then Exit Sub 'backspace - we will allow to use this :)
If IsNumeric(Chr$(KeyAscii)) = False Then _
KeyAscii = 0 'don't accept anything else than numbers
End Sub
'u clicked out of the textbox so tha we will remove leading zeros now...
Private Sub txtIndex_LostFocus()
On Local Error GoTo 0 'dont worry about errors
txtIndex.Text = CInt(txtIndex.Text) 'convert index to integer value. ie remove leading 0s
End Sub
'a user disconnects
Private Sub sck_Close(Index As Integer)
CloseWinsock Index 'close the winsock correctly and set all variables right
End Sub
'some1 wants to connect
Private Sub sck_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim intNewIndex As Integer 'index for new client
lblStatus = "Connection requested(" & CStr(requestID) & ")" 'show that some1 wants to connect
intNewIndex = GetFreeWinSock 'get index for new client
sck(intNewIndex).Accept requestID 'let the new winsock accept the connection
'
' Here you could check if this user may connect. Maybe ill add this in a _
future version. Or you could do that and send me ;)
'
lblStatus = "Connection from " & sck(intNewIndex).RemoteHostIP & "(" & CStr(requestID) & ") accepted" 'show that the connection was accepted
lst.AddItem "Client(" & CStr(intNewIndex) & ") connected." 'add to the listbox
End Sub
'we got data
Private Sub sck_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strGotData As String 'variable to receive the data
sck(Index).GetData strGotData 'put the data into the string
lst.AddItem "Client(" & CStr(Index) & "):" & strGotData 'add line of data to the listbox
'
' This is not a good way doing this! but this is not a sample on how to _
parse data. Maybe itll be in later versions ;)
'
lst.ListIndex = lst.ListCount - 1 'select new line in the listbox
End Sub
'*** End of code ***