vb.net如何通过tcp连接发送文件

时间:2015-08-01 10:55:18

标签: vb.net tcp sendfile

我正在尝试使用tcp连接将我的文件从一个客户端发送到另一个客户端。我该怎么做呢?我打开一个文件对话框,在选择文件后,我希望它通过tcp连接将其发送到连接的客户端。另一方面,我希望它在检测到正在发送文件时提示对话框,并且用户可以选择接收文件anot。

这是我的连接代码:

Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click

    Try
        If (cmdStart.Text = "Start") Then
            'Current Status is IDLE
            If (optTCPConnection.Checked = True) Then
                'TCP Selected
                ChatSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 'Create TCP socket
                ChatSocket.DontFragment = True
                ChatSocket.ExclusiveAddressUse = False
                ChatSocket.ReceiveBufferSize = 1024
                ChatSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
                ChatSocket.Bind(New IPEndPoint(IPAddress.Parse(txtLocalIP.Text), CInt(Val(txtLocalPort.Text)))) 'Bind socket to desired local adaptor
                ChatSocket.Blocking = False
                If (chkServer.Checked = True) Then
                    'I am a SERVER will listen for connection
                    ChatSocket.Listen(10)
                    cmdStart.Text = "Stop"
                    lblStatus.Text = "Status: Listening"
                    pbximage.BackColor = Color.GreenYellow
                Else
                    'I am a CLIENT will try to connect to server
                    cmdStart.Text = "Stop"
                    lblStatus.Text = "Status: Connecting"
                    pbximage.BackColor = Color.GreenYellow


                End If
            Else
                'UDP Selected
                ChatSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 'Create UDP socket
                ChatSocket.DontFragment = True
                ChatSocket.ExclusiveAddressUse = False
                ChatSocket.ReceiveBufferSize = 1024
                ChatSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
                ChatSocket.Bind(New IPEndPoint(IPAddress.Parse(txtLocalIP.Text), CInt(Val(txtLocalPort.Text)))) 'Bind socket to desired local adaptor
                ChatSocket.Blocking = False
                cmdStart.Text = "Stop"
                lblStatus.Text = "Status: Receiving"
                txtSend.Enabled = True
                pbximage.BackColor = Color.GreenYellow

            End If
            grpConnection.Enabled = False
            tmrTimer.Start()
        Else
            'Current Status is Connected
            grpConnection.Enabled = True
            tmrTimer.Stop()
            txtChat.Clear()
            txtSend.Clear()
            txtSend.Enabled = False
            If (ChatSocket IsNot Nothing) Then
                If (ChatSocket.Connected = True) Then ChatSocket.Disconnect(False) 'Disconnect if connected (TCP only)
                ChatSocket.Close() 'Close the port permanently
                ChatSocket = Nothing
                cmdStart.Text = "Start"
                lblStatus.Text = "Status: IDLE"
                pbximage.BackColor = Color.LightBlue
            End If
        End If
    Catch ex As Exception
        MsgBox("An error has occurred, please ensure IP addresses and port number are valid.", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "TCP/UDP Chat Window")
    End Try
End Sub

选择我的文件时,这是我的事件处理程序:

Private Sub openfile_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles openfile.FileOk
    If (MsgBox("Are you sure you want to send this file?", MsgBoxStyle.YesNo, "Confirm")) = MsgBoxResult.Yes Then

    End If
End Sub

0 个答案:

没有答案