在Android(C#)上创建LAN tcp服务器时出现静态套接字错误

在Android(C#)上创建LAN tcp服务器时出现静态套接字错误,c#,android,sockets,tcp,server,C#,Android,Sockets,Tcp,Server,我正在尝试制作一个局域网服务器,它可以通过本地路由器在android设备之间传输整数和字符串。c#代码与system.net指令一起工作,但是会产生一个错误,说明“修饰符'static'对此项无效” 我们是白痴吗 谢谢 using System; using Android.Systems; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Wid

我正在尝试制作一个局域网服务器,它可以通过本地路由器在android设备之间传输整数和字符串。c#代码与system.net指令一起工作,但是会产生一个错误,说明“修饰符'static'对此项无效”

我们是白痴吗

谢谢

using System;
using Android.Systems;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Net;
using Java.Net;



namespace My_App
{
    [Activity(Label = "My_App", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.bt_client);

            button.Click += delegate {

                static Socket sck;


                sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
                try
                {
                    sck.Connect("127.0.0.1", 1234);
                }
                catch
                {
                    Console.WriteLine("Unable to connect to remote end point! \r\n");
                }
                Console.Write("Enter Text");
                String text = Console.ReadLine();
                byte[] data = Encoding.ASCII.GetBytes(text);

                sck.Send(data);
                Console.Write("Data Sent \r\n");
                Console.Write("Press any key to continue...");
                Console.Read();
                sck.Close();

            };

        }
    }
}
使用系统;
使用安卓系统;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Android.Net;
使用Java.Net;
名称空间My_应用程序
{
[活动(Label=“My_App”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.bt_客户端);
按钮。单击+=委派{
静态插座;
sck=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
IPEndPoint localEndPoint=新的IPEndPoint(IPAddress.Parse(“127.0.0.1”),1234);
尝试
{
sck.Connect(“127.0.0.1”,1234);
}
抓住
{
Console.WriteLine(“无法连接到远程端点!\r\n”);
}
控制台。写入(“输入文本”);
String text=Console.ReadLine();
字节[]数据=Encoding.ASCII.GetBytes(文本);
发送(数据);
Console.Write(“已发送的数据”);
控制台。写入(“按任意键继续…”);
Console.Read();
sck.Close();
};
}
}
}
根据以下MSDN:

C#不支持静态局部变量(在方法范围内声明的变量)

由于a是指向方法的指针,这意味着您不能在委托中声明静态变量。如果希望套接字是静态的,则必须在类级别声明它