|
代码如下:
Private Sub Command1_Click()
If Command1.Caption = "启动服务" Then
'txtip.Text = Winsock1.LocalPort
List1.AddItem Time & " 正在启动服务"
On Error GoTo skip '注释:如此端口已有通信程序则退出
sckListen.LocalPort = txtport.Text
sckListen.Listen
Command1.Caption = "停止服务"
List1.AddItem Time & " 成功启动服务,正在运行……"
Label3.Caption = "服务器状态:运行"
Exit Sub
skip:
If Err.Number = 10048 Then
List1.AddItem Time & " 端口已经被占用了"
End If
Else
Command1.Caption = "启动服务"
List1.AddItem Time & " 正在停止服务"
sckListen.Close
List1.AddItem Time & " 成功停止服务"
Label3.Caption = "服务器状态:停止"
End If
End Sub
Private Sub Command2_Click()
'选择目录
Dim strResFolder As String
strResFolder = BrowseForFolder(hWnd, "请选择一个目录.")
If strResFolder = "" Then
' Call MsgBox("你取消了选择目录..", vbExclamation)
Else
' Call MsgBox("目录" & strResFolder & "被选择!", vbExclamation)
txtml.Text = strResFolder
End If
End Sub
Private Sub Command3_Click()
i = WriteIni("server", "mulu", txtml.Text, App.Path & "\server.ini")
j = WriteIni("server", "ip", txtip.Text, App.Path & "\server.ini")
k = WriteIni("server", "port", txtport.Text, App.Path & "\server.ini")
If i <> 0 And j <> 0 And k <> 0 Then
msg = MsgBox("保存成功!", vbOKOnly, "提示")
Unload Me
Else
msg = MsgBox("保存失败!请检查" & App.Path & "\server.ini是否有写权限?", vbInformation, "提示")
End If
Form1.Show
End Sub
Private Sub Form_Load()
txtip.Text = sckListen.LocalIP
txtport.Text = getIni(App.Path & "\server.ini", "server", "port")
txtml.Text = getIni(App.Path & "\server.ini", "server", "mulu")
End Sub
Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
List1.AddItem Time & " 请求号:" & requestID & "正在请求连接"
Dim i As Long
Dim sckServer() As Winsock
If CurNumber < MaxNumber Then 'MaxNumber为最大连接数,CurNumber为当前连接数
For i = 1 To CurNumber
If sckServer(i).State = 0 Then '判断是否有空闲Winsock控件
Exit For
End If
Next i
If i = CurNumber Then
CurNumber = CurNumber + 1
i = CurNumber
End If
Load sckServer(i) '动态加载Winsock控件
sckServer(i).Protocol = sckTCPProtocol
sckServer(i).Accept requestID
Exit Sub
End If
'sckBusy.Close
'sckBusy.Accept requestID
'If sckListen.State <> sckClosed Then
' sckListen.Close
'End If
'sckListen.Accept requestID
End Sub
Private Sub sckServer_DataArrival(ByVal bytesTotal As Long)
i = MsgBox(sckServer.GetData, vbOKCancel, "")
End Sub
|
|