How to communicate between two VB.NET applications over a network

I am programming in VB.NET.

I would like to send a String or Integer from a VB.NET application to another VB.NET application on different computers.

I looked through some lessons, but all the tutorials work only on the local network, and I want it to work over the Internet.

This is my code for local connections:

Dim Listener As New TcpListener(34349) Dim Client As New TcpClient Dim Message As String = "" Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Timer1.Tick If Listener.Pending = True Then Message = "" Client = Listener.AcceptTcpClient() Dim Reader As New StreamReader(Client.GetStream()) While Reader.Peek > -1 Message = Message + Convert.ToChar(Reader.Read()).ToString End While RichTextBox1.ForeColor = Color.Black RichTextBox1.Text += Message + vbCrLf End If End Sub Private Sub btnSend_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnsend.Click If txtName.Text = "" Or cmbAddress.Text = "" Then MessageBox.Show("All Fields must be Filled", _ "Error Sending Message", _ MessageBoxButtons.OK, _ MessageBoxIcon.Error) Else Try Client = New TcpClient(cmbAddress.Text, 34349) Dim Writer As New StreamWriter(Client.GetStream()) Writer.Write(txtName.Text & " Says: " & txtmessage.Text) Writer.Flush() RichTextBox1.Text += (txtName.Text & " Says: " & txtmessage.Text) + vbCrLf txtmessage.Text = "" Catch ex As Exception Console.WriteLine(ex) Dim Errorresult As String = ex.Message MessageBox.Show(Errorresult & vbCrLf & vbCrLf & "Please Review Client Address", "Error Sending Message", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub 

txtmessage.text is the string I want to send.

txtName.Text is just the name of the sender

cmbAddress.text is the IP address of the remote computer

How can I send data to another remote computer in VB.NET?

+4
source share
1 answer

It is about creating a client-server application. There are several ways to do this.

The easiest way is to talk to your programs with a web application or web service. Basically, you will create a site to which your programs will connect and send data, or check the data for the planned interval. To do this, you will need to use some database to store updates until the client requests them.

The second option is more complex and uses socket connections. Basically, you will use sockets to connect to a program running on a specific port on a remote computer. Your program will need to have a send class to send data, as well as a listener class to wait for incoming connections. You should also keep in mind that you will need to open the incoming ports on the local firewall. Due to problems with the firewall and the difficulty of configuring socket connections, this is a much more advanced option.

+2
source

Source: https://habr.com/ru/post/1501287/


All Articles