468,117 Members | 1,342 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

sending large files using UDP in Visual Basic

1 Bit
Hye, I have been task to creat a UDP file transfer using Visual Basic 2019.. i can transfer small files below 64kbytes but the requirement is bigger than that.. How to modify this code for it to be able to transfer larger files atleast 1Mbytes.

below is the client and server coding.

Client :
Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Sockets
  2. Imports System.Text
  3. Imports System.IO
  4. Imports System.Net
  5.  
  6. Public Class frmClient
  7.     Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
  8.  
  9.         Try
  10.             Dim udpClient As UdpClient = New UdpClient(txtIP.Text, Convert.ToInt32(txtPort.Text))
  11.             Dim fStream As FileStream
  12.  
  13.             Dim sendPacket() As Byte
  14.             fStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
  15.             ReDim sendPacket(fStream.Length)
  16.             fStream.Read(sendPacket, 0, fStream.Length)
  17.  
  18.             Dim str As String = OpenFileDialog1.SafeFileName + " (" + fStream.Length.ToString + " bytes). Do you agree?"
  19.             udpClient.Send(Encoding.UTF8.GetBytes(str), str.Length)
  20.  
  21.             Dim receivedPacket(5000) As Byte
  22.  
  23.             udpClient.Client.ReceiveTimeout = 6000
  24.             receivedPacket = udpClient.Receive(Nothing)
  25.             If Encoding.UTF8.GetString(receivedPacket) = "YES" Then
  26.                 udpClient.Send(sendPacket, sendPacket.Length)
  27.                 udpClient.Client.ReceiveTimeout = 1000
  28.                 receivedPacket = udpClient.Receive(Nothing)
  29.                 If Encoding.UTF8.GetString(receivedPacket) = "SUCCESS" Then
  30.                     MsgBox("File transfer success.")
  31.                 Else
  32.                     MsgBox("File transfer fail.")
  33.                 End If
  34.             Else
  35.                 MsgBox("Server rejects file transfer request.")
  36.             End If
  37.  
  38.             fStream.Close()
  39.             udpClient.Close()
  40.  
  41.         Catch ex As Exception
  42.             Console.WriteLine(ex.Message)
  43.         End Try
  44.     End Sub
  45.  
  46.     Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
  47.         Dim result As DialogResult = OpenFileDialog1.ShowDialog()
  48.  
  49.         If result = Windows.Forms.DialogResult.OK Then
  50.             txtFile.Text = OpenFileDialog1.FileName
  51.         End If
  52.  
  53.     End Sub
  54.  
  55.     Private Sub frmClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  56.  
  57.     End Sub
  58.  
  59. End Class

Server:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Net.Sockets
  2. Imports System.Text
  3. Imports System.IO
  4. Imports System.Net
  5.  
  6. Public Class frmServer
  7.     Private Sub btnListen_Click(sender As Object, e As EventArgs) Handles btnListen.Click
  8.  
  9.         Dim udpServer As UdpClient = New UdpClient(Convert.ToInt32(txtPort.Text))
  10.         Dim remoteEP As IPEndPoint = New IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPort.Text))
  11.         Try
  12.             Dim receivedPacket(5000) As Byte
  13.  
  14.             btnListen.Enabled = False
  15.             txtPort.ReadOnly = True
  16.             Me.Text = "Server - Listening"
  17.  
  18.             udpServer.Client.ReceiveTimeout = 3000
  19.             receivedPacket = udpServer.Receive(remoteEP)
  20.  
  21.             Dim res As MsgBoxResult = MsgBox(remoteEP.ToString +
  22.                                                 " requests to transfer " +
  23.                                                 Encoding.UTF8.GetString(receivedPacket), MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Message")
  24.             If res = MsgBoxResult.Yes Then
  25.                 udpServer.Send(Encoding.UTF8.GetBytes("YES"), 3, remoteEP)
  26.                 receivedPacket = udpServer.Receive(remoteEP)
  27.                 Dim res2 As DialogResult = SaveFileDialog1.ShowDialog()
  28.                 If res2 = Windows.Forms.DialogResult.OK Then
  29.                     Dim fStream As FileStream = New FileStream(SaveFileDialog1.FileName, FileMode.Create, FileAccess.Write)
  30.                     fStream.Write(receivedPacket, 0, receivedPacket.Length)
  31.                     fStream.Close()
  32.                     udpServer.Send(Encoding.UTF8.GetBytes("SUCCESS"), 7, remoteEP)
  33.                 Else
  34.                     udpServer.Send(Encoding.UTF8.GetBytes("FAIL"), 4, remoteEP)
  35.                 End If
  36.             ElseIf res = MsgBoxResult.No Then
  37.                 udpServer.Send(Encoding.UTF8.GetBytes("NO"), 2, remoteEP)
  38.             End If
  39.  
  40.             udpServer.Close()
  41.             btnListen.Enabled = True
  42.             txtPort.ReadOnly = False
  43.             Me.Text = "Server"
  44.  
  45.         Catch ex As Exception
  46.             Console.WriteLine(ex.Message)
  47.             udpServer.Close()
  48.             btnListen.Enabled = True
  49.             txtPort.ReadOnly = False
  50.             Me.Text = "Server"
  51.         End Try
  52.  
  53.     End Sub
  54.  
  55.     Private Sub frmServer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  56.  
  57.     End Sub
  58. End Class
Feb 28 '21 #1
0 1395

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using this site, you agree to our Privacy Policy and Terms of Use.