如何使用 vb.net 从一台计算机向另一台计算机发送消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7196262/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:40:40  来源:igfitidea点击:

How do i send messages from one computer to another using vb.net?

vb.netnetworkingbluetoothmessage

提问by daniel11

Ok so i've been wanting to do this for a few years now and it amazes me that no one can come up with a solution... i want to write a program (preferably in vb.net) that can send a message to another computer (or device) that is connected to the same network as me. I want the message to appear on the other computer (or device) in a popup message box. Also, it would be ideal if this could be done through some sort of bluetooth connection, if not then local ip connection will do. I don't know how i would go about doing this but i know it's possible because i've seen some programs do it... in fact i've seen a program called blackrain that without any additional software installed, can display messages on an ipod touch screen to instruct the yser what to do, and then display the results from their input on the computer screen, almost instantly. I would like to know how that works as well if anyone has any thoughts please feel free to share them!

好的,所以我几年来一直想这样做,令我惊讶的是没有人能想出解决方案......我想编写一个程序(最好在 vb.net 中),可以将消息发送到与我连接到同一网络的另一台计算机(或设备)。我希望消息出现在另一台计算机(或设备)上的弹出消息框中。此外,如果这可以通过某种蓝牙连接来完成,那将是理想的,如果不是,那么本地 ip 连接就可以了。我不知道我会怎么做,但我知道这是可能的,因为我见过一些程序这样做......事实上我见过一个名为 blackrain 的程序,它没有安装任何额外的软件,可以在上面显示消息一个 ipod 触摸屏来指示用户做什么,然后在计算机屏幕上显示他们输入的结果,几乎是瞬间。我想知道它是如何工作的,如果有人有任何想法,请随时分享!

Additional Details:

额外细节:

  • I have alot of experience with vb.net, command-line functions, and vbscript.

  • I am currently running Windows 7 Professional x64

  • I have an external bluetooth mini adapter.

  • I would like this to be (if possible) similar to those ipod/iphone apps that let you control your laptop cursor over wifi sync; in the sense that there is no setup required, and no additional software needed. (Example: remotepad.ipa)

  • 我在 vb.net、命令行函数和 vbscript 方面有很多经验。

  • 我目前运行的是 Windows 7 Professional x64

  • 我有一个外部蓝牙迷你适配器。

  • 我希望它(如果可能)类似于那些让您通过 wifi 同步控制笔记本电脑光标的 ipod/iphone 应用程序;从某种意义上说,不需要设置,也不需要额外的软件。(例如:remotepad.ipa)

The code for the message box would be something like:

消息框的代码类似于:

ObjClient = New TcpClient("127.0.0.1", 1000)
TcpClient.Start()
Messagebox.Show("Popup Message Here")
TcpClient.Close()

I know that this code will do sort of the same thing in the command prompt:

我知道这段代码会在命令提示符下做同样的事情:

msg * /SERVER:localhost hello 

or this code will do the same thing in the command prompt:

或者此代码将在命令提示符下执行相同的操作:

msg * hello > localhost

But i want to do this without any batch files if possible, because i don't want to have to set up anything on the other end.

但如果可能的话,我想在没有任何批处理文件的情况下执行此操作,因为我不想在另一端设置任何内容。

Thanks!

谢谢!

Does it have anything to do with Sockets or Ports maybe?

它可能与套接字或端口有关吗?



采纳答案by Zhais

Using TcpClient and related libraries is definitely the correct answer.

使用 TcpClient 和相关库绝对是正确的答案。

Sample code for writing data to a specific IP/port:

将数据写入特定 IP/端口的示例代码:

''' <summary>
''' Send data over TCP/IP network
''' </summary>
''' <param name="data">Data string to write</param>
''' <param name="IP">The connected destination TcpClient</param>
Public Sub WriteData(ByVal data As String, ByRef IP As String)
    Console.WriteLine("Sending message """ & data & """ to " & IP)
    Dim client As TcpClient = New TcpClient()
    client.Connect(New IPEndPoint(IPAddress.Parse(IP), My.Settings.CommPort))
    Dim stream As NetworkStream = client.GetStream()
    Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(data)
    stream.Write(sendBytes, 0, sendBytes.Length)
End Sub

Use TcpListener for watching for incoming data.

使用 TcpListener 监视传入数据。

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.aspx

edit: For knowing what IP to send it to... You could either have a list of internal IPs to connect to, or have each networked computer 'subscribe' to your program if it's hosted statically on a box. For my purposes, when I'm using this code, the host process sits on a known server. Client processes that want to receive messages sends a quick message to the host, which will then record the IP to be able to send to later.

编辑:为了知道要将其发送到哪个 IP... 您可以拥有要连接的内部 IP 列表,或者如果您的程序静态托管在盒子上,则可以让每台联网计算机“订阅”您的程序。出于我的目的,当我使用此代码时,主机进程位于已知服务器上。想要接收消息的客户端进程向主机发送一条快速消息,然后主机将记录 IP 以便稍后发送。

Obtaining the IP of a requesting client:

获取请求客户端的IP:

''Given variable m_listener is an active TcpListener...
Dim client As TcpClient = m_listener.AcceptTcpClient()
Dim requesterIP As String = client.Client.RemoteEndPoint.ToString().Split(New Char() {":"})(0)