I am new to VB.NET. I am making a TCP/IP Chat Application. I have struggled a lot, till i came across a very simple guide to make one. I followed all the instructions, step by step.
My problem is that when i directly run the Server and the Client, I get a NOtValidOperationException. It says, i have performed a cross threading.
However, when i execut from the .exe file in the bin directory. I am able to run them. The clients are able to connect to the server successfully. The Clients are able to send message to the server, but the client don't rceieve messages from the server. Or better say, the broad-casted message from the server is not shown on the clients text_display.
I need help finishing this project as soon as possible.
This the guide. It is from Microsoft Website. https://msdn.microsoft.com/en-us/library/aa478452.aspx
Thanks,
Clinton.
Complete Code for Server and Client are attached:
ServerSocket:
ClientSocket:
My problem is that when i directly run the Server and the Client, I get a NOtValidOperationException. It says, i have performed a cross threading.
However, when i execut from the .exe file in the bin directory. I am able to run them. The clients are able to connect to the server successfully. The Clients are able to send message to the server, but the client don't rceieve messages from the server. Or better say, the broad-casted message from the server is not shown on the clients text_display.
I need help finishing this project as soon as possible.
This the guide. It is from Microsoft Website. https://msdn.microsoft.com/en-us/library/aa478452.aspx
Thanks,
Clinton.
Complete Code for Server and Client are attached:
ServerSocket:
Code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Net
Public Class Form1
Private mcolClients As New Hashtable()
Private mobjListener As TcpListener
Public Delegate Sub StatusInvoker(ByVal t As String)
Private Sub UpdateStatus(ByVal t As String)
lstStatus.Items.Add(t)
lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mobjThread As Threading.Thread
mobjThread = New Threading.Thread(AddressOf DoListen)
mobjThread.Start()
UpdateStatus("Listener started")
End Sub
Private Sub DoListen()
Try
Dim IpAdd As IPAddress = IPAddress.Parse("127.0.0.1")
mobjListener = New TcpListener(IpAdd, 5000)
mobjListener.Start()
Do
Dim x As New Client(mobjListener.AcceptTcpClient)
AddHandler x.Connected, AddressOf OnConnected
AddHandler x.Disconnected, AddressOf OnDisconnected
AddHandler x.LineReceived, AddressOf OnLineReceived
mcolClients.Add(x.ID, x)
Dim params() As Object = {"New connection"}
Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
Loop Until False
Catch e As Exception
UpdateStatus("Exception")
End Try
End Sub
Private Sub Form1_Closing(ByVal sender As Object,
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
mobjListener.Stop()
End Sub
Private Sub OnConnected(ByVal sender As Client)
UpdateStatus("Connected")
End Sub
Private Sub OnDisconnected(ByVal sender As Client)
UpdateStatus("Disconnected")
mcolClients.Remove(sender.ID)
End Sub
Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
UpdateStatus("New Message: " & Data)
Dim objClient As Client
Dim d As DictionaryEntry
For Each d In mcolClients
objClient = d.Value
objClient.Send(Data & vbCrLf)
Next
End Sub
End Class
Public Class Client
Public Event Connected(ByVal sender As Client)
Public Event Disconnected(ByVal sender As Client)
Public Event LineReceived(ByVal sender As Client, ByVal Data As String)
Private marData(1024) As Byte
Private mobjText As New StringBuilder()
Private mgID As Guid = Guid.NewGuid
Public ReadOnly Property ID() As String
Get
Return mgID.ToString
End Get
End Property
Private mobjClient As TcpClient
Public Sub New(ByVal client As TcpClient)
mobjClient = client
RaiseEvent Connected(Me)
mobjClient.GetStream.BeginRead(marData, 0, 1024,
AddressOf DoReceive, Nothing)
End Sub
Private Sub DoReceive(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try
SyncLock mobjClient.GetStream
intCount = mobjClient.GetStream.EndRead(ar)
End SyncLock
If intCount < 1 Then
RaiseEvent Disconnected(Me)
Exit Sub
End If
BuildString(marData, 0, intCount)
SyncLock mobjClient.GetStream
mobjClient.GetStream.BeginRead(marData, 0, 1024,
AddressOf DoReceive, Nothing)
End SyncLock
Catch e As Exception
RaiseEvent Disconnected(Me)
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte,
ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 13 Then
RaiseEvent LineReceived(Me, mobjText.ToString)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Public Sub Send(ByVal Data As String)
SyncLock mobjClient.GetStream
Dim w As New IO.StreamWriter(mobjClient.GetStream)
w.Write(Data)
w.Flush()
End SyncLock
End Sub
End Class
Code:
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports System.Net
Public Class Form1
Private mobjClient As TcpClient
Private marData(1024) As Byte
Private mobjText As New StringBuilder()
Private Sub DisplayText(ByVal t As String)
txtDisplay.AppendText(t)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ipAdd As IPAddress = IPAddress.Parse("127.0.0.1")
mobjClient = New TcpClient("localhost", 5000)
DisplayText("Connecetd to host" & vbCrLf)
End Sub
Private Sub Send(ByVal Data As String)
Dim w As New StreamWriter(mobjClient.GetStream())
w.Write(Data & vbCrLf)
w.Flush()
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
Send(txtSend.Text)
txtSend.Text = ""
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try
intCount = mobjClient.GetStream.EndRead(ar)
If (intCount < 1) Then
MarkAsDisconnected()
Exit Sub
End If
BuildString(marData, 0, intCount)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
Catch ex As Exception
MarkAsDisconnected()
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 10 Then
mobjText.Append(vbCrLf)
Dim params() As Object = {mobjText.ToString}
Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)
mobjText = New StringBuilder()
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Public Delegate Sub DisplayInvoker(ByVal t As String)
Private Sub MarkAsDisconnected()
DisplayText("Server Disconnected")
End Sub
End Class