below is the client and server coding.
Client :
Expand|Select|Wrap|Line Numbers
- Imports System.Net.Sockets
- Imports System.Text
- Imports System.IO
- Imports System.Net
- Public Class frmClient
- Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
- Try
- Dim udpClient As UdpClient = New UdpClient(txtIP.Text, Convert.ToInt32(txtPort.Text))
- Dim fStream As FileStream
- Dim sendPacket() As Byte
- fStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
- ReDim sendPacket(fStream.Length)
- fStream.Read(sendPacket, 0, fStream.Length)
- Dim str As String = OpenFileDialog1.SafeFileName + " (" + fStream.Length.ToString + " bytes). Do you agree?"
- udpClient.Send(Encoding.UTF8.GetBytes(str), str.Length)
- Dim receivedPacket(5000) As Byte
- udpClient.Client.ReceiveTimeout = 6000
- receivedPacket = udpClient.Receive(Nothing)
- If Encoding.UTF8.GetString(receivedPacket) = "YES" Then
- udpClient.Send(sendPacket, sendPacket.Length)
- udpClient.Client.ReceiveTimeout = 1000
- receivedPacket = udpClient.Receive(Nothing)
- If Encoding.UTF8.GetString(receivedPacket) = "SUCCESS" Then
- MsgBox("File transfer success.")
- Else
- MsgBox("File transfer fail.")
- End If
- Else
- MsgBox("Server rejects file transfer request.")
- End If
- fStream.Close()
- udpClient.Close()
- Catch ex As Exception
- Console.WriteLine(ex.Message)
- End Try
- End Sub
- Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
- Dim result As DialogResult = OpenFileDialog1.ShowDialog()
- If result = Windows.Forms.DialogResult.OK Then
- txtFile.Text = OpenFileDialog1.FileName
- End If
- End Sub
- Private Sub frmClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- End Class
Server:
Expand|Select|Wrap|Line Numbers
- Imports System.Net.Sockets
- Imports System.Text
- Imports System.IO
- Imports System.Net
- Public Class frmServer
- Private Sub btnListen_Click(sender As Object, e As EventArgs) Handles btnListen.Click
- Dim udpServer As UdpClient = New UdpClient(Convert.ToInt32(txtPort.Text))
- Dim remoteEP As IPEndPoint = New IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPort.Text))
- Try
- Dim receivedPacket(5000) As Byte
- btnListen.Enabled = False
- txtPort.ReadOnly = True
- Me.Text = "Server - Listening"
- udpServer.Client.ReceiveTimeout = 3000
- receivedPacket = udpServer.Receive(remoteEP)
- Dim res As MsgBoxResult = MsgBox(remoteEP.ToString +
- " requests to transfer " +
- Encoding.UTF8.GetString(receivedPacket), MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Message")
- If res = MsgBoxResult.Yes Then
- udpServer.Send(Encoding.UTF8.GetBytes("YES"), 3, remoteEP)
- receivedPacket = udpServer.Receive(remoteEP)
- Dim res2 As DialogResult = SaveFileDialog1.ShowDialog()
- If res2 = Windows.Forms.DialogResult.OK Then
- Dim fStream As FileStream = New FileStream(SaveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
- fStream.Write(receivedPacket, 0, receivedPacket.Length)
- fStream.Close()
- udpServer.Send(Encoding.UTF8.GetBytes("SUCCESS"), 7, remoteEP)
- Else
- udpServer.Send(Encoding.UTF8.GetBytes("FAIL"), 4, remoteEP)
- End If
- ElseIf res = MsgBoxResult.No Then
- udpServer.Send(Encoding.UTF8.GetBytes("NO"), 2, remoteEP)
- End If
- udpServer.Close()
- btnListen.Enabled = True
- txtPort.ReadOnly = False
- Me.Text = "Server"
- Catch ex As Exception
- Console.WriteLine(ex.Message)
- udpServer.Close()
- btnListen.Enabled = True
- txtPort.ReadOnly = False
- Me.Text = "Server"
- End Try
- End Sub
- Private Sub frmServer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- End Sub
- End Class