UDPClient 第一次使用 -- 有什么问题吗?
我在学校时想测试 UDPClient 类。 我连接到学校的无线网络,它有严格的防火墙。
与此示例相比,此代码看起来非常可靠。 (http://msdn.microsoft.com/en -us/library/system.net.sockets.udpclient.aspx) 但是当我打开wireshark时,我看不到任何数据包(当我过滤UDP数据包或其他数据包时)。
关于我的代码可能有什么问题有什么想法吗?我认为它被学校的防火墙阻止了,但我不确定。
public static void CallBack(IAsyncResult result)
{
UdpClient myClient = result.AsyncState as UdpClient;
int sent = myClient.EndSend(result);
Console.WriteLine("Sent " + sent.ToString() + " bytes");
}
static void Main(string[] args)
{
UdpClient myClient = new UdpClient(57422);
try
{
myClient.Connect(IPAddress.Parse("127.0.0.1"), 57422);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
int b = 1;
b = IPAddress.HostToNetworkOrder(b);
string myName = "ALEX";
int lengthOfB = System.BitConverter.GetBytes(b).Length;
int lengthOfName = NUEncoder.GetByteCount(myName);
Byte[] intBytes = System.BitConverter.GetBytes(b);
Byte[] nameBytes = NUEncoder.GetBytes(myName);
Byte[] bytesToSend = new Byte[lengthOfB + lengthOfName];
int i = 0;
for (i = 0; i < lengthOfName; i++)
{
bytesToSend[i] = nameBytes[i];
}
for (int k = 0; k < lengthOfB; k++)
{
bytesToSend[i] = intBytes[k];
i++;
}
myClient.BeginSend(bytesToSend, bytesToSend.Length, CallBack, myClient);
Console.WriteLine("Sleeping...");
Thread.Sleep(50);
Console.WriteLine("Done");
}
}
Wanted to test the UDPClient class out while I was at school.
I am connected to the school's wireless which has a strict firewall.
This code seems pretty solid when compared to this example. (http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx)
But when I open up wireshark I don't see any of my packets (when I am filtering for UDP packets or otherwise).
Any ideas on what could be wrong with my code? I am thinking it's being blocked by the school's firewall but I'm not sure.
public static void CallBack(IAsyncResult result)
{
UdpClient myClient = result.AsyncState as UdpClient;
int sent = myClient.EndSend(result);
Console.WriteLine("Sent " + sent.ToString() + " bytes");
}
static void Main(string[] args)
{
UdpClient myClient = new UdpClient(57422);
try
{
myClient.Connect(IPAddress.Parse("127.0.0.1"), 57422);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
int b = 1;
b = IPAddress.HostToNetworkOrder(b);
string myName = "ALEX";
int lengthOfB = System.BitConverter.GetBytes(b).Length;
int lengthOfName = NUEncoder.GetByteCount(myName);
Byte[] intBytes = System.BitConverter.GetBytes(b);
Byte[] nameBytes = NUEncoder.GetBytes(myName);
Byte[] bytesToSend = new Byte[lengthOfB + lengthOfName];
int i = 0;
for (i = 0; i < lengthOfName; i++)
{
bytesToSend[i] = nameBytes[i];
}
for (int k = 0; k < lengthOfB; k++)
{
bytesToSend[i] = intBytes[k];
i++;
}
myClient.BeginSend(bytesToSend, bytesToSend.Length, CallBack, myClient);
Console.WriteLine("Sleeping...");
Thread.Sleep(50);
Console.WriteLine("Done");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在向 PC (127.0.0.1) 发送数据。我认为这就是为什么您在 Wireshark 中看不到任何内容的原因。
You are sending data to your PC (127.0.0.1). I think that this is why you don't see anything with Wireshark.