Question

I am coding a poker game in vb.net, and I want to create a multiplayer game using the internet, whether it being on WAN or LAN. I really have no idea where to start, and I don't know what to search on Google. I know that I will have to listen on a port, and I need to send and receive packets. No searches on Google are helping. Any links/ideas?

Also, how would I be able to show a list box of all the games currently in progress and allow the user to join? And how to create a new game?

Était-ce utile?

La solution

I'd recommend learning the UdpClient class, it's a good option for beginners.

You first start by 'setting up' two clients, one to send data, and one to listen for incoming data. Once you've assigned the clients with the appropriate addresses and port numbers you then start the listening client in a loop, so that you can consistently listen for data.

Then you 'wire up' your sending client to some form of trigger, (in the example i've given below, i've set my sending client up to a button event) so you can send data at irregular intervals, or you can set your client in a loop to continuously send data.

Once you've done that, you now need to convert the data you want to send from string to a byte array, then you can finally send it, and vis versa for receiving data (from byte array to string).

Here's a simple example,

Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net

Public Class Form1

    Private Const port As Integer = 9653 'Or whatever port number you want to use
    Private Const broadcastAddress As String = "255.255.255.255"
    Private receivingClient As UdpClient
    Private sendingClient As UdpClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        InitializeSender()
        InitializeReceiver()
    End Sub

    Private Sub InitializeSender()
        sendingClient = New UdpClient(broadcastAddress, port) 'Use broadcastAddress for sending data locally (on LAN), otherwise you'll need the public (or global) IP address of the machine that you want to send your data to
        sendingClient.EnableBroadcast = True
    End Sub

    Private Sub InitializeReceiver()
        receivingClient = New UdpClient(port)
        ThreadPool.QueueUserWorkItem(AddressOf Receiver) 'Start listener on another thread
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim stringToSend As String = TextBox1.Text 'Assuming you have a textbox with the data you want to send
        If (Not String.IsNullOrEmpty(stringToSend)) Then
            Dim data() As Byte = Encoding.ASCII.GetBytes(stringToSend)
            sendingClient.Send(data, data.Length)
        End If
    End Sub

    Private Sub Receiver()
        Dim endPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, port) 'Listen for incoming data from any IP on the specified port
        Do While True 'Notice that i've setup an infinite loop to continually listen for incoming data
            Dim data() As Byte
            data = receivingClient.Receive(endPoint)
            Dim message As String = Encoding.ASCII.GetString(data) 'Recived data as string
        Loop
    End Sub
End Class


And now for adding a list box for available games.

Short answer, it's impossible very, very hard to do unless you have a server.

Longer answer, you'll need a server, and assuming you have a server you'll need to create an additional program to handle the data that's being sent to it, and to send data to other users.

I could go on to explain how to setup the additional program etc, etc, but judging that your network programming skills are still 'spreading out their wings', I'd suggest that you leave out advanced features like this until you have some more experience, then when you're more confident have a try by yourself and if you're still struggling just pop a question on here and I'm sure someone will help you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top