0

       I'm trying to send a picture over lan using UDP. I have to "cut" the picture into small packets and then reassemble it at the other end. So far I've made the server and almost the client (the server sends the picture). I've made some tests with BeginReceive and worked (on other project). Now I get nothing on the client(no error..nothing). Here's the code for the server:

Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding


Public Class Form1
    Dim publisher As New Sockets.UdpClient(0)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sendbytes() As Byte = ASCII.GetBytes(txt1.Text)
        Dim img As Image, img_stream As MemoryStream, buffer As Byte()
        Dim packet_size As Integer = 1500, sent_size As Long


        Try
            publisher.Connect("localhost", 60000)
            img_stream = imgToBytes(txt1.Text)

            ReDim buffer(packet_size)

            While Not img_stream.Position = img_stream.Length
                sent_size += img_stream.Read(buffer, 0, packet_size)

                publisher.Send(buffer, buffer.Length)
            End While


        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub

    Function imgToBytes(ByVal file_name As String) As MemoryStream
        Dim img As Image = Image.FromFile(file_name)
        Dim stream As New MemoryStream

        img.Save(stream, Drawing.Imaging.ImageFormat.Jpeg)
        stream.Position = 0

        Return stream
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub
End Class

The client is on the second form:

Imports System
Imports System.IO
Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text.Encoding


Public Class Form2
    Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
    Dim client As New UdpClient(1000)

    Public Event new_msg(ByVal msg As Byte())
    Public Sub client_msg(ByVal msg As Byte())
        Debug.Print("a")
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try      ''don't know why I put this here
            client.BeginReceive(New AsyncCallback(AddressOf receive), client)

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

    Sub receive(ByVal ar As IAsyncResult)
        Dim buffer As Byte()
        Debug.Print("b")

        Try
            buffer = client.EndReceive(ar, ep)
            ''RaiseEvent new_msg(buffer)

            client.BeginReceive(New AsyncCallback(AddressOf receive), client)
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

End Class


       Where is the problem?

sergiu reznicencu
  • 1,039
  • 1
  • 11
  • 31

1 Answers1

1

Your problem is that your clients are not communicating over the same port.

In Form2 you do:

Dim client As New UdpClient(1000)

Then in Form1 when you connect you do:

publisher.Connect("localhost", 60000)

Change the port to 1000 and it works:

publisher.Connect("localhost", 1000)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • It worked. However I don't understand something..why do I have to specify the port when creating a new instance of the udpClient when in the end I'll use an ipEndPoint which already contains the port? There's another weird thing:I've deleted the port in `Dim client As New UdpClient(1000)` and put `Dim client As New UdpClient(ep)`. With the ep's port set to 60000 it worked(like this `Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000)`). But I know that 0 means every port. With `Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)` it doesn't work. Why? – sergiu reznicencu Apr 30 '16 at 20:27
  • When I rewrite the code like this `Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000) Dim client As New UdpClient(ep)` everything works fine. But with the 'universal' port (0) it receives no packet: `Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0) Dim client As New UdpClient(ep)` – sergiu reznicencu Apr 30 '16 at 20:35
  • @sergiu : Hmm, strange. Maybe the port is changed by the `IPEndPoint` because is perhaps doesn't allow it to be 0? Also, you don't have to specify a port when creating the `UdpClient`. Doing `Dim client As New UdpClient()` should work without a problem. – Visual Vincent Apr 30 '16 at 20:38
  • This looks very strange. It looks like I'm sending all the packets from the server but I get only 67000 of bytes from the picture's 320000 bytes. I get only 45 packets of 1500 bytes each... – sergiu reznicencu Apr 30 '16 at 20:40
  • I'm starting to hate UDP. This is the second time I try to send a large image over lan...no success. I'm thinking to use TCp instead of UDP – sergiu reznicencu Apr 30 '16 at 20:42
  • @sergiu : UDP is a lossy protocol. There will probably be some packet loss at times (however in your case that's very many). If you want to reliably send the whole image you should switch to TCP instead which resends dropped packets automatically. Also, [you cannot listen to any port. 0 is invalid](http://stackoverflow.com/a/3301202/3740093). – Visual Vincent Apr 30 '16 at 20:44
  • New UPDATE: I'm getting now 74 packets..I think it's the common 'packets lost' disease...I'll try to 'id; every packet I send . – sergiu reznicencu Apr 30 '16 at 20:44
  • @sergiu : ID-ing every packet will probably not help unless you find a way to resend lost ones. Switch to TCP, it's better and more reliable and does all that for you. See [**my answer**](http://stackoverflow.com/a/35240061/3740093) to how you can implement packet handling. – Visual Vincent Apr 30 '16 at 20:46