daniel11 daniel11 - 4 months ago 26
Vb.net Question

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

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!

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)



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?



Answer

Using TcpClient and related libraries is definitely the correct answer.

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

''' <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.

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.

Obtaining the IP of a requesting client:

''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)
Comments