vb.net 实现线程间数据交互,求助?

qqmushui 2018-08-17 07:12:02
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

...全文
566 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
milaoshu1020 2018-10-18
  • 打赏
  • 举报
回复
请问代码运行起来有什么问题吗?
qqmushui 2018-08-17
  • 打赏
  • 举报
回复
我想实现一个线程接收数据然后通过另一个线程发送出去,求大神们帮忙看下

1,502

社区成员

发帖
与我相关
我的任务
社区描述
VB 网络编程
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧