473,432 Members | 1,765 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,432 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 2647

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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.