Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.Rows.Add("11", "2", "3", "4") ' This is added to the grid upon starting up so this works.
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' DECLARE VARIABLES FOR BACKGROUND WORKER THREAD
Dim ipAddress As IPAddress = Dns.GetHostEntry("localhost").AddressList(0)
Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 8888)
Dim serverSocket As New TcpListener(ipLocalEndPoint)
Dim clientSocket As TcpClient
Dim counter As Integer
' START TCP LISTENER
serverSocket.Start()
' WHAT DO WE NEED THIS COUNTER FOR?
counter = 0
While (True)
' DO WE REALLY NEED A COUNT THAT IS NEVER USED?
counter += 1
' ACCEPT PENDING CONNECTION REQUEST
clientSocket = serverSocket.AcceptTcpClient()
' I GOT RID OF YOUR STARTCLIENT() ROUTINE BECAUSE IT WAS NOT NEEDED
' DO YOUR "CHATTING"
doChat(clientSocket, BackgroundWorker1)
End While
' STOP THE TCP LISTENER
serverSocket.Stop()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim D(20) As String
Dim ReceivedLine As String = ""
ReceivedLine = CType(e.UserState, String)
MsgBox(ReceivedLine) ' -----This shows the correct full string with ~ delimiters
D = ReceivedLine.Split(CChar("~"))
Try
' This monitors the progress. Row count is incremented each time
' this is done. MsgBox above and here shows the data both as
' ReceivedLine and D. Row is increased by count but does not display
' in the form1 grid
MsgBox("Rows " + DataGridView1.RowCount.ToString)
MsgBox("data is " + D(0) + " " + D(1) + " " + D(2) + " " + D(3)) ' currently only using the first 4 variables until it works
Me.DataGridView1.Rows.Add(D)
'Me.MainGrid.Rows.Add(D(0), D(1), D(2), D(3)) ' Not used per above line
DataGridView1.Refresh()
MsgBox("Rows " + DataGridView1.RowCount.ToString)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' NOTICE I USED BYREF AND NOT BYVAL. THIS LETS ME USE THE TCPCLIENT THAT HAS ALREADY BEEN DECLARED
Public Sub doChat(ByRef clientSocket As TcpClient, ByRef bw As System.ComponentModel.BackgroundWorker)
' DECLARE VARIABLES FOR THIS SUB ONLY
Dim requestCount As Integer = 0
Dim dataFromClient As String
Dim ReceivedLine As String = ""
Dim bytesFrom(10024) As Byte
Dim RtnString As String = ""
While (True)
Try
' REALLY? WHY DO WE NEED TO COUNT WHEN THE COUNT IS NOT USED?
requestCount = requestCount + 1
' OPEN NETWORK STREAM
Dim networkStream As NetworkStream = clientSocket.GetStream()
' READ YOUR BYTES
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
' PARSE YOUR DATA
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
' PASS THE DATA BACK UP TO THE BACKGROUNDWORKER
If dataFromClient.Contains(vbCr) Or dataFromClient.Contains(vbCrLf) Then
bw.ReportProgress(1, ReceivedLine)
RtnString = ReceivedLine
SendTCP(networkStream, ReceivedLine)
ReceivedLine = ""
Else
ReceivedLine += dataFromClient.Substring(0, 1)
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End While
End Sub
Sub SendTCP(ByRef N As NetworkStream, ByVal R As String)
Dim sendBytes As [Byte]()
Dim serverResponse As String
serverResponse = "Server to clinet(" + R + ") "
sendBytes = Encoding.ASCII.GetBytes(serverResponse)
N.Write(sendBytes, 0, sendBytes.Length)
N.Flush()
End Sub
End Class