[英]Convert C# code to Android
I'm new to android. 我是android新手。 I've implemented kind of a hole punching in C# and I'm trying to implement the same logic in android. 我已经在C#中实现了打孔,并且试图在android中实现相同的逻辑。
Some of the classes from .NET solution that I Implemented new classes in android such as TimeSpan, though there are some classes that I need to implement and I got stacked. 我在android中实现了新类的.NET解决方案中的一些类,例如TimeSpan,尽管我需要实现一些类,但我还是将它们堆叠在一起。
CODE: 码:
private static Boolean rec_and_wait(TimeSpan interval)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
byte[] data = new byte[1024];
IAsyncResult ar = socket.BeginReceive(data, 0, data.Length, SocketFlags.None, null, null);
int res = WaitHandle.WaitAny(new WaitHandle[] { ar.AsyncWaitHandle, _cancellationTokenSource.Token.WaitHandle }, interval.Add(TimeSpan.FromSeconds(10))); // allow extra 10 seconds for network delay
switch (res)
{
case 0: // response
return true;
case WaitHandle.WaitTimeout: // time out
case 1: //cancelled
default: // should not happen
return false;
}
}
catch
{
return false;
}
finally
{
if (socket != null)
{
socket.Close();
socket.Dispose();
socket = null;
}
}
}
Thanks 谢谢
Use a language converter tool. 使用语言转换器工具。 You can find some nice ones listed HERE 你可以在这里找到一些不错的
It wont give you a 100% result every time, but will take you close to the final result where with a few minor changes here and there you'll get what you want. 它不会每次都为您提供100%的结果,但会带您接近最终结果,在这里进行一些小的更改,您将获得想要的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.