I've recently just started looking into TCP sockets for Server-Client communications in VB.Net(VS2010); which I have working fine whilst on a single form. However, when I try to load another form it calls the form_load part and runs that part, but then appears as if it's in an endless loop on the original form because the mouse turns to time out whilst on the new form. Anybody know a solution on how to solve this?
Client side Login form(works fine and is responsive)
Mainscreen form(client side) unresponsive after its initially loaded from call within Login form:
Tried breakpointing to find out where the loop appears to be and I think its in the doRead sub.
Client side Login form(works fine and is responsive)
VB.NET:
Public Class Login
Private client As System.Net.Sockets.TcpClient
Private Const BYTES_TO_READ As Integer = 255
Private readBuffer(BYTES_TO_READ) As Byte
Public UName As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
client = New System.Net.Sockets.TcpClient("172.16.3.9", 43001)
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing)
End Sub
Private Sub doRead(ByVal ar As System.IAsyncResult)
Dim totalRead As Integer
Try
totalRead = client.GetStream.EndRead(ar) 'Ends the reading and returns the number of bytes read.
Catch ex As Exception
'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
'to this client and remove it from the list of connected clients.
End Try
If totalRead > 0 Then
'the readBuffer array will contain everything read from the client.
Dim receivedString As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, totalRead)
messageReceived(receivedString)
End If
Try
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) 'Begin the reading again.
Catch ex As Exception
'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
'to this client and remove it from the list of connected clients.
End Try
End Sub
Private Sub messageReceived(ByVal message As String)
If message = "Connected" Then
MessageBox.Show("Welcome " & Username.Text)
MainScreen.Show()
MainScreen.Focus()
End If
End Sub
Private Sub SendMessage(ByVal msg As String)
Dim sw As IO.StreamWriter
Try
sw = New IO.StreamWriter(client.GetStream)
sw.Write(msg)
sw.Flush()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click
Dim strMes As String
strMes = "CONNECT|" & Username.Text & "|" & Password.Text & ChrW(13)
UName = Username.Text
SendMessage(strMes)
End Sub
Private Sub RectangleShape1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
SendMessage("DISCONNECT|")
End
End Sub
End Class
Mainscreen form(client side) unresponsive after its initially loaded from call within Login form:
VB.NET:
Public Class MainScreen
Private Sub MainScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TreeView1.Nodes.Add("Users2")
TreeView1.Nodes(0).Nodes.Add("User3")
End Sub
End Class
Tried breakpointing to find out where the loop appears to be and I think its in the doRead sub.