0

I am trying to use a tcp connection to send my files over from one client to another client. How do I go about doing that? I open a file dialog and after selecting the file, I want to it to send it to a connected client via tcp connection. At the other end, I want it to prompt a dialog when they detect that a file is being sent and the user can choose to receive the file anot.

This is my connection code:

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

This is my event handler when my file is selected:

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
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
山本一樹
  • 157
  • 1
  • 3
  • 15
  • Since you are using the `Socket` class, you might want to check out [this msdn page.](https://msdn.microsoft.com/en-us/library/sx0a40c2(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1) – Zohar Peled Aug 01 '15 at 12:45
  • Zohar : Send file doesn't do anything unless the receive end knows what to do with file. The solution is not that simple. Using FTP which is an application that uses tcp as the transport layer is one way of going. There are 1000 other ways. To send a file first you have to have an application layer where the client tells the server that a file is being uploaded. Second the end of file must be determined by server. One of 3 methods 1) Terminate file with known character. 2) Send byte size at beginning of message (file upload). 3) Send fix size message which doesn't work well with files – jdweng Aug 01 '15 at 14:51
  • I did try to use that Socket.Sendfile method but I do not know how to code the receiving end. – 山本一樹 Aug 02 '15 at 06:07

0 Answers0