通过IpV6发送文件

时间:2017-04-01 13:11:56

标签: vb.net sockets networking tcp ipv6

我想通过tcp连接在互联网上发送文件。我的代码很好地为IpV4处理了这个问题(这里的信用转到http://technotif.com/creating-simple-tcpip-server-client-transfer-data-using-c-vb-net/,我只是改变了一些小问题来纠正文件输出) 我试着和我的一个朋友一起使用它,但是他的路由器是无用的垃圾,它无法向任何端口转发,甚至不能使用upnp。它也设置为IpV6,据我所知,IPv6不再需要端口转发,因为每个设备都有自己的公共IP。

遗憾的是我的程序无法使用IPv6地址,我很难找到有关此主题的任何信息。

这是我的代码:

Public Class Form1
Private nSockets As ArrayList


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim IPHost As IPHostEntry
    IPHost = Dns.GetHostByName(Dns.GetHostName())
    lblStatus.Text = "My IP address is " +
 IPHost.AddressList(0).ToString()
    nSockets = New ArrayList()
    Dim thdListener As New Thread(New ThreadStart _
    (AddressOf listenerThread))
    thdListener.Start()
End Sub



Public Sub listenerThread()

    Control.CheckForIllegalCrossThreadCalls = False
    Dim tcpListener As New TcpListener(7080)
    Dim handlerSocket As Socket
    Dim thdstHandler As ThreadStart
        Dim thdHandler As Thread
        tcpListener.Start()
        Do
            handlerSocket = tcpListener.AcceptSocket()
            If handlerSocket.Connected Then
                lbConnections.Items.Add(
                handlerSocket.RemoteEndPoint.ToString() +
                "connected.")
                SyncLock (Me)
                    nSockets.Add(handlerSocket)
                End SyncLock
                thdstHandler = New ThreadStart(AddressOf _
                handlerThread)
                thdHandler = New Thread(thdstHandler)
                thdHandler.Start()
            End If
        Loop
End Sub

Public Sub handlerThread()

        Dim handlerSocket As Socket
        handlerSocket = nSockets(nSockets.Count - 1)
    Dim networkStream As NetworkStream = New _
NetworkStream(handlerSocket)
    Dim blockSize As Int16 = 16
        Dim thisRead As Int16
        Dim dataByte(blockSize) As Byte
        SyncLock Me
            ' Only one process can access the
            ' same file at any given time
            Dim fileStream As Stream
            fileStream = File.OpenWrite("C:\Whatever.file")
            While (True)
                thisRead = networkStream.Read(dataByte,
0, dataByte.Length)
                fileStream.Write(dataByte, 0, thisRead)
                If thisRead = 0 Then Exit While
            End While
            fileStream.Close()
            networkStream.Close()
        End SyncLock
        lbConnections.Items.Add("File Written")
    handlerSocket = Nothing
End Sub

如何使其具备IPv6功能?

忘记加入我的客户,我需要在这里更改才能使其正常工作?因为即使对我的服务器进行了更改,它仍然无法正常连接。

Private Sub Sendfile()
    Dim filebuffer As Byte()
    Dim fileStream As Stream
    fileStream = File.OpenRead(tbFilename.Text)
    ' Alocate memory space for the file
    ReDim filebuffer(fileStream.Length)
    fileStream.Read(filebuffer, 0, fileStream.Length)
    ' Open a TCP/IP Connection and send the data
    Dim clientSocket As New TcpClient(tbServer.Text, 7080)
    Dim networkStream As NetworkStream
    networkStream = clientSocket.GetStream()
    networkStream.Write(filebuffer, 0, fileStream.Length)
    networkStream.Close()
End Sub

1 个答案:

答案 0 :(得分:1)

您的侦听器当前正在侦听IPv4地址0.0.0.0,这是您只为侦听器指定端口时的默认设置。

您需要使用TcpListener(IPAddress, Integer) overload并指定IPv6Any来收听IPv6地址。

Dim tcpListener As New TcpListener(IPAddress.IPv6Any, 7080)

作为旁注,您应该使用List(Of T)而不是ArrayList。后者是无类型的,并不像以前那样针对.NET进行优化。