2016-04-30 37 views
0

             我试图使用UDP通过LAN发送图片。我必须将图片“剪切”成小包,然后在另一端重新组装。到目前为止,我已经制作了服务器和几乎客户端(服务器发送图片)。我用BeginReceive做了一些测试,并在其他项目上工作。现在我没有得到任何客户(没有错误..没有)。下面是该服务器的代码:没有UDP数据包接收使用异步方法VB.Net

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 

客户是第二个形式:

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 


             问题出在哪里?

+0

了我多情通过你的代码使用断点? –

+0

不......我不认为我会得到任何东西......我把打印任何地方......什么都没有 –

+0

通过断点你可以得到你的应用程序停止执行代码的地方。我正在尝试你的代码。虽然我不禁注意到你对收到的数据没有做任何事情,但真正的问题是什么? –

回答

1

您的问题是您的客户端不通过同一端口进行通信。

Form2你这样做:

Dim client As New UdpClient(1000) 

然后在Form1当你连接你这样做:

publisher.Connect("localhost", 60000) 

更改端口1000和它的作品:

publisher.Connect("localhost", 1000) 
+0

它工作。然而,我不明白的东西......为什么我最终在创建udpClient的新实例时必须指定端口,我将使用一个已经包含端口的ipEndPoint?还有一件奇怪的事情:我已经在'Dim client As New UdpClient(1000)'中删除了端口,并将'Dim client As New UdpClient(ep)'。随着ep的端口设置为60000它工作(像这样“Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any,60000)')。但我知道0意味着每个端口。使用'Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any,0)'它不起作用。为什么? –

+0

@sergiu:请定义“不起作用”。 –

+0

当我重写这样的代码'Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any,60000) Dim client As New UdpClient(ep)'一切工作正常。但是'universal'端口(0)没有收到数据包:'Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any,0) Dim client as New UdpClient(ep)' –