It works great!! I'm getting ACK's back from the server with no problem. I can even send multiple messages at a time. HOWEVER...
If I highlight one item and send it... then highlight another item immediately afterwards and send it... I get a connection refused.
If I send one item, wait about 7-8 seconds... then send another one... I'm fine.
Please review my code and tell me where I'm going wrong!! Thanks in advance!!
Expand|Select|Wrap|Line Numbers
- Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
- Dim ListViewSelect As ListView.SelectedListViewItemCollection = Me.ListView1.SelectedItems
- Dim item As ListViewItem
- Dim selecteditem As String
- Dim selecteditemhex As String = Chr(11)
- Dim SelectedCount As String
- Dim tcpClient As New System.Net.Sockets.TcpClient
- Try
- tcpClient.Connect("10.1.1.94", 8444)
- 'tcpClient.Connect("127.0.0.1", 8444)
- Catch ex As Exception
- MessageBox.Show(Err.Description)
- End Try
- SelectedCount = ListViewSelect.Count()
- Select Case SelectedCount
- Case 0
- MessageBox.Show("Please select 1 item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Case Is > 0
- For Each item In ListViewSelect
- selecteditem = selecteditemhex + LTrim(item.SubItems(4).Text)
- Dim networkStream As NetworkStream = tcpClient.GetStream
- If networkStream.CanWrite And networkStream.CanRead Then
- Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(selecteditem)
- networkStream.Write(sendBytes, 0, sendBytes.Length)
- Dim bytes(tcpClient.ReceiveBufferSize) As Byte
- networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
- Dim returndata As String = Encoding.ASCII.GetString(bytes)
- MessageBox.Show("Host returned: " + returndata)
- Else
- If Not networkStream.CanRead Then
- MessageBox.Show("cannot write data to this stream")
- tcpClient.Close()
- Else
- If Not networkStream.CanWrite Then
- MessageBox.Show("cannot read data from this stream")
- tcpClient.Close()
- End If
- End If
- End If
- Next
- End Select
- btnSend.Text = "Sent"
- tcpClient.GetStream.Close()
- 'tcpClient.Close()
- End Sub