关于UDP的连接问题
我想要做一个小游戏,需要使用多人连接。为了完成服务器的搜索这一功能,我使用了WINSOCK的UDP协议,远程地址设置为255.255.255.255以此来广播。但是多次运行都没办法接收到讯息,甚至我把255.255.255.255更改成绝对路径192.168.5.1依然无法完成连接。实在是琢磨了好几天了琢磨不出来,在此麻烦一下各位帮我看一下我的代码有什么问题。
代码写的惨不忍睹,凑合着看吧,在此谢过了。
这里放文件
军棋游戏 未完成.zip
(550.93 KB)
现在刚开始做,其他Form都是空架子,只有界面没有代码。那个多人游戏大厅Form3才是现在在做的。大家只要看一下那一段代码就好了。有一些被注释掉的代码,是我在网上搜索相关的代码测试用的,只清除了一部分,还有一小部分忘记清除掉了,大家忽略即可。
如果不想下载,我就把无效的一段代码发上来。
程序代码:
Dim 刷新倒计时 As Integer, WinSockMode As Boolean Const Client = False Const Server = True Dim ConnectedUser(0 To 4) As String Option Explicit Private Sub Command1_Click() Winsock1.Close Winsock1.RemotePort = 6000 Winsock1.LocalPort = 6001 Winsock2.LocalPort = 6003 Winsock2.RemotePort = 6002 '修改两种模式的端口 Winsock1.Bind '绑定端口防止被别的程序占据 Timer1.Enabled = False Label2.Caption = "已连接的人数-0" List1.AddItem "你的服务器" & Winsock1.LocalIP Winsock2.Close Winsock2.Protocol = sckUDPProtocol Winsock2.Bind WinSockMode = Server '设定模式服务端 Command1.Enabled = False End Sub Private Sub Form_Load() 'Dim lResult As Long ' Results of 1st option. ' Dim lResult2 As Long ' Results of 2nd option. List1.Clear '清空列表 List1.AddItem "正在尝试搜索服务器中…点击服务器信息可以连接目标服务器…" 刷新倒计时 = 10 '重置刷新倒计时计数器 Winsock1.Close Winsock2.Close Winsock1.RemoteHost = "255.255.255.255" Winsock1.Protocol = sckUDPProtocol Winsock2.Protocol = sckUDPProtocol Winsock1.RemotePort = 6001 Winsock1.LocalPort = 6000 Winsock1.Bind '绑定端口 Winsock2.LocalPort = 6002 Winsock2.RemotePort = 6003 ' Change two options valid for UDP sockets. ' lResult = setsockopt(Winsock1.SocketHandle, SOL_SOCKET, SO_REUSEADDR, 1, 4) ' If (lResult = SOCKET_ERROR) Then ' MsgBox "Error setting SO_REUSEADDR option: " & CStr(Err.LastDllError) ' End If 'lResult2 = setsockopt(Winsock1.SocketHandle, SOL_SOCKET, SO_BROADCAST, 0, 4) 'If (lResult2 = SOCKET_ERROR) Then ' MsgBox "Error setting SO_BROADCAST option: " & CStr(Err.LastDllError) 'End If '允许全频道广播-自己添加的代码 Winsock1.SendData "/GetServicerIP:" & Winsock1.LocalIP '全波段广播 WinSockMode = Client '设定模式客户端 End Sub Private Sub List1_Click() If WinSockMode = Server Then Exit Sub '判定模式防止出错 If List1.ListIndex = 0 Or Left(List1.List(List1.ListIndex), 5) = "你的服务器" Then Exit Sub '防止用户错误单击提示信息 Winsock2.RemoteHost = Mid((List1.List(List1.ListIndex)), 7) Winsock2.SendData "/Connect" & Winsock2.LocalIP '再次确认服务器状态,同时发送本机IP Debug.Print Mid((List1.List(List1.ListIndex)), 7) End Sub Private Sub Timer1_Timer() 刷新倒计时 = 刷新倒计时 - 1 If 刷新倒计时 = 0 Then Winsock1.SendData "/GetServicerIP:" & Winsock1.LocalIP 刷新倒计时 = 10 Else Label2.Caption = "刷新倒计时:" & 刷新倒计时 End If End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim StrData As String If WinSockMode = Server Then GoTo ServerMode '判定模式 Winsock1.GetData StrData If Left(StrData, 11) = "/ServicerIP" Then Debug.Print Mid(StrData, 13) List1.AddItem "服务器IP:" & Mid(StrData, 13) '处理接收信息,获得服务器IP地址 End If Exit Sub '-------------------------------------------------------以上为客户端代码,以下为服务端代码 ServerMode: Winsock1.GetData StrData If Left(StrData, 14) = "/GetServicerIP" Then Debug.Print Mid(StrData, 16) Winsock1.RemoteHost = Mid(StrData, 16) Winsock1.SendData "/ServicerIP:" & Winsock1.LocalIP '发送服务器IP地址 End If End Sub Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long) If WinSockMode = Server Then GoTo ServerMode '判定模式 Dim StrData As String Winsock1.GetData StrData If StrData = "/Connected" Then MsgBox "服务器连接成功,获得Connected信息,连接成功服务器的IP值为" & Winsock2.RemoteHost ElseIf StrData = "Already Full,Refuse to Enter" Then MsgBox "服务器连接失败,返回Refuse信息,错误原因:玩家已满" End If Exit Sub ServerMode: '服务器 Winsock1.GetData StrData If Left(StrData, 8) = "/Connect" Then Dim UserIp As String UserIp = Replace(StrData, "/Connect", "") '获得远程IP If ConnectedUser(4) <> "" Then '首先判定玩家人数是否已满 Winsock2.RemoteHost = UserIp Winsock2.SendData "Already Full,Refuse to Enter" Exit Sub End If Dim i As Integer For i = 1 To 4 If ConnectedUser(i) = "" Then ConnectedUser(i) = UserIp '写入远程IP Winsock2.SendData "Connected" '发送链接成功信息 Exit Sub '结束SUB End If Next i End If End Sub
[此贴子已经被作者于2017-1-8 12:07编辑过]