Imports
Imports
Imports System.Threading
Imports System.Text
Imports System.Diagnostics
Imports
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
Private cclient As Socket
Private swWriter As StreamWriter
Private nsStream 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()
' SmyThread.Start()
End Sub
Public Sub Listen()
Dim cbytes() As Byte = 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
''''''''''''''''''''''''
Dim remotePoint As New IPEndPoint(IPAddress.Parse("192.168.186.128"), 502)
cclient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
cclient.Connect(remotePoint)
'cclient = New TcpClient("192.168.186.128", 502)
'nsStream = cclient.GetStream()
'swWriter = New StreamWriter(nsStream)
'''''''''''''''''''''''''
While True
Dim bytesRec As Integer = CmySocket.Receive(cbytes)
''Dim judge As Byte = cbytes(0)
Dim cc As Integer = cbytes(11)
Dim sc As Integer = cc * 2 + 8
'MsgBox(sc)
'nsStream.Write(cbytes, 0, bytesRec)
Dim ccbytes() As Byte = New [Byte](sc) {}
cclient.Send(cbytes)
cclient.Receive(ccbytes)
' Dim i As Integer = 0
'While (nsStream.DataAvailable = False)
'End While
'ReDim ccbytes(sc)
'nsStream.Read(ccbytes, 0, sc)
''ccbytes(1) += 1
CmySocket.Send(ccbytes)
' MsgBox(i)
End While
End Sub
Public Sub SListen()
Dim sbytes() As Byte = New [Byte](1024) {}
Dim data As String = Nothing
Dim SlocalEndPoint As New IPEndPoint(IPAddress.Parse("2.2.2.2"), 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
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)
SmySocket.Send(ssbytes)
End While
End Sub
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