Home > Software engineering >  To implement the data interaction between threads, vb.net for help?
To implement the data interaction between threads, vb.net for help?

Time:09-17

Imports System.Net
Imports System.Net.Sockets
Imports System. When
Imports System. Text
Imports System. Diagnostics
Imports System. IO


Public Class frmServer

'the server Socket
Dim Clistener As Socket
Dim Slistener As Socket
'and the client session Socket
Dim CmySocket As Socket
Dim SmySocket As Socket
'the server running state
Dim IsRun As Boolean=False
'listen to receive data thread
Dim CmyThread As Thread
Dim SmyThread As Thread


'Dim have As Integer
'Public Shared flag As Integer
'Public Shared BTS () As Byte
'Public Shared bl As Integer


Private cclient As TcpClient
Private sclient As TcpClient
Private lclisten As TcpListener
Private lslisten As TcpListener
Private swWriter As StreamWriter
Private srReader As StreamReader
Private cwWriter As StreamWriter
Private nsStream As NetworkStream
Private cnsStream As NetworkStream


Private Sub Form1_Load (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles MyBase. Load
CmyThread=New Thread (AddressOf Listen)
'SmyThread=New Thread (AddressOf SListen)

CmyThread. Start ()
'CmyThread. The Join ()
'SmyThread. Start ()
'SmyThread. The Join ()

End Sub


Public Sub Listen ()
Cclient=New TcpClient (502) "192.168.186.128,"
Dim cbytes () As Byte=New (Byte) (1024) {}
'the BTS=New (Byte) (1024) {}
'Dim the data As String=Nothing
Dim ClocalEndPoint As New IPEndPoint (IPAddress. Parse (" 1.1.1.10 "), 502)
'initialize the socket
Clistener=New Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp)
'binding port
Clistener. Bind (ClocalEndPoint)
'to start listening to
Clistener. Listen (10)
The BeginInvoke (New EventHandler (AddressOf AddInfo), "the server is started, is waiting for the connection...
")CmySocket=Clistener. The Accept ()
IsRun=True


NsStream=cclient. GetStream ()
SwWriter=New StreamWriter (nsStream)
SrReader=New StreamReader (nsStream)
While True
Dim bytesRec As Integer=CmySocket. The Receive (cbytes)
'bl=bytesRec
'flag=12
'Dim judge As Byte=cbytes (0)
Dim cc As Integer=cbytes (11)
Dim sc As Integer=cc * 2 + 8
Dim ccbytes () As Byte=New/Byte (sc) {}
Dim csbytes () As Byte=New/Byte (sc) {}
Dim chars (1024) As Char
Dim CTMP As Integer=readReg (cbytes, bytesRec ccbytes, sc)

NsStream. Write (cbytes, 0, bytesRec)
NsStream. Flush ()
NsStream. Read (csbytes, 0, sc)
'Dim s As String
'cc=srReader. Read (chars, 0, sc)
'MsgBox (cc)
'the cc=chartobytes (chars, sc, csbytes)
's=""
"' Dim I As an Integer
'For I=0 To sc
's +=CStr (csbytes (I))
'Next
'MsgBox (s)
CmySocket. Send (csbytes)
End While



End Sub
Public Sub SListen ()
Sclient=New TcpClient (502) "192.168.186.1,"
Dim sbytes () As Byte=New (Byte) (1024) {}
Dim the data As String=Nothing

Dim SlocalEndPoint As New IPEndPoint (IPAddress. Parse (" 192.168.186.1 "), 502)
'initialize the socket
Slistener=New Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp)
'binding port
Slistener. Bind (SlocalEndPoint)
'to start listening to
Slistener. Listen (10)
The BeginInvoke (New EventHandler (AddressOf AddInfo), "the server is started, is waiting for the connection...
")SmySocket=Slistener. The Accept ()
IsRun=True

CnsStream=sclient. GetStream ()
CwWriter=New StreamWriter (cnsStream)
While True
Dim SbytesRec As Integer=SmySocket. The Receive (sbytes)
'Dim judge As Byte=cbytes (0)
Dim cs As Integer=sbytes (11)
Dim ss As Integer=cs * 2 + 8
Dim ssbytes () As Byte=New/Byte (ss) {}
Dim STMP As Integer=readReg (sbytes SbytesRec, ssbytes, ss)
CnsStream. Write (ssbytes, 0, ss)
'SmySocket. Send (ssbytes)
End While

End Sub


'send information
'Private Sub btnSend_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles btnSend. Click
. 'Dim MSG As Byte ()=Encoding UTF8. GetBytes (" Chat | "+ TextBox1. Text)
'CmySocket. Send (MSG)
'the ListBox1. Items. Insert (0, "local information:" + TextBox1. Text)
'the TextBox1. Text=""
'End Sub


'the UI thread calls the form controls to ensure the safety of the thread, have nothing to do with chat implementation
Sub AddInfo (ByVal sender As System. Object, ByVal e the As System. EventArgs)
ListBox1. Items. Insert (0, "remote information:" + sender. ToString)
End Sub


'hair shut out information when you close the window and clean up resources
Private Sub Form1_FormClosing (ByVal sender As Object, ByVal e the As System. Windows, Forms the FormClosingEventArgs) Handles Me. FormClosing
If IsRun Then
. Dim MSG As Byte ()=Encoding UTF8. GetBytes (" Exit | server side Exit: "+ Me. The Handle. The ToString)
Dim bytesSent As Integer=CmySocket. Send (MSG)
End the If
Clistener. Close ()
Slistener. Close ()
CmySocket=Nothing
SmySocket=Nothing
CmyThread. Abort ()
SmyThread. Abort ()
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related