General Socket Help

kurt69

Well-known member
Joined
Jan 17, 2006
Messages
78
Location
Australia
Programming Experience
Beginner
Hi there, I'm writing a server app for a game and I've run into a few problems basically because I'm still newish to sockets.

Firstly I'm restricted to pure sockets I can't use tcpclients or tcplisteners, secondly thanks for reading haha.

Ok my first problem is that I don't know how to properly check for connections on my listener socket, I googled this but could only find tcpclient/listener code. This is the code I have:

VB.NET:
    Public Sub setupserver()
        mgrSock = New Net.Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Stream, Sockets.ProtocolType.Tcp)
        _mgrSock = New packetHandler(mgrSock)
        Dim i As Integer
        For i = 0 To 9
            playerSock(i) = New Net.Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Stream, Sockets.ProtocolType.Tcp)
        Next
        mgrSock.NoDelay = True
        mgrSock.Blocking = True
        mgrSock.LingerState.Enabled = True
        mgrSock.LingerState.LingerTime = 1000
        Dim ad As IPHostEntry = Dns.GetHostEntry("localhost")
        Dim ep As New IPEndPoint(ad.AddressList(0), txbPort.Text)
        mgrSock.Bind(ep)
        mgrSock.Listen(20)
        rtbChat.AppendText(Chr(13) & "Listening on Port " & txbPort.Text)
        running = True
        connectionMgrWorker.RunWorkerAsync()
    End Sub

    Private Sub connectionMgrWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles connectionMgrWorker.DoWork
        'Manages Connections
        'Assign new players their unique socket and pass it to the client and packetHandler classes.
        'Need this on a separate thread.
        Dim i As Integer
        Try
            For i = 0 To 9
                If playerSock(i).Connected = False Then
                    playerSock(i) = mgrSock.Accept()
                End If
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.Message.ToString)
            Exit Try
        Finally
            'get new socket from mgrSock.Accept() and check login
            'spawn a new packetHandler and pass the socket too it
            _playerSock(i) = New packetHandler(playerSock(i))
            'spawn a new client class
            obj_client(i) = New client(_playerSock(i), playerSock(i))
            e.Result = "Debug: Someone has connected."
        End Try
    End Sub

    Private Sub connectionMgrWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles connectionMgrWorker.RunWorkerCompleted
        rtbChat.AppendText(Chr(13) & e.Result.ToString)
        If running = True Then
            connectionMgrWorker.RunWorkerAsync()
        End If
    End Sub

The other problem I have, I don't know how to return multiple things from a function. The current way I do it is by adding the 2 strings together with a separator inbetween then separating later on. Any examples there?
 
Last edited:
Back
Top