Results 1 to 4 of 4

Thread: Sending a string from one computer to another.

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    7

    Sending a string from one computer to another.

    Does anyone have a super simplistic example of sending a string from a vb.net app on one computer to a vb.net app on another computer.

    Most of the examples I'm finding are examples of chat programs, etc.

    App1 would have a relatively small amount of data, like a temperature, or the status of a switch, and then send that data (basically strings of less then 10 characters) to app2, running on another computer.

    I think there used to be a really simple project in vb6. But when .NET came, the examples became much more complex.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,738

    Re: Sending a string from one computer to another.

    Chat programs send text so what's the problem? Just ignore the parts you don't need. What exactly don't you understand?

  3. #3
    Frenzied Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    1,235

    Re: Sending a string from one computer to another.

    Exactly. My chat program here uses Dropbox, this is useful if the remote computer is really remote.

    https://www.vbforums.com/showthread....=1#post5531479

    Both machines need to be running Dropbox of course. Each has access to the same shared area and one copies the data in the form of a file to the shared folder. The other picks it up.

    It couldn't be more simple as you are just doing ordinary file i/o.

    Dropbox is only required if the two machines are not on the same local network. If they are both local then the whole process is even more simple. Share a folder, write and read the data as a file.
    By the power invested in me, all the threads I start are Niya and Olaf free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,404

    Re: Sending a string from one computer to another.

    The documentation on the Send and Receive methods of the UDPClient class give a pretty simple example of sending and receiving a text string. https://docs.microsoft.com/en-us/dot...tframework-4.6

    To do a quick test, I opened two instances of Visual Studio, and in one I created a WinForm project called SimpleUDPSend or something like that, and in the other I created a winform project name SimpleUDPReceive.

    In the Send project form, I added a single line textbox and a button.
    In the Receive project form I added a label.

    The code in the Send project (adapted from the Microsoft example given above) :
    Code:
    Imports System.Net          'IPEndPoint
    Imports System.Net.Sockets  'UDPClient
    Imports System.Text
    
    Public Class Form1
      Private udpClient As New UdpClient("127.0.0.1", 11000)
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim sendBytes As Byte() = Encoding.ASCII.GetBytes(TextBox1.Text)
        Try
          udpClient.Send(sendBytes, sendBytes.Length)
        Catch ex As Exception
          MessageBox.Show(ex.ToString)
        End Try
      End Sub
    End Class
    The code in the receiver project, also adapted from the Microsoft example.
    Code:
    Imports System.Net          'IPEndPoint
    Imports System.Net.Sockets  'UDPClient
    Imports System.Threading    'Thread
    Imports System.Text
    
    Public Class Form1
      Private UDP_Thread As New Thread(AddressOf UDP_Receive_Thread)
      Private Running As Boolean = True
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        UDP_Thread.IsBackground = True
        UDP_Thread.Start()
      End Sub
    
      Private Sub UDP_Receive_Thread()
        Dim receivingUDpClient As New UdpClient(11000)
        Dim RemoteIPEndPoint As New IPEndPoint(IPAddress.Any, 0)
    
        While Running
          Try
            Dim receiveBytes As Byte() = receivingUDpClient.Receive(RemoteIPEndPoint)
            Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
            Me.Invoke(Sub() Label1.Text = returnData)
          Catch ex As Exception
            MessageBox.Show(ex.ToString)
          End Try
        End While
    
      End Sub
    End Class
    Start the sender and the receiver from the IDEs, type something in the textbox and hit the button, and you should see it show up in the label.

    If you run the receiver on another computer, just change the 127.0.0.1 to the IP address of the receiving computer and the example should work between machines.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width