我有一个用VB .Net构建的工具并使用Socket编程。如果只使用1个服务器,我的代码已经可以使用,但是当我想要用户2服务器并从1个服务器连接时,我的工具无法解决这个问题。
我想1个客户端可以连接到2个服务器
我的代码就像这样
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Pabertiyan
Public Class Form1
Dim Client As TcpClient
Dim Client2 As TcpClient
Dim sWrinteras As StreamWriter
'Dim NickFrefix As Integer = New Random().Next(1111, 9999)
Dim Pabertiyan As New Pabertiyan.Pabertiyan
Delegate Sub _xUpdate(ByVal str As String)
Sub xUpdate(ByVal str As String)
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), str)
Else
MsgBox("Sum of word in server is : " & str)
'TextBox1.AppendText(str & vbNewLine)
End If
End Sub
Sub Read(ByVal str As IAsyncResult)
Try
xUpdate(New StreamReader(Client.GetStream).ReadLine)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
Catch ex As Exception
xUpdate("Disconnect From Server")
Exit Sub
End Try
End Sub
Private Sub Send(ByVal str As String)
Try
sWrinteras = New StreamWriter(Client.GetStream)
sWrinteras.WriteLine(str)
sWrinteras.Flush()
Catch ex As Exception
MsgBox("You not Connect to Server")
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Connect" Then
Try
Client = New TcpClient(Pabertiyan.GetIPServer, Pabertiyan.GetPortServer)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
Button1.Text = "Disconnect"
TextBox3.BackColor = Color.Lime
Catch ex As Exception
MsgBox("Server is Offline. Please Try Again Later!!!")
End Try
Else
Client.Client.Close()
Client = Nothing
Button1.Text = "Connect"
TextBox3.BackColor = Color.Red
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Send(TextBox5.Text)
End Sub
Private Sub CheckConfig_Tick(sender As Object, e As EventArgs) Handles CheckConfig.Tick
If Pabertiyan.ReloadConfig = 1 Then
Button4.Enabled = True
TextBox6.Text = Pabertiyan.ReloadConfig
End If
If Pabertiyan.ReloadConfig = 0 Then
Button4.Enabled = False
TextBox6.Text = Pabertiyan.ReloadConfig
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox5.Text = Pabertiyan.SearchChar
TextBox2.Text = Pabertiyan.TotalServer
TextBox7.Text = Pabertiyan.ModuleLogs
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox7.Text = Pabertiyan.ReloadConfig
TextBox5.Text = Pabertiyan.SearchChar
End Sub
End Class
任何人都可以帮我解决问题吗?