UDPを使用するモノラルC#アプリケーションを作成しています。私はパケットがNUnitで正しく受け取られるようにテストスイートを書いていました。UDPをローカルでテストするC#
UdpCom
クラスを使用した最小限の作業例を作成しました。これはただちに耳を傾け、コールバックメソッドで受け取ったバイトをプリントアウトすることになっています。
マイ最小限UdpCom
クラスは次のようになります。
using System;
using System.Net;
using System.Net.Sockets;
namespace UdpPractice
{
public class UdpState
{
public UdpClient client;
public IPEndPoint endpoint;
public UdpState(UdpClient c, IPEndPoint iep)
{
this.client = c;
this.endpoint = iep;
}
}
public class UdpCom
{
public UdpState uState;
public UdpClient uClient;
public IPAddress address;
public int port;
public IPEndPoint uEndpoint;
public UdpCom (IPAddress address, int port)
{
uEndpoint = new IPEndPoint (address, port);
uClient = new UdpClient (uEndpoint);
uState = new UdpState (uClient, uEndpoint);
uClient.BeginReceive (new AsyncCallback(RxCallback), uState);
}
public void RxCallback(IAsyncResult result)
{
UdpState rState = (UdpState)(result.AsyncState);
UdpClient rClient = rState.client;
IPEndPoint rEndPoint = rState.endpoint;
byte[] rxBytes = rClient.EndReceive (result, ref rEndPoint);
Console.WriteLine ("Received Bytes ___________________________");
Console.WriteLine (rxBytes.ToString());
rClient.BeginReceive (new AsyncCallback(RxCallback), rState);
}
}
}
私の簡単なテストは、このクラスをインスタンス化して、それをバイトのダミーパケットを送信します。結果を今はテストしていません。私のRxCallback
メソッドにブレークポイントを設定するだけです。
これは私のNUnitのテストです:
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UdpPractice;
namespace UdpTest
{
[TestFixture()]
public class Test
{
[Test()]
public void TestCase()
{
// Setup Receiver
IPAddress address = new IPAddress (new byte[] { 127, 0, 0, 1 });
int port = 14580;
UdpCom com = new UdpCom (address, port);
com.uClient.Connect (com.uEndpoint);
// Set up sender
UdpClient sender = new UdpClient(new IPEndPoint (address, port));
sender.Connect (address, port);
// Dummy Data
byte [] dummyData = new byte[] {1, 2, 3, 4, 4, 5};
sender.Send (dummyData, dummyData.Length);
Thread.Sleep (100);
// Close
sender.Close();
com.uClient.Close();
}
}
}
私の問題は私が、私はこの例外を取得していますバイトを取得しようとしているライン上のRxCallback
方法でいる時ということです。
System.ObjectDisposedException has been thrown
Cannot access a disposed object.
Object name: System.Net.Sockets.UdpClient
これらのバイトをUdpCom
クラスのキューに格納したいと思っていますが、最初にそれらのバイトにアクセスする際に問題があります。
私はUDPの基本的な概念をいくつか立てるのに失敗しているかもしれませんが、それはかなり前のようです。私は、次のされています
udpの例は以下のとおりです。
- https://yal.cc/cs-dotnet-asynchronous-udp-example/
- https://www.codeproject.com/articles/132623/basic-udp-receiver
EDIT ので、私は地元を使用していない、次のように私は私のクラスとテストを更新ループバック。しかし、私はまだ同じ例外を取得しています。
using System;
using System.Net;
using System.Net.Sockets;
namespace UdpPractice
{
public class UdpState
{
public UdpClient client;
public IPEndPoint endpoint;
public UdpState(UdpClient c, IPEndPoint iep)
{
this.client = c;
this.endpoint = iep;
}
}
public class UdpCom
{
public UdpState uState;
public UdpClient uClient;
public IPAddress address;
public int port;
public IPEndPoint uEndpoint;
public UdpCom (IPAddress address, int port)
{
uEndpoint = new IPEndPoint (address, port);
uClient = new UdpClient (uEndpoint);
uState = new UdpState (uClient, uEndpoint);
uClient.BeginReceive (new AsyncCallback(RxCallback), uState);
}
public void RxCallback(IAsyncResult result)
{
UdpState rState = (UdpState)(result.AsyncState);
UdpClient rClient = rState.client;
IPEndPoint rEndPoint = rState.endpoint;
byte[] rxBytes = rClient.EndReceive (result, ref rEndPoint);
Console.WriteLine ("Received Bytes ___________________________");
Console.WriteLine (rxBytes.ToString());
rClient.BeginReceive (new AsyncCallback(RxCallback), rState);
}
}
}
とテストは次のとおりです: UdpCom
は今ある
using NUnit.Framework;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UdpPractice;
namespace UdpTest
{
[TestFixture()]
public class Test
{
[Test()]
public void TestCase()
{
// Setup Receiver
IPAddress address = new IPAddress (new byte[] { 192, 168, 1, 161 });
int port = 14580;
UdpCom com = new UdpCom (IPAddress.Any, port);
// com.uClient.Connect (com.uEndpoint);
// Set up sender
UdpClient sender = new UdpClient(new IPEndPoint (address, port));
sender.Connect (address, port);
// Dummy Data
byte [] dummyData = new byte[] {1, 2, 3, 4, 4, 5};
sender.Send (dummyData, dummyData.Length);
Thread.Sleep (100);
// Close
sender.Close();
com.uClient.Close();
}
}
}
あなたが使用できる別のリソース:http://stackoverflow.com/a/19787486/5233410 – Nkosi
同じIPアドレス、宛先IP、ポート番号の3つのプロパティを持つ接続を1つだけ持つことができます。ローカルに接続するときは、クライアントとサーバーが同じ3つのプロパティを使用していないことを確認する必要があります。解決策は、サーバーをIPAnyでリッスンし、クライアントをPCのIPアドレスに接続させることです。通常はLocalHostであるPC 127.0.0.1のLoopBackアドレスを使用しています。 Netライブラリでは、クライアントがループバックIPを使用することを許可しておらず、例外が発生します。 – jdweng
私は 'com.uClient.Connect(com.uEndpoint);'行を取り出して、 'com'クライアントを' UdpCom com = new UdpCom(IPAddress.Any、port);と送信者UdpClient 192.168.1.161に接続してください。同じ例外が発生しています。変更を反映するために質問を更新しました。 – Jesse