【问题标题】:Udp client used for receiving and sendin not working用于接收和发送的 udp 客户端不起作用
【发布时间】:2016-05-02 15:45:40
【问题描述】:

     我成功创建了一个 UDP 客户端-服务器连接并通过它发送了一个大图像。
    我将图像拆分并发送成每个 1500 字节的数据包。我在另一端得到了 353723 的 330025 字节(我认为很不错)。但是正如您所知,UDP 数据包不是按顺序排列的,所以我必须识别它发送的每个数据包(我不能使用 tcp,因为我的项目游戏需要速度)。现在我想向“服务器”询问丢失的数据包。



这是服务器的代码:

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


Public Class Form1
    Dim ep_client As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)
    Dim ep_server As New IPEndPoint(IPAddress.Any, 60000)
    Dim publisher As New UdpClient(ep_client)


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

    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 = 1024, sent_size As Long


        Try
            img_stream = imgToBytes(txt1.Text)

            Debug.Print(img_stream.Length)
            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

            Debug.Print(100 * sent_size / img_stream.Length & "%")
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ''When I press this button it is supposed to 'listen'

        Try
            'Silence...Nothing here..
            publisher.BeginReceive(New AsyncCallback(AddressOf receive), publisher) 
            Debug.Print("listening")
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try


    End Sub
    Sub receive(ByVal ar As IAsyncResult)
        ''It should take the packets from coming from 'any' ip.

        publisher.EndReceive(ar, ep_server)

        Debug.Print("received")

        publisher.BeginReceive(New AsyncCallback(AddressOf receive), publisher)
    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
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_server As IPEndPoint = New IPEndPoint(IPAddress.Any, 60000)
    Dim ep_client As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000)

    Dim client As New UdpClient(ep_client)
    Dim stream As New MemoryStream



    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            'I belive it's listening for packets comming from ep_client ->
            'because I initialized the 'client' object with it
            'How can I make it listen for ep_server?(from all ip's)
            client.BeginReceive(New AsyncCallback(AddressOf receive), client)

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

    Public Sub receive(ByVal ar As IAsyncResult)
        Dim buffer As Byte()

        Try
           'Now I use ep_server ->the proper way
            buffer = client.EndReceive(ar, ep_server)  
            stream.Write(buffer, 0, buffer.Length)

            client.BeginReceive(New AsyncCallback(AddressOf receive), client)

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

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim img As Image = Image.FromStream(stream)

        Debug.Print(stream.Length)
        PictureBox1.Image = img      ''Weird image because the packets are not arranged
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Debug.Print("sent")
        client.Send(ASCII.GetBytes("a"), 1, ep_client)   ''Send something ->not working
    End Sub

End Class


     没有错误..没什么。我可以将图片从服务器发送到客户端,但不能反过来。还有一个奇怪的事情。我已经将客户端的代码放在另一台机器上......当我在 ep_client 中编写 ipv4(另一台机器的)时,就像 Dim ep_client As New IPEndPoint(IPAddress.Parse("192.168.1.100"), 60000) 一样。
     我没有收到任何错误。如果我像这样在服务器和客户端代码上初始化两个“udp-clients”:Dim client as new UdpClient(ep_client) 它会给出一个错误:

每个套接字地址(协议/网络地址/端口)只能使用一次 通常是允许的

但是如果我这样写(在服务器端)Dim client as new UdpClient("127.0.0.1",60000) 它不会出错。有什么区别? 问题出在哪里?

【问题讨论】:

    标签: vb.net sockets udp


    【解决方案1】:

    您的直接问题是您在任何给定时间只能将一个套接字绑定到一台机器上的端口号 - 因此必须已经绑定到端口 60000 才能为您提供“每个套接字地址只能使用一次.. ." 异常 - 也许您的程序的另一个实例已经在运行?

    除此之外,我是一名游戏开发人员,并且了解使用 UDP 的必要性,这里使用 UDP 代替 TCP 不是一个好的选择。 UDP 适用于小型“实时”数据而不是大型一次性数据。如果使用 UDP,您必须担心:可靠性 - 数据报可能根本无法通过,重复 - 数据报可能重复,拥塞控制 - 快速连续发送太多数据报而不检查先前的数据报是否通过只会导致它们被丢弃...真正发送大文件更适合 TCP。

    【讨论】:

    • 我认为你是对的。我将使用 TCP。我可以看到没有人为 UDP 或 UDP 问题而烦恼..
    • UDP 确实有一个用例 - 用于小而频繁的“实时”数据,但如果您需要可靠地发送所有内容 - 您在传输文件时执行此操作 - 您最好使用 TCP重新发明轮子。
    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2023-03-06
    • 2021-05-05
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多