Jump to content

  • Log In with Google      Sign In   
  • Create Account


programmer100

Member Since 12 Sep 2013
Offline Last Active Sep 22 2013 04:32 AM

Topics I've Started

XNA and VB.NET + C#

12 September 2013 - 02:46 AM

Hello! I have small problem! I try make XNA multiplayer game using VB.NET, but my client socket not working on XNA but working VB.NET and C#. Server get sended data but game get error when try receive data and two small error.

XNA code:

Public Class Server

    Private Shared _clientSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Private Shared _PORT As Integer = 21
    Public Shared Sub ConnectToServer()
        RequestLoop()
        [Exit]()
        Dim attempts As Integer = 0
 
        While Not _clientSocket.Connected
            Try
                attempts += 1
                Console.WriteLine("Connection attempt " & attempts)
                _clientSocket.Connect(IPAddress.Loopback, _PORT)
            Catch generatedExceptionName As SocketException
                Console.Clear()
            End Try
        End While
 
        Console.Clear()
        Console.WriteLine("Connected")
    End Sub
    Private Shared Sub RequestLoop()
        Console.WriteLine("<Type ""exit"" to properly disconnect client>")
 
        While True
            SendRequest("")
            ReceiveResponse()
        End While
    End Sub
    ''' <summary>
    ''' Close socket and exit app
    ''' </summary>
    Private Shared Sub [Exit]()
        SendString("exit")
        ' Tell the server we re exiting
        _clientSocket.Shutdown(SocketShutdown.Both)
        _clientSocket.Close()
        Environment.[Exit](0)
    End Sub
    Private Shared Sub SendRequest(text As String)
        Console.Write("Send a request: ")
        Dim request As String = text
        SendString(request)
 
        If request.ToLower() = "exit" Then
            [Exit]()
        End If
    End Sub
    ''' <summary>
    ''' Sends a string to the server with ASCII encoding
    ''' </summary>
    Public Shared Sub SendString(text As String)
        If text <> "" Then
            Dim buffer As Byte() = Encoding.ASCII.GetBytes(text)
            _clientSocket.Send(buffer, 0, buffer.Length, SocketFlags.None)
        End If
    End Sub
    Private Shared Sub ReceiveResponse()
        Dim buffer = New Byte(2047) {}
        Dim received As Integer = _clientSocket.Receive(buffer, SocketFlags.None)
        If received = 0 Then
            Return
        End If
        Dim data = New Byte(received - 1) {}
        Array.Copy(buffer, data, received)
        Dim text As String = Encoding.ASCII.GetString(data)
        Console.WriteLine(text)
 
        If text.ToLower() = "movedup" Then
            MsgBox("test")
        End If
    End Sub
End Class



Error when i run program:

System.Net.Sockets.SocketException was unhandled
ErrorCode=10057
HResult=-2147467259
Message=Tietojen lähetys- tai vastaanottopyyntö hylättiin, koska vastake ei ole kytketty ja (lähetettäessä datagrammivastakkeeseen Sendto-kutsua käyttäen) osoitetta ei määritetty
NativeErrorCode=10057
Source=System
StackTrace:
kohteessa System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
kohteessa System.Net.Sockets.Socket.Receive(Byte[] buffer, SocketFlags socketFlags)
kohteessa XNAgame.Server.ReceiveResponse() Server.vb:rivillä 66
kohteessa XNAgame.Server.RequestLoop() Server.vb:rivillä 31
kohteessa XNAgame.Server.ConnectToServer() Server.vb:rivillä 8
kohteessa XNAgame.Game1..ctor() tiedostossa Game1.vb:rivillä 6
kohteessa XNAgame.Program.Main(String[] args) Program.vb:rivillä 8
InnerException:


PARTNERS