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
Form:
Client
Class: TCPControl
Form:
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