2016-05-01 17 views
1

         私はUDPクライアント - サーバー接続を作成し、大きな画像を送信しました。
       私は分割して、それぞれ1500バイトのパケットに画像を送りました。私は353723の330025バイトのもう一端を得ました(これはかなり良いと思います)。しかし、あなたが知っているように、UDPパケットは順序どおりではないので、私はそれが送信されたすべてのパケットにIDを持たなければならない(私は自分のプロジェクトゲームのスピードが必要だから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 


         エラーなし。私はサーバーからクライアントに画像を送ることができますが、それ以外の方法で画像を送ることはできません。そして別の奇妙なことがあります。私はクライアントのコードを別のマシンに置いています...これはepvclientに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

ただちに、1つのマシン上のポート番号に1つのソケットをバインドすることができます。したがって、「各ソケットアドレスの1つの使用法」を提供するために、ポート60000にバインドされているものが必要です。 .. "例外 - あなたのプログラムの別のインスタンスがすでに実行されていますか?

私はゲーム開発者であり、UDPを使用する必要性を理解しています。ここでは、UDPはTCPの代わりに適した選択肢ではありません。 UDPは、大規模な1回限りのデータではなく、小さな「リアルタイム」データに適しています。 UDPを使用している場合は、信頼性 - データグラムがまったく通過しない、重複する - データグラムが重複する、輻輳制御 - 前回のデータグラムを確認せずにすばやく連続して送信するなど、落とされています...本当に大きなファイルを送信する方がTCPにはるかに適しています。

+0

あなたは正しいと思います。私はTCPを使用します。私はUDPやUDPの質問で誰も気にしないことができます。 –

+0

UDPはユースケースを持っていますが、小さいながら頻繁な "リアルタイム"のデータに対しては、ファイルを送信するときに確実にすべてを送信する必要がある場合、ホイールを再発明するのではなく、TCPを使う方がよいでしょう。 – markmnl

関連する問題