16,550
社区成员
发帖
与我相关
我的任务
分享
Imports System.Threading
Public Class ServerMain
Public WithEvents MyServer As TCPServer
Public Delegate Sub ReceiveData(ByVal sType As Integer, ByVal DataString As String)
Private Sub ServerMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
MyServer = New TCPServer(5000, 10000, 4096, 50)
MyServer.StartServer()
Catch ex As Exception
MsgBox(ex.Message, , Application.ProductName & "错误")
End Try
End Sub
Private Sub MyServer_DataArrived(ByVal value() As Byte, ByVal Len As Integer, ByVal IPAddress As String, ByVal Port As String) Handles MyServer.DataArrived
’服务器接收客户端发过来的数据
’Add your code here
Try
Dim myStr As String
myStr = System.Text.UTF8Encoding.UTF8.GetString(value)
Debug.Print(MyStr)
Catch ex As Exception
Debug.Print(ex.message)
End Try
End Sub
Private Sub MyServer_ClientClose(ByVal IP As String, ByVal Port As String) Handles MyServer.ClientClose
‘客户端与服务服务器端断开连接
’Add your code here
End Sub
Private Sub MyServer_ClientConnected(ByVal IP As String, ByVal Port As String) Handles MyServer.ClientConnected
'客户端向服务服务器端发起连接
’Add your code here
End Sub
Private Sub MyServer_Exception(ByVal ex As System.Exception) Handles MyServer.Exception
' Socket套接字通讯捕捉到错误
’Add your code here
End Sub
End Class
在客户端界面窗体代码内添加如下代码:
Imports System.Windows.Forms
Imports System.Threading
Imports System.Net
Public Class ClientMain
Public MyClient(N) As TCPClient
Private Sub ClientMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For index As Integer = 0 To 20
MyClient(index) = new TCPClient(Host_IP(index), 5000, 4096, 90 ) 'Host_IP()内存放N个服务器端的IP地址
If MyClient.Connected Then
AddHandler MyClient(index).ClientClosed, AddressOf MyClient_ClientClosed
AddHandler MyClient(index).Exception, AddressOf MyClient_Exception
AddHandler MyClient(index).DataArrived, AddressOf MyClient_DataArrived
Debug.print("Connet Host:" &Host_IP(index))
Else
Debug.print("Disconnet Host:" &Host_IP(index))
End If
Next
Catch ex As Exception
Debug.Print(ex.message)
End Try
Private Sub MyClient_DataArrived(ByVal value() As Byte, ByVal Len As Integer)
Try
Dim myStr As String
myStr = System.Text.UTF8Encoding.UTF8.GetString(value)
Debug.Print(myStr)
Catch ex As Exception
Debug.Print(ex.message)
End Try
End Sub
Private Sub MyClient_ClientClosed()
'add your code here
End Sub
Private Sub MyClient_Exception(ByVal ex As System.Exception)
'add your code here
End Sub
End Class