Programación General > Redes y Comunicaciones
Indiana jhones en busca del paquete perdido
Begeo:
Saludos.
He intentado agregarle funcionalidad multijugador a mi programita, y estoy teniendo varios problemas en lo que respecta al que hace de servidor, esperar a que llegue un paquete, y cargar datos de un ini.
Básicamente, el servidor no está reciviendo ningún dato del cliente, pero tampoco esperando a que lo reciba (o eso me parece). He intentado hacer varios fixes pero no he podido al final. Igual ustedes me pueden ayudar a encontrar el error.
--- Código: Text ---Private Sub Connect_Click()If ActAs = "Serve" Then Exit Sub Winsock1.RemoteHost = txtIP.TextWinsock1.RemotePort = txtPORT.Text Winsock1.CloseWinsock1.Connect ActAs = "Client" txtIP.Text = "Found match!"End Sub Private Sub CServ_Click()If ActAs = "Client" Then Exit Sub Winsock1.Close ActAs = "Serve" Winsock1.LocalPort = txtPORT.Text Winsock1.Listen txtIP.Text = "Waiting for a rival . . ."End Sub Private Sub Dissc_Click()Winsock1.CloseEndEnd Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) Winsock1.Close ShallNotPass = True Winsock1.Accept requestID TTGameOnline.CHATwin.SelStart = Len(TTGameOnline.CHATwin.Text)TTGameOnline.CHATwin.Text = TTGameOnline.CHATwin.Text & TTGameOnline.GetVar(App.Path & "player.ini", "PLAYER", "Name") & " Is acting as SERVER!" & vbCrLfTTGameOnline.CHATwin.SelStart = Len(TTGameOnline.CHATwin.Text) Winsock1.SendData "1" & TTGameOnline.GetVar(App.Path & "player.ini", "PLAYER", "Name") & " Is acting as SERVER!" & vbCrLf 'Cartejas1 = GetVar(App.Path & "player.ini", "PLAYER", "Card1")'Cartejas2 = GetVar(App.Path & "player.ini", "PLAYER", "Card2")'Cartejas3 = GetVar(App.Path & "player.ini", "PLAYER", "Card3")'Cartejas4 = GetVar(App.Path & "player.ini", "PLAYER", "Card4")'Cartejas5 = GetVar(App.Path & "player.ini", "PLAYER", "Card5")Debug.Print Cartejas1Winsock1.SendData "_" txtIP.Text = "Found match!" End Sub Public Sub Winsock1_Error(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) Winsock1.Close MsgBox "Lost connection with game partner. Error:" & Number TTGameOnline.HideMultiplayForm.ShowEnd Sub Public Sub Winsock1_DataArrival(ByVal bytesTotal As Long)Dim Buffer As String Winsock1.GetData Buffer If Left(Buffer, 1) = "1" Then Buffer = Mid(Buffer, 2, Len(Buffer)) TTGameOnline.CHATwin.Text = TTGameOnline.CHATwin & Buffer & vbCrLfEnd IfIf Left(Buffer, 1) = "z" Then MsgBox "The game partner has left the session." MultiplayForm.Winsock1.Close Me.Show TTGameOnline.HideEnd IfCartejas1 = GetVar(App.Path & "player.ini", "PLAYER", "Card1")Cartejas2 = GetVar(App.Path & "player.ini", "PLAYER", "Card2")Cartejas3 = GetVar(App.Path & "player.ini", "PLAYER", "Card3")Cartejas4 = GetVar(App.Path & "player.ini", "PLAYER", "Card4")Cartejas5 = GetVar(App.Path & "player.ini", "PLAYER", "Card5")If Left(Buffer, 1) = "C" Then' If ShouldClientNotPass = True Then Exit Sub' If Left(Buffer, 2) = "Y" Then ShallNotPass = False' If ShallNotPass = True Then Exit Sub If ActAs = "Serve" Then' ShouldClientNotPass = True Winsock1.SendData "CN" & Cartejas1 & Cartejas2 & Cartejas3 & Cartejas4 & Cartejas5 End If 'For i = 1 To 5 ' EneDCards(i) = CInt(Left(Buffer, i + 1)) 'Next i' Dim i As Integer' Dim doomstring(1 To 5) As String' For i = 1 To 5' doomstring(i) = Left(Buffer, 2 + i)' Debug.Print doomstring(i); v' Next i EneCar = Left(Buffer, 3) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card1", EneCar) EneCar = Left(Buffer, 4) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card2", EneCar) EneCar = Left(Buffer, 5) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card3", EneCar) EneCar = Left(Buffer, 6) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card4", EneCar) EneCar = Left(Buffer, 7) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card5", EneCar) TTGameOnline.Show Me.Hide End IfIf Left(Buffer, 1) = "_" Then Winsock1.SendData "CY" & Cartejas1 & Catejas2 & Catejas3 & Catejas4 & Catejas5End IfEnd Sub
También les dejo el código completo por si quieren echarle un vistazo más de cerca:
*cought mejor no, privilegios cought*
Espero su respuesta.
m0skit0:
En Connect_Click() deberías de poner Winsock1.Close antes de asignar IP y puerto.
En Winsock1_ConnectionRequest (el evento de cuando se recibe una petición de conexión, que debería estar en el servidor) no deberías hacer Winsock1.Close porque sino cierras la conexión :P
Llamar dos veces seguidas (líneas 42 y 46) a TTGameOnline.GetVar(App.Path & "player.ini", "PLAYER", "Name") es ineficaz. Guarda el valor en una variable para volverlo a utilizar.
A ver si con las dos primeras se arregla algo, sino ya le echo un vistazo más en profundidad.
Begeo:
Gracias por el interés, moskit0, cuántas veces me habrás ayudado :lol:
Ahora me da un error tonto con esta línea:
--- Citar ---Winsock1.Accept requestID
--- Fin de la cita ---
40020 Invalid operation at current state
Pongo el código modificado ^_^
--- Código: Text ---Public ActAs As String Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpdefault As String, ByVal lpreturnedstring As String, ByVal nsize As Long, ByVal lpfilename As String) As LongPrivate Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyname As Any, ByVal lpString As String, ByVal lpfilename As String) As Long Public Cartejas1 As StringPublic Cartejas2 As StringPublic Cartejas3 As StringPublic Cartejas4 As StringPublic Cartejas5 As String Public EneCar As String Public ShouldNotPass As BooleanPublic ShouldClientNotPass As BooleanPublic Sub PutVar(File As String, Header As String, Var As String, Value As String) Call WritePrivateProfileString(Header, Var, Value, File)End Sub Private Sub cOFF_Click()TTGameOffline.ShowMe.HideEnd SubPrivate Function GetVar(File As String, Header As String, Var As String) As StringDim sSpaces As String ' Max string lengthDim szReturn As String ' Return default value if not found szReturn = vbNullString sSpaces = Space$(1000) Call GetPrivateProfileString(Header, Var, szReturn, sSpaces, Len(sSpaces), File) GetVar = RTrim$(sSpaces) GetVar = Left(GetVar, Len(GetVar) - 1)End Function Private Sub Connect_Click()If ActAs = "Serve" Then Exit Sub Winsock1.Close Winsock1.RemoteHost = txtIP.TextWinsock1.RemotePort = txtPORT.Text Winsock1.Connect ActAs = "Client" txtIP.Text = "Found match!"End Sub Private Sub CServ_Click()If ActAs = "Client" Then Exit Sub Winsock1.Close ActAs = "Serve" Winsock1.LocalPort = txtPORT.Text Winsock1.Listen txtIP.Text = "Waiting for a rival . . ."End Sub Private Sub Dissc_Click()Winsock1.CloseEndEnd Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) ShallNotPass = True Winsock1.Accept requestID Dim nombrejugador As String nombrejugador = TTGameOnline.GetVar(App.Path & "player.ini", "PLAYER", "Name") TTGameOnline.CHATwin.SelStart = Len(TTGameOnline.CHATwin.Text)TTGameOnline.CHATwin.Text = TTGameOnline.CHATwin.Text & nombrejugador & " Is acting as SERVER!" & vbCrLfTTGameOnline.CHATwin.SelStart = Len(TTGameOnline.CHATwin.Text) Winsock1.SendData "1" & nombrejugador & " Is acting as SERVER!" & vbCrLf 'Cartejas1 = GetVar(App.Path & "player.ini", "PLAYER", "Card1")'Cartejas2 = GetVar(App.Path & "player.ini", "PLAYER", "Card2")'Cartejas3 = GetVar(App.Path & "player.ini", "PLAYER", "Card3")'Cartejas4 = GetVar(App.Path & "player.ini", "PLAYER", "Card4")'Cartejas5 = GetVar(App.Path & "player.ini", "PLAYER", "Card5")Debug.Print Cartejas1Winsock1.SendData "_" txtIP.Text = "Found match!" End Sub Public Sub Winsock1_Error(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) Winsock1.Close MsgBox "Lost connection with game partner. Error:" & Number TTGameOnline.HideMultiplayForm.ShowEnd Sub Public Sub Winsock1_DataArrival(ByVal bytesTotal As Long)Dim Buffer As String Winsock1.GetData Buffer If Left(Buffer, 1) = "1" Then Buffer = Mid(Buffer, 2, Len(Buffer)) TTGameOnline.CHATwin.Text = TTGameOnline.CHATwin & Buffer & vbCrLfEnd IfIf Left(Buffer, 1) = "z" Then MsgBox "The game partner has left the session." MultiplayForm.Winsock1.Close Me.Show TTGameOnline.HideEnd IfCartejas1 = GetVar(App.Path & "player.ini", "PLAYER", "Card1")Cartejas2 = GetVar(App.Path & "player.ini", "PLAYER", "Card2")Cartejas3 = GetVar(App.Path & "player.ini", "PLAYER", "Card3")Cartejas4 = GetVar(App.Path & "player.ini", "PLAYER", "Card4")Cartejas5 = GetVar(App.Path & "player.ini", "PLAYER", "Card5")If Left(Buffer, 1) = "C" Then' If ShouldClientNotPass = True Then Exit Sub' If Left(Buffer, 2) = "Y" Then ShallNotPass = False' If ShallNotPass = True Then Exit Sub If ActAs = "Serve" Then' ShouldClientNotPass = True Winsock1.SendData "CN" & Cartejas1 & Cartejas2 & Cartejas3 & Cartejas4 & Cartejas5 End If 'For i = 1 To 5 ' EneDCards(i) = CInt(Left(Buffer, i + 1)) 'Next i' Dim i As Integer' Dim doomstring(1 To 5) As String' For i = 1 To 5' doomstring(i) = Left(Buffer, 2 + i)' Debug.Print doomstring(i); v' Next i EneCar = Left(Buffer, 3) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card1", EneCar) EneCar = Left(Buffer, 4) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card2", EneCar) EneCar = Left(Buffer, 5) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card3", EneCar) EneCar = Left(Buffer, 6) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card4", EneCar) EneCar = Left(Buffer, 7) Call PutVar(App.Path & "player.ini", "PLAYER2", "Card5", EneCar) TTGameOnline.Show Me.Hide End IfIf Left(Buffer, 1) = "_" Then Winsock1.SendData "CY" & Cartejas1 & Catejas2 & Catejas3 & Catejas4 & Catejas5End IfEnd Sub
m0skit0:
Pues efectivamente, tenías esa parte bien. Todo parece correcto. Voy a echarle un vistazo al conjunto. Ya te cuento.
m0skit0:
Añádete un evento Winsock1_Connect() y ponle un MsgBox, para ver si la conexión se realiza con éxito. Dependiendo de ello, buscaremos en un sentido u otro. Y en el evento Winsock1_Error() usa Description en vez de Number. Suerte.
Navegación
[#] Página Siguiente
Ir a la versión completa