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