我正在尝试转换可以在Mono for Android中处理TCP服务器的代码。这是来自this chat app for Windows tutorial的代码。但是,我收到了错误:
ListenForClient访问被拒绝!
它由代码的这一部分创建:
try
{
//We are using TCP sockets
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
//Assign the any IP of the machine and listen on port number 1000
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1000);
//Bind and listen on the given address
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);
//Accept the incoming clients
serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
}
catch (Exception ex)
{
System.Console.WriteLine("@ ListenForClient" + ex.Message);
Android.Util.Log.Info("ListenForClient", ex.Message);
}
看起来Mono for Android中不允许使用serverSocket.Bind(IPEndPoint);
。
有人知道解决方法或知道为什么不允许这样做吗?
答案 0 :(得分:0)
找到解决方案,这是2个问题,
1)android清单:
<uses-permission android:name="android.permission.INTERNET" />
2)保留端口1000!