📄 sever.frm
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "mswinsck.ocx"
Begin VB.Form Form1
Caption = "Server"
ClientHeight = 5385
ClientLeft = 60
ClientTop = 345
ClientWidth = 11550
LinkTopic = "Form1"
ScaleHeight = 5385
ScaleWidth = 11550
StartUpPosition = 3 '窗口缺省
Begin MSWinsockLib.Winsock Winsock1
Index = 0
Left = 4320
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin VB.CommandButton cmdexit
Caption = "exit"
Height = 375
Left = 8760
TabIndex = 11
Top = 4440
Width = 855
End
Begin VB.CommandButton cmddatasend
Caption = "send"
Height = 375
Left = 7080
TabIndex = 10
Top = 4440
Width = 855
End
Begin VB.TextBox txtdata_send
Height = 495
Left = 240
TabIndex = 9
Top = 4440
Width = 5895
End
Begin VB.TextBox txtlocal_port
Height = 495
Left = 6720
TabIndex = 8
Top = 3600
Width = 2295
End
Begin VB.TextBox txtlocal_ip
Height = 495
Left = 6720
TabIndex = 6
Top = 2640
Width = 2295
End
Begin VB.TextBox txtremote_port
Height = 495
Left = 6720
TabIndex = 4
Top = 1680
Width = 2295
End
Begin VB.TextBox txtremote_ip
Height = 495
Left = 6720
TabIndex = 2
Top = 600
Width = 2295
End
Begin MSWinsockLib.Winsock Winsock1
Index = 1
Left = 4800
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 2
Left = 5280
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 3
Left = 5760
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 4
Left = 6240
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 5
Left = 6720
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 6
Left = 7200
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 7
Left = 7680
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 8
Left = 8160
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 9
Left = 8640
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin MSWinsockLib.Winsock Winsock1
Index = 10
Left = 9120
Top = 4920
_ExtentX = 741
_ExtentY = 741
_Version = 393216
Protocol = 1
End
Begin VB.Label lblusers
BackColor = &H00000000&
BeginProperty Font
Name = "宋体"
Size = 7.5
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFFFF&
Height = 3255
Left = 9240
TabIndex = 13
Top = 840
Width = 2055
End
Begin VB.Label Label6
Caption = "users table:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 9240
TabIndex = 12
Top = 240
Width = 2175
End
Begin VB.Label Label5
Caption = "Local_port:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 6480
TabIndex = 7
Top = 3240
Width = 1935
End
Begin VB.Label Label4
Caption = "Local_ip:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 6480
TabIndex = 5
Top = 2280
Width = 1695
End
Begin VB.Label Label3
Caption = "Remote_port:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 6480
TabIndex = 3
Top = 1200
Width = 2175
End
Begin VB.Label Label2
Caption = "Remote_ip:"
BeginProperty Font
Name = "宋体"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 6480
TabIndex = 1
Top = 120
Width = 1695
End
Begin VB.Label Label1
BackColor = &H00000000&
ForeColor = &H00FFFFFF&
Height = 3735
Left = 120
TabIndex = 0
Top = 240
Width = 6015
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim data As String '要发送的数据或接收到的数据
Dim old_string As String '原来接受到的数据
Dim remote_ip As String '发送方ip
Dim remote_port As String '发送方port
Dim real_data As String '接受到真正数据
Dim running(10) As Boolean '对应于10个winsock控件是否有用户连接
Dim ip(10) As String '对应于有用户连接的winsock控件用户ip
Dim port(10) As String '对应于有用户连接的winsock控件用户ip
Private Sub cmddatasend_Click()
Dim i As Integer
If txtremote_ip.Text = "" Or txtremote_port.Text = "" Then '如果不填ip或port,就以广播的形式发送信息
For i = 1 To 10
If running(i) = True Then
Winsock1(i).SendData txtlocal_ip.Text & "@" & txtlocal_port.Text & "@" & txtdata_send.Text
old_string = old_string & txtlocal_ip.Text & "发出信息:" & txtdata_send.Text & Chr(13)
Label1.Caption = old_string
End If
Next i
Else
For i = 1 To 10 '服务器施行点对点发送
If ip(i) = txtremote_ip.Text And port(i) = txtremote_port.Text Then
Winsock1(i).SendData txtlocal_ip.Text & "@" & txtlocal_port.Text & "@" & txtdata_send.Text
old_string = old_string & txtlocal_ip.Text & "发出信息:" & txtdata_send.Text & Chr(13)
Label1.Caption = old_string
Exit For
End If
Next i
End If
End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Integer
data = ""
For i = 1 To 10
running(i) = False
ip(i) = ""
port(i) = ""
Next i
ip(0) = txtlocal_ip.Text
port(0) = txtlocal_port.Text
running(0) = True
txtremote_ip.Text = "127.0.0.1" '初始化ip和port
txtremote_port.Text = "1001"
txtlocal_ip.Text = "127.0.0.1"
txtlocal_port.Text = "1000"
If App.PrevInstance = True Then '初始化winsock控件
End
End If
With Winsock1(0)
'.RemoteHost = txtremote_ip.Text
'.RemotePort = Val(txtremote_port.Text)
.Bind Val(txtlocal_port.Text), txtlocal_ip.Text
End With
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim ii As Integer '大循环的循环变量
Dim jj As Integer '分配一个winsock控件
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim temp As String
Dim send_port As Integer '传给客户端,告诉其服务端给分配的端口
For ii = 0 To 10
data = ""
remote_ip = ""
remote_port = ""
real_data = ""
If running(ii) = True Then
Winsock1(ii).GetData data
For i = 1 To Len(data) '求出发送方ip
temp = Mid(data, i, 1)
If temp <> "@" Then
remote_ip = remote_ip & temp
Else
Exit For
End If
Next i
For j = i + 1 To Len(data) '求出发送方port
temp = Mid(data, j, 1)
If temp <> "@" Then
remote_port = remote_port & temp
Else
Exit For
End If
Next j
For k = j + 1 To Len(data) '求出发送方发来的真正数据
temp = Mid(data, k, 1)
real_data = real_data & temp
Next k
If ii = 0 And real_data = "request connect!" And remote_ip = txtlocal_ip.Text Then '请求一个连接
For jj = 1 To 10
If running(jj) = False Then
running(jj) = True
ip(jj) = remote_ip
port(jj) = remote_port
With Winsock1(jj)
.RemoteHost = remote_ip
.RemotePort = Val(remote_port)
.Bind Val(txtlocal_port.Text) + 100 + jj, txtlocal_ip.Text
End With
lblusers.Caption = lblusers.Caption & remote_ip & "来到了聊天室!" & Chr(13) '用户表
old_string = old_string & remote_ip & "has connected!" & Chr(13) '显示发送的内容
send_port = Val(txtlocal_port.Text) + 100 + jj
Winsock1(jj).SendData txtlocal_ip.Text & "@" & send_port & "@" & "OK!"
Label1.Caption = old_string
Exit For
End If
Next jj
'data = ""
'remote_ip = ""
'remote_port = ""
'real_data = ""
Exit For
ElseIf ii = 0 And real_data = "request disconnect!" And remote_ip = txtlocal_ip.Text Then '结束一个连接
For jj = 1 To 10
If ip(jj) = remote_ip And port(jj) = remote_port Then
running(jj) = False
lblusers.Caption = lblusers.Caption & remote_ip & "离开了聊天室!" & Chr(13)
old_string = old_string & remote_ip & "has disconnected!" & Chr(13)
Label1.Caption = old_string
Winsock1(jj).Close
Exit For
End If
Next jj
'data = ""
'remote_ip = ""
'remote_port = ""
'real_data = ""
Exit For
ElseIf data <> "" Then '消息转发
'For jj = 1 To 10
If ip(ii) = remote_ip And port(ii) = remote_port Then
Winsock1(ii).SendData remote_ip & "@" & remote_port & "@" & real_data
old_string = old_string & remote_ip & "发出信息:" & real_data & Chr(13)
Label1.Caption = old_string
Exit For
End If
'Next jj
'data = ""
'remote_ip = ""
'remote_port = ""
'real_data = ""
Exit For
End If
End If
Next ii
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -