Jump to content
  • Advertisement
Sign in to follow this  
programmer100

XNA and VB.NET + C#

This topic is 2130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[PHP]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

[/PHP]

Error when i run program:

[PHP]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:
[/PHP]

Share this post


Link to post
Share on other sites
Advertisement

The stack trace tells you what the problem is. It tells you that you're calling SendString() on a string that is null.

 

I fixed it. One error more and i have no idea what do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!