473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2638

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

Similar topics

1
by: Oren nizri | last post by:
i need to create new microsoft outlook mail profile by using visual basic...
2
by: Pankajmani das | last post by:
Dear sir I am trying to open a word file using Visual Basic Commad. But I am failed. So, please guid me to open a perticular word or another file. Note: I know how to open a file using common...
1
by: Danakmj | last post by:
I developed a program using visual basic 6.0. I want to create a setup program that has an agreement page (i.e. it has two buttons ,I agree and I donít agree buttons, so that if a user does not agree...
3
by: sena0112 | last post by:
Hi, I'm a beginner in visual basic and programming. My programme needs to let the user browse for a text file and when the user press a command button, the programme will list out the text file into...
1
by: Edward1 | last post by:
Hi! I am a newbie to programming, I am tryin to build a database application using visual basic .net and Ms Access, right now I am unable to update the Ms Access Database from visual basic. can...
6
by: ashokbio | last post by:
I want to create an associative array, similar to PERL language, using Visual Basic 6. Example: Using PERL %A = ("A"=>"Apple","B"=>"Banana"); i.e., A is associated to the word Apple and B...
7
by: vikas000000a | last post by:
Hi all, I need to know how can I create a PDF file using visual basic. If somebody can tell me the step-by-step procedure for that, I will be very thankful. Actually, I have researched and tried...
2
by: Johnny Wong | last post by:
Hi, I am new to visual basic. In my vb system, there is a dynamic datagrid to represent different reocrds in our database. Today I am gonna export the data to Excel file. I found most examples code...
0
by: paolo71 | last post by:
I have attached the source code written in Visual Basic 6 and I can not translate it in Visual 10 Express, I tried with different translators and even by hand, I modified the parameters that did not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.