VB.Net Socket编程中如何访问多个客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18914353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access multiple client in VB.Net Socket Programming
提问by Ravi
hey i creating a lan based application in vb.net,I'm already created a server and a client and make a release build of both programmer.
嘿,我在 vb.net 中创建了一个基于 LAN 的应用程序,我已经创建了一个服务器和一个客户端,并为这两个程序员制作了一个发布版本。
Server code
服务器代码
Imports System.Net
Imports System.Net.Sockets
Module Module1
Dim isActive As Boolean
Sub Main()
Dim port As Integer 'this is port number
Dim localAddr As IPAddress 'Ip Address
Dim server As TcpListener ' for use tcp/ip protocol
Dim client As TcpClient
Dim aString As String
Dim negotiateStream As Security.NegotiateStream = Nothing
Dim d As DictionaryEntry
Try
port = 8888
localAddr = IPAddress.Loopback 'return the ipaddress of self
'creating the server
server = New TcpListener(localAddr, port)
server.Start()
Console.WriteLine("Server ready")
'this is a block method, it will wait for new connection
'before continuing
client = server.AcceptTcpClient
Console.WriteLine("Connected to " & IPAddress.Parse(CType(client.Client.LocalEndPoint, IPEndPoint).Address.ToString).ToString)
Console.WriteLine("Connected to " & client.ToString)
Do
Console.Write(" Ready to quit? (y/n)")
aString = Console.ReadLine()
If aString <> "y" Then
'do something
End If
Loop Until aString = "y" ' Or aString = "n"
Catch e As System.Exception
Console.WriteLine("Can't create the server : " + e.Message)
End Try
End Sub
End Module
Client code
客户端代码
Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener
Imports System.Text
Public Class Form1
Dim aString As String
Dim port As Integer 'this is the port number
Dim localAddr As IPAddress ' this is the IP address
Dim client As TcpClient ' This is for the TCP/IP Protocol
Dim networkStream As NetworkStream 'use to send/recieve data
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ConnectToServer(localAddr, port)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "Press button to connect with the server"
port = 8888
localAddr = IPAddress.Loopback
Button2.Enabled = False
End Sub
Private Sub ConnectToServer(ByRef ipaddr, ByRef port)
Try
client = New TcpClient(localAddr.ToString, port)
TextBox1.Text = "Connencted to : " & IPAddress.Parse(CType(client.Client.LocalEndPoint, IPEndPoint).Address.ToString).ToString & " "
networkStream = client.GetStream()
Button1.Enabled = False
Button2.Enabled = True
Catch e As Exception
MsgBox(e.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("Message from Client$")
networkStream.Write(outStream, 0, outStream.Length)
networkStream.Flush()
End Sub
End Class
now when i start the server and connect the client1 to the server a message is print on console of server like that "connect to client _" but now it open another client(client2) and then connect it to the server no new message is show for the client2.
现在,当我启动服务器并将客户端 1 连接到服务器时,会在服务器控制台上打印一条消息,例如“连接到客户端_”,但现在它打开另一个客户端(客户端 2),然后将其连接到服务器,没有显示新消息为客户2。
采纳答案by Steve
Because
因为
client = server.AcceptTcpClient
is a waiting for a connection and after it gets 1, it does not wait for another.
是等待连接,在它获得 1 后,它不会等待另一个。
To wait for more, you could put this inside of a loop but you open a whole can of worms. You now need to track connections (maybe in an array or something) and then keep the loop going so you can always be waiting for more connections and processing messages from other connections. But, because you cannot wait, and keep the loop going, you need to make this multi-threaded.
为了等待更多,你可以把它放在一个循环中,但你打开一整罐蠕虫。您现在需要跟踪连接(可能在数组或其他东西中),然后保持循环运行,这样您就可以始终等待更多连接并处理来自其他连接的消息。但是,因为您不能等待并保持循环运行,所以您需要使这个多线程。
Looks like your simple app just became complicated but at least I hope I gave you some ideas to plan this out a little further.
看起来你的简单应用程序变得复杂了,但至少我希望我给了你一些想法来进一步规划这个。

