vb.net 实现线程间数据交互,求助?
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Diagnostics
Imports System.IO
Public Class frmServer
'服务器端的Socket
Dim Clistener As Socket
Dim Slistener As Socket
'与客户端会话的Socket
Dim CmySocket As Socket
Dim SmySocket As Socket
'服务器端的运行状态
Dim IsRun As Boolean = False
'监听接收数据线程
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 As System.EventArgs) Handles MyBase.Load
CmyThread = New Thread(AddressOf Listen)
' SmyThread = New Thread(AddressOf SListen)
CmyThread.Start()
' CmyThread.Join()
' SmyThread.Start()
' SmyThread.Join()
End Sub
Public Sub Listen()
cclient = New TcpClient("192.168.186.128", 502)
Dim cbytes() As Byte = New [Byte](1024) {}
' bts = New [Byte](1024) {}
'Dim data As String = Nothing
Dim ClocalEndPoint As New IPEndPoint(IPAddress.Parse("1.1.1.10"), 502)
'初始化socket
Clistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'绑定端口
Clistener.Bind(ClocalEndPoint)
'开始监听
Clistener.Listen(10)
BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
CmySocket = Clistener.Accept()
IsRun = True
nsStream = cclient.GetStream()
swWriter = New StreamWriter(nsStream)
srReader = New StreamReader(nsStream)
While True
Dim bytesRec As Integer = CmySocket.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)
'cc = chartobytes(chars, sc, csbytes)
's = ""
''Dim i As 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("192.168.186.1", 502)
Dim sbytes() As Byte = New [Byte](1024) {}
Dim data As String = Nothing
Dim SlocalEndPoint As New IPEndPoint(IPAddress.Parse("192.168.186.1"), 502)
'初始化socket
Slistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'绑定端口
Slistener.Bind(SlocalEndPoint)
'开始监听
Slistener.Listen(10)
BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
SmySocket = Slistener.Accept()
IsRun = True
cnsStream = sclient.GetStream()
cwWriter = New StreamWriter(cnsStream)
While True
Dim SbytesRec As Integer = SmySocket.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
'发送信息
'Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
' Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" + TextBox1.Text)
' CmySocket.Send(msg)
' ListBox1.Items.Insert(0, "本地信息: " + TextBox1.Text)
' TextBox1.Text = ""
'End Sub
'非UI线程调用窗体控件,保证线程安全。与聊天实现无关
Sub AddInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.Items.Insert(0, "远程信息: " + sender.ToString)
End Sub
'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsRun Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|服务器端退出: " + Me.Handle.ToString)
Dim bytesSent As Integer = CmySocket.Send(msg)
End If
Clistener.Close()
Slistener.Close()
CmySocket = Nothing
SmySocket = Nothing
CmyThread.Abort()
SmyThread.Abort()
End Sub
End Class
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Diagnostics
Imports System.IO
Public Class frmServer
'服务器端的Socket
Dim Clistener As Socket
Dim Slistener As Socket
'与客户端会话的Socket
Dim CmySocket As Socket
Dim SmySocket As Socket
'服务器端的运行状态
Dim IsRun As Boolean = False
'监听接收数据线程
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 As System.EventArgs) Handles MyBase.Load
CmyThread = New Thread(AddressOf Listen)
' SmyThread = New Thread(AddressOf SListen)
CmyThread.Start()
' CmyThread.Join()
' SmyThread.Start()
' SmyThread.Join()
End Sub
Public Sub Listen()
cclient = New TcpClient("192.168.186.128", 502)
Dim cbytes() As Byte = New [Byte](1024) {}
' bts = New [Byte](1024) {}
'Dim data As String = Nothing
Dim ClocalEndPoint As New IPEndPoint(IPAddress.Parse("1.1.1.10"), 502)
'初始化socket
Clistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'绑定端口
Clistener.Bind(ClocalEndPoint)
'开始监听
Clistener.Listen(10)
BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
CmySocket = Clistener.Accept()
IsRun = True
nsStream = cclient.GetStream()
swWriter = New StreamWriter(nsStream)
srReader = New StreamReader(nsStream)
While True
Dim bytesRec As Integer = CmySocket.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)
'cc = chartobytes(chars, sc, csbytes)
's = ""
''Dim i As 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("192.168.186.1", 502)
Dim sbytes() As Byte = New [Byte](1024) {}
Dim data As String = Nothing
Dim SlocalEndPoint As New IPEndPoint(IPAddress.Parse("192.168.186.1"), 502)
'初始化socket
Slistener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'绑定端口
Slistener.Bind(SlocalEndPoint)
'开始监听
Slistener.Listen(10)
BeginInvoke(New EventHandler(AddressOf AddInfo), "服务器端已启动,正在等待连接......")
SmySocket = Slistener.Accept()
IsRun = True
cnsStream = sclient.GetStream()
cwWriter = New StreamWriter(cnsStream)
While True
Dim SbytesRec As Integer = SmySocket.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
'发送信息
'Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
' Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" + TextBox1.Text)
' CmySocket.Send(msg)
' ListBox1.Items.Insert(0, "本地信息: " + TextBox1.Text)
' TextBox1.Text = ""
'End Sub
'非UI线程调用窗体控件,保证线程安全。与聊天实现无关
Sub AddInfo(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.Items.Insert(0, "远程信息: " + sender.ToString)
End Sub
'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsRun Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|服务器端退出: " + Me.Handle.ToString)
Dim bytesSent As Integer = CmySocket.Send(msg)
End If
Clistener.Close()
Slistener.Close()
CmySocket = Nothing
SmySocket = Nothing
CmyThread.Abort()
SmyThread.Abort()
End Sub
End Class
...全文
相关推荐
VB.NET中实现文本的复制粘贴(调用剪贴板)
综上所述,VB.NET中实现文本复制粘贴主要依赖于System.Windows.Forms命名空间下的Clipboard类。
VB.Net多线程编程基础与实例
在VB.Net中,线程的创建和管理可以通过.NET Framework提供的命名空间下的Thread类来实现。Thread类是线程编程的基础,它封装了线程的操作。创建线程的基本步骤包括实例化Thread类的一个对象,将要运行的方法作为线程的入口点传递给构造函数,然后调用方法来启动线程。一旦启动,操作系统调度器就会管理该线程的执行。下面是使用Thread类创建线程的一个简单示例:Sub Main()' 创建线程实例' 启动线程t.Start()End Sub。
VB.net实现打印的简单方法
在VB.NET中,打印功能是应用程序开发中必不可少的一部分,它允许用户将数据或文档输出到打印机。本篇文章将深入探讨如何使用VB.NET实现简单的打印功能,主要包括以下知识点:1.
vb.net实现tcp通讯
最后,文档中的"TCP通讯代码.doc"可能包含了完整的示例代码,包括客户端和服务端的实现,以及可能的错误处理和性能优化策略。研究这个文档可以帮助你更好地理解VB.NET中的TCP通讯实现。
用VB.net实现OPC客户端
VB.NET中的OPC客户端实现在使用VB.NET开发OPC客户端程序时,首先需要引用OPC服务器提供的COM库。这通常涉及以下几个步骤:1.


网络编程
