Reputation: 709
I am programming in VB.NET.
I would like to sent a String or an Integer from a VB.NET application to another VB.NET application on different computers.
I looked at some tutorials, 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 a 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?
Upvotes: 4
Views: 14411
Reputation: 1857
What you are talking about is creating a client-server application. There are a few different ways you can do this.
The easiest way would be to have your programs talk to a web application or web service. Basically you will create a site that your programs will connect to and send data, or have it check for data on a scheduled interval. For this you would need to use some sort of database to hold the updates until the client requests them.
The second option is way more complex and utilizes socket connections. You will basically use sockets to connect to the program running on a certain port on the remote machine. Your program will need to have a send class to send the data as well as a listener class to wait for incoming connections. You will also have to keep in mind that you will need to opens up the incoming ports on both local firewall. Due to firewall issues, and the complexity of setting up socket connections, this is a far more advanced option.
Upvotes: 5