使用TCP / IP在不同网络上的计算机之间发送数据 [英] Send Data Between Computers on Different Networks with TCP/IP

查看:96
本文介绍了使用TCP / IP在不同网络上的计算机之间发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想将数据(例如小字符串,例如字符串)从一台计算机发送到另一台计算机。
请注意,这些计算机必须位于不同的网络上。 即。一个在我家,另一个在几英里外的另一个家。我能够在同一个互联网网络上的计算机之间进行通信,但不能通过不同的
网络进行通信。我的代码是:

I want to send data (small, like a string, for example) from one computer to another. Note that these computers have to be on different networks. I.e. one is at my house, the other is at another fellow's house a couple miles away. I have been able to communicate between computers on the same internet network, but not over different networks. My code is:

服务器:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1

    Dim _server As TcpListener
    Dim _listOfClients As New List(Of TcpClient)



    Private Sub NewClient(state As Object)
        Dim client As TcpClient = _server.AcceptTcpClient()
        Try
            _listOfClients.Add(client)
            Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

            While True
                Dim ns As NetworkStream = client.GetStream()

                Dim toReceive(100000) As Byte
                ns.Read(toReceive, 0, toReceive.Length)
                Dim txt As String = Encoding.ASCII.GetString(toReceive)

                For Each c As TcpClient In _listOfClients
                    If c IsNot client Then
                        Dim nns As NetworkStream = c.GetStream()
                        nns.Write(Encoding.ASCII.GetBytes(txt), 0, txt.Length)
                    End If
                Next
            End While

        Catch ex As Exception
            If _listOfClients.Contains(client) Then
                _listOfClients.Remove(client)
            End If
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim ip As String = TextBox1.Text
            Dim port As Integer = 49155 '5432

            _server = New TcpListener(IPAddress.Parse(ip), port)
            _server.Start()

            Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

客户端:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1

    Dim _client As TcpClient

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim ip As String = TextBox2.Text
            Dim port As Integer = 49155 '5432

            _client = New TcpClient(ip, port)

            CheckForIllegalCrossThreadCalls = False

            Threading.ThreadPool.QueueUserWorkItem(AddressOf ReceiveMessages)

            AcceptButton = Button1
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ReceiveMessages(state As Object)
        Try
            While True

                Dim ns As NetworkStream = _client.GetStream()

                Dim toReceive(100000) As Byte
                ns.Read(toReceive, 0, toReceive.Length)
                Dim txt As String = Encoding.ASCII.GetString(toReceive)

                If RichTextBox1.TextLength > 0 Then
                    RichTextBox1.Text &= vbNewLine & txt
                Else
                    RichTextBox1.Text = txt
                End If
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim ns As NetworkStream = _client.GetStream()

            ns.Write(Encoding.ASCII.GetBytes(TextBox1.Text), 0, TextBox1.Text.Length)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

关于我如何做的任何想法这个?感谢您的帮助,祝您度过愉快的一天!

Any ideas on how I could do this? Thank you for the help, and have a great day!

推荐答案

Wilkyrl,

Wilkyrl,

从计算机发送数据的时间是从两台计算机之间放置电缆的时间。 

Sending data with computers is from the time you could put a cable between 2 computers. 

使用互联网时,它总是从某个地方接收数据。  或将数据放在您有权限(共享)的地方。然后它就像一个驱动器,虽然你必须使用UNC路径代码。 

With Internet it is always receiving data from a certain place.  Or putting data on a place you have got authority (shared) to. And then it is just as a drive although you have to use an UNC path code. 


这篇关于使用TCP / IP在不同网络上的计算机之间发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆