VB.NET Sockets

SillyRabbit

New member
Joined
Mar 1, 2005
Messages
3
Location
Ontario, Canada
Hi there. Im working with a basic socket server/client setup. Being my frist time working with sockets...Im a bit inexperienced with it.

Server Code:
Code:
    Sub Main()
         Must listen on correct port- must be same as port client wants to connect on.
        Dim tcpListener As New System.Net.Sockets.TcpListener(55432)
        tcpListener.Start(500)
        Try
            Accept the pending client connection and return 
            a TcpClient initialized for communication.
            Console.WriteLine("Waiting for connection...")
            Dim tcpClient As System.Net.Sockets.TcpClient = tcpListener.AcceptTcpClient()
            Console.WriteLine("Connection accepted.")
             Get the stream
            Dim networkStream As System.Net.Sockets.NetworkStream = tcpClient.GetStream()
             Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
             Return the data received from the client to the console.
            Dim clientdata As String = System.Text.Encoding.UTF8.GetString(bytes)
            Console.WriteLine(("Client sent: " + clientdata))
            Dim s As String() = System.Text.RegularExpressions.Regex.Split(clientdata, " ")
            Dim username As String
            Dim status As String
            username = s(0)
            status = s(1)
            Console.WriteLine("... Username is " + username + " and Status is " + status)
            Dim responseString As String = "Success"
            Dim sendBytes As [Byte]() = System.Text.Encoding.UTF8.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            tcpClient.Client.Disconnect(False)
            tcpClient.Close()
            If tcpClient.Client.Connected = False Then
                Console.WriteLine("... Connection closed.")
            End If
            Console.ReadLine()
        Catch err As Exception
            Console.WriteLine(err.ToString())
            Console.ReadLine()
        End Try
    End Sub

Client Code:
Code:
Public Function UpdateStatus()
        Dim n As Integer = 0
        Dim username As String = "sillyrabbit"
        Dim status As String = "away"
        Dim presence As New Net.Sockets.TcpClient()
        Dim server As String = "127.0.0.1"

        presence.Connect(server, 55432)

        Dim networkStream As System.Net.Sockets.NetworkStream = presence.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
             Do a simple write.
            Dim sendBytes As [Byte]() = System.Text.Encoding.UTF8.GetBytes(username & " " & status)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
             Read the NetworkStream into a byte buffer.
            Dim bytes(presence.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(presence.ReceiveBufferSize))
             Output the data received from the host to the console.
            Dim returndata As String = System.Text.Encoding.UTF8.GetString(bytes)
            Console.WriteLine((returndata))
            If returndata.Contains("Success") Then
                presence.Client.Disconnect(False)
                presence.Close()
            Else : MsgBox("Error:" & returndata)
                presence.Close()
            End If
        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                presence.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    presence.Close()
                End If
            End If
        End If
    End Function

The two connect, communicate and disconnect perfectly. However, I cant figure out how to loop the server so as soon as the client is disconnected, its ready to accept a new client and repeat the process.

EXAMPLE: Server is running. Client connects, does its business disconnects. Server is still running. Client tries to connect again, nothing happens (obviously).

If anybody could help me out with this loop or even some code improvements I would be very appreciative. Thank you very much.

-rabbit
 
One client at a time...but if I could do more that would be awesome. But if only one at a time, Id need it so that if any incoming connections come in while it is already in use, theyd go in a queue.

-rabbit
 
Back
Top