我正在用 VB.NET 编程。
我想将一个字符串或整数从一个 VB.NET 应用程序发送到不同计算机上的另一个 VB.NET 应用程序。
我查看了一些教程,但所有教程都只能在本地网络上运行,我希望它可以在 Internet 上运行。
这是我的本地连接代码:
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
是我要发送的字符串。
txtName.Text
只是发件人的名字
cmbAddress.text
是远程计算机的IP 地址
如何在 VB.NET 中将数据发送到另一台远程计算机?