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
uj5u.com熱心網友回復:
我想實作一個執行緒接收資料然后通過另一個執行緒發送出去,求大神們幫忙看下uj5u.com熱心網友回復:
請問代碼運行起來有什么問題嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/42042.html
標籤:網絡編程
上一篇:vbs打不開
