Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 TcpClient receive Data from server.

$
0
0
My server is non-stop send out the hex code,then i want to write a small program to receive the hex data from server.
i facing a receive problem, the tcpclient only receive once only.

how can i make my program auto receive the hex data?

here is my code

VB code Code:
  1. Imports System
  2. Imports System.ComponentModel
  3. Imports System.Net
  4. Imports System.Net.Sockets
  5. Imports System.Text
  6. Imports System.Configuration
  7. Imports vb = Microsoft.VisualBasic
  8.  
  9.     Public WithEvents clientSocket As New System.Net.Sockets.TcpClient()
  10.     Public WithEvents myNetworkStream As System.Net.Sockets.NetworkStream
  11.  
  12. Public Class frm_control_status
  13.     Private ptX, ptY As Integer
  14.     Dim dragging As Boolean = False
  15.     Private pb As PictureBox
  16.     Private comBuffer As Byte()
  17.     Private Delegate Sub UpdateFormDelegate()
  18.     Private UpdateFormDelegate1 As UpdateFormDelegate
  19.     Dim rcvQ As New List(Of Byte)
  20.     Dim rcvQlock As New Object
  21.  
  22. Private Sub frm_control_status_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  23.  
  24. Dim ipadd, portnum As String
  25.         ipadd = My.Settings.txt_ip_address
  26.         portnum = My.Settings.txt_port_num
  27.         If ipadd <> "" Then
  28.             Try
  29.                 clientSocket.ReceiveBufferSize = 4096
  30.                 clientSocket.Connect(ipadd, portnum)
  31.  
  32.                 If clientSocket.Connected = True Then
  33.                     frm_main.lbl_server.Text = "Client Socket - Server Connected!"
  34.                 End If
  35.  
  36.             Catch ex As Exception
  37.                 MessageBox.Show(ex.ToString)
  38.                 frm_main.lbl_server.Text = "Client Socket - Disconnect!"
  39.             End Try
  40.  
  41.         Else
  42.             MsgBox("Didn't set the host IP or Port number")
  43.         End If
  44.  
  45.         myNetworkStream = clientSocket.GetStream
  46.         If myNetworkStream.CanRead = True Then
  47.             Call DataReceived()
  48.         End If
  49.  
  50. End Sub
  51.  
  52. Private Sub DataReceived()
  53.  
  54.         Dim n As Integer = myNetworkStream.ReadByte
  55.         Dim comBuffer(n - 1) As Byte
  56.         UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
  57.         myNetworkStream.Read(comBuffer, 0, n)
  58.         Threading.Monitor.Enter(rcvQlock)
  59.         rcvQ.AddRange(comBuffer)
  60.         Threading.Monitor.Exit(rcvQlock)
  61.         Me.BeginInvoke(UpdateFormDelegate1)
  62.  
  63.     End Sub
  64.  
  65. Private Sub UpdateDisplay()
  66. do my work......
  67. End Sub
  68.  
  69.  
  70. End Class

please help me!

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>