Server that can accept multiply clients

Acuena

New member
Joined
Feb 28, 2015
Messages
3
Programming Experience
Beginner
Hi!

New to the forum :)

Anyways, can someone recommend a tutorial on how to make a server and client setup where the server can handle multiply connections at once?
I have seen this tutorial and followed it: https://www.youtube.com/watch?v=MSiBbtxWpI8
He sad that he might do a tutorial on how to do it but as i can't find any.

I have also tried to make so I could specify an IP and Port when the program is running. It looks like the client connect but nothing shows up on the server when I send data to the server.
Here is the code for the server, if someone would like to take the time to help(Link to code: https://www.dropbox.com/s/a31ysb91hnluxg0/ServerClient.zip?dl=0):

Server

Class: TCPServer

VB.NET:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class TCPControl

    ' Custome event
    Public Event MessageRecived(Sender As TCPControl, Data As String)
    'Sever config
    'Dim ServerIP As IPAddress = IPAddress.Parse("127.0.0.1")
    'Dim ServerPort As Integer = "64555"

    Dim Server As TcpListener

    'Listner thread
    Private CommThread As Thread
    Public IsListning As Boolean = True

    ' Clients
    Private Client As TcpClient
    Private ClientData As StreamReader

    Public Sub New(IP As IPAddress, Port As Integer)
        Dim ServerIP As IPAddress = IP
        Dim ServerPort As Integer = "64555"

        Server = New TcpListener(ServerIP, Port)
        Server.Start()

        CommThread = New Thread(New ThreadStart(AddressOf Listening))
        CommThread.Start()

    End Sub

    Private Sub Listening()
        'Create listner loop
        Do Until IsListning = False
            ' Accept incomming connections
            If Server.Pending = True Then
                Client = Server.AcceptTcpClient
                ClientData = New StreamReader(CLient.GetStream)
            End If

            'Raise event for incomming messages

            Try
                RaiseEvent MessageRecived(Me, ClientData.ReadLine)
            Catch ex As Exception
                ' MsgBox(ex.Message.ToString)
            End Try

            ' Reduce CPU usage
            Thread.Sleep(100)
        Loop

    End Sub
End Class

Form:

VB.NET:
Imports System.Net

Public Class Form1
    Private Server As TCPControl

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Server.IsListning = False
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Server.MessageRecived, AddressOf OnLineReceived
    End Sub

    Private Delegate Sub UpdateTextDelegate(TB As TextBox, txt As String)
    ' Update textbox
    Private Sub UpdateText(TB As TextBox, txt As String)
        If TB.InvokeRequired Then
            TB.Invoke(New UpdateTextDelegate(AddressOf UpdateText), New Object() {TB, txt})
        Else
            If Not IsNothing(txt) = True Then TB.AppendText(txt & vbCrLf)
        End If
    End Sub

    Private Sub OnLineReceived(Sender As TCPControl, Data As String)
        UpdateText(TxtChat, Data)
    End Sub

    Private Sub CmdStart_Click(sender As Object, e As EventArgs) Handles CmdStart.Click
        Dim ServerIP As IPAddress = IPAddress.Parse(TxtIP.Text.ToString)
        'Dim ServerPort As Integer = "64555"

        Server = New TCPControl(ServerIP, "64555")
        TxtChat.Text = ":: Server started ::" & vbCrLf
    End Sub
End Class


Client

Class: TCPControl

VB.NET:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Public Class TCPControl
    Public Client As TcpClient
    Public DataStream As StreamWriter

    Public Sub New(Host As String, Port As Integer)
        ' Client
        Client = New TcpClient(Host, Port)
        DataStream = New StreamWriter(Client.GetStream)
    End Sub

    Public Sub Send(Data As String)
        DataStream.Write(Data & vbCrLf)
        DataStream.Flush()
    End Sub
End Class

Form:

VB.NET:
Public Class Form1
    Private Client As TCPControl

    Private Sub CmdConnect_Click(sender As Object, e As EventArgs) Handles CmdConnect.Click
        Client = New TCPControl(TxtIP.Text, TxtPort.Text)
        If Client.Client.Connected Then
            CmdConnect.Text = "Connected"
            CmdConnect.Enabled = False
        End If
    End Sub

    Private Sub CmdSend_Click(sender As Object, e As EventArgs) Handles CmdSend.Click
        SendMsg(TxtText.Text)
        TxtText.Clear()
        TxtText.Focus()
    End Sub

    Public Sub SendMsg(Data As String)
        If Client.Client.Connected = True Then
            If Not Data = "" Then
                Client.Send(Data)
            End If
        End If
    End Sub

    Private Sub TxtText_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtText.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendMsg(TxtText.Text)
            TxtText.Clear()
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Client.Client.Connected = True Then
            Client.DataStream.Close()
            Client.Client.Close()
        End If
    End Sub
End Class
 
You don't really need a tutorial. You just need to think it through a bit. When you call AcceptTcpClient you will create a TcpClient when a client connects. If you want to accept more client connections then you simply need to call AcceptTcpClient again after a connection. It's not rocket science; if you know how to accept a connection and you want to accept multiple connections then you simply accept a single connection multiple times. That said, you might want to check out this example of mine, which was more about the asynchronous aspect but does demonstrate multiple clients:

Asynchronous TcpListener & TcpClient
 
Had to launch two seperate instancies of Visual Studio to be able to run both server and client, but then I managed to make it work :)
Gonna digg into the code to try to figure out how to do what I want.
Thanks jmcilhinney
 
Had to launch two seperate instancies of Visual Studio to be able to run both server and client, but then I managed to make it work :)

I don't think you actually did. I don't know whether VS Express is different but I doubt it. In VS Pro and above, you can right-click the solution and select the option to set the startup project(s), which allows you to select one or multiple projects to start when you select Run. Even if you only have one startup project, after running it you can still right-click another project and choose to run it as well.
 
I don't know whether VS Express is different but I doubt it. In VS Pro and above, you can right-click the solution and select the option to set the startup project(s), which allows you to select one or multiple projects to start when you select Run.
VB Express has this as well.
 
Grrr, don't like anti virus programs and false positives. The one we have at my work sad that my program using your code (not the dll, just my exe) was a virus. Uploaded the files to VirusTotal.com and it reported nothing.
Grr
 
Back
Top