2016-05-01 89 views
1

           我成功创建了一个UDP客户端 - 服务器连接并通过它发送大图像。
         我分裂并发送图像到每个1500字节的数据包。我在另一端330025字节的353723(我认为很好)。但是,正如你所知,UDP数据包不是按顺序排列的,所以我必须确认它发送的每个数据包(我不能使用tcp,因为我需要项目游戏的速度)。现在我想问问'服务器'丢失的数据包。Udp客户端用于接收和发送不工作



这是服务器的代码:

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的客户对服务器和客户端的代码如下:Dim client as new UdpClient(ep_client)它给出了一个错误:

每个套接字地址的一个用法(协议/网络地址/端口) 是正常允许

但如果我把它写这样的(在服务器端)Dim client as new UdpClient("127.0.0.1",60000)它没有给error.What的区别? 问题在哪里?

回答

1

你的直接问题是你在任何时候只能在一台机器上绑定一个端口号 - 所以必须已经有一些绑定到端口60000的东西给你“每个套接字地址只有一个用法。 ..“异常 - 也许你的程序的另一个实例已经运行?

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

+0

我认为你是对的。我将使用TCP。我可以看到没有人用UDP或UDP-问题困扰。 –

+0

UDP确实有一个用例 - 对于小而频繁的“实时”数据,但是如果您需要可靠地发送所有内容 - 您在传输文件时要做的事情 - 你最好用TCP而不是重新发明轮子。 – markmnl

相关问题