I am experimenting with transferring files within a LAN by UDP. I know that UDP has issues with reliability, but at the present time, this is just an experiment and sort of a proof-of-concept.
On Computer1, I'm Using Visual Studio 2012, and have the following code as a "UDP Client"
On Computer2, also using VS 2012, I am using the following as a "UDP Server"
When I click on the send button in Computer1, it should send the file tempzip.zip to Computer2. I have WireShark on both computers and can see that the packets are successfully sent from Computer1/UDPClient and are reaching Computer2/UDPServer. (As I noted in a comment toward the end of the code for the UDPServer, the filestream appears to be getting data.)
The problem is that tempzip.zip on Computer2/UDPServer (at C:\Users\Computer2\Desktop\UDPServer\tempzip.zip) is always empty.
I have tried this same setup with a simple text file of only 14 bytes, and the problem is the same. The packets are received, but nothing is written to the file on Computer2.
After spending the better part of this afternoon trying to find a solution, I still can't understand what the problem is. Does anyone have any ideas?
On Computer1, I'm Using Visual Studio 2012, and have the following code as a "UDP Client"
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
Private Sub ButtonSend_Click(sender As Object, e As EventArgs) Handles ButtonSend.Click
Dim udp_Client As System.Net.Sockets.UdpClient
Dim fStream As FileStream
fStream = File.OpenRead("C:\Users\Computer1\Desktop\tempzip.zip")
Dim fileBytes(fStream.Length) As Byte
fStream.Read(fileBytes, 0, fileBytes.Length)
udp_Client = New Sockets.UdpClient()
udp_Client.Send(fileBytes, fileBytes.Length, "192.168.100.40", 5555)
End Sub
End Class
On Computer2, also using VS 2012, I am using the following as a "UDP Server"
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO
Public Class Form1
Dim IP As String = "192.168.100.50"
Dim Port As Integer = 5555
Dim listenerThread As Thread
Dim udp_Client As UdpClient
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
listenerThread = New Thread(AddressOf Listen)
listenerThread.Start()
End Sub
Public Sub Listen()
udp_Client = New UdpClient(Port)
Dim clientThread As Thread = New Thread(New ThreadStart(AddressOf ThreadPacket))
clientThread.Start()
End Sub
Public Sub ThreadPacket()
Dim fStream As FileStream
fStream = File.Create("C:\Users\Computer2\Desktop\UDPServer\tempzip.zip")
While True
Dim message As Byte() = udp_Client.Receive(New IPEndPoint(IPAddress.Parse(IP), Port))
fStream.Write(message, 0, message.Length)
LabelStatus.BackColor = Color.Yellow 'the label does turn yellow after I send a file from Computer1
End While
End Sub
End Class
When I click on the send button in Computer1, it should send the file tempzip.zip to Computer2. I have WireShark on both computers and can see that the packets are successfully sent from Computer1/UDPClient and are reaching Computer2/UDPServer. (As I noted in a comment toward the end of the code for the UDPServer, the filestream appears to be getting data.)
The problem is that tempzip.zip on Computer2/UDPServer (at C:\Users\Computer2\Desktop\UDPServer\tempzip.zip) is always empty.
I have tried this same setup with a simple text file of only 14 bytes, and the problem is the same. The packets are received, but nothing is written to the file on Computer2.
After spending the better part of this afternoon trying to find a solution, I still can't understand what the problem is. Does anyone have any ideas?