-12

这种情况最近开始发生,每次我使用一段代码都认为它是错误的。

例如:

假设我想做一个套接字

public class PacketReader
{

Socket MainSocket;

MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);                               // acts like MainSocket was never declared

    // Bind the socket to the selected IP address
    MainSocket.Bind(newIPEndPoint(IPAddress.Parse(cmbInterfaces.Text),0));

    // Set the socket options
    MainSocket.SetSocketOption(SocketOptionLevel.IP,  //Applies only to IP packets
                       SocketOptionName.HeaderIncluded, //Set the include header
                       true);                           //option to true

    byte[] byTrue = newbyte[4]{1, 0, 0, 0};
    byte[] byOut = newbyte[4];

    //Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
    MainSocket.IOControl(IOControlCode.ReceiveAll,  //SIO_RCVALL of Winsock
                 byTrue, byOut);

    //Start receiving the packets asynchronously
    MainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                    newAsyncCallback(OnReceive), null);
}

然后当我尝试在函数或类的某些部分中使用它时,它会将其返回为“名称是一个字段,但它用作一种类型”。当我所做的只是在课堂上声明它时,我不知道如何将它用作一种类型。

所以我的问题是,我该如何防止这种情况发生。我需要快速找到解决方案,否则我会因为这个愚蠢的问题而使一个月内到期的项目失败。

4

3 回答 3

2

您的代码不在方法中。

您不能在类范围内声明变量并单独分配它。您也不能将随机表达式放在类范围内。将您的代码更改为:

public class PacketReader
{
    Socket MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

    public PacketReader() {
        // rest of the code here in your constructor
    }
}

这听起来很卑鄙……但如果您不了解这样的基本语言结构规则……那么数据包监视器可能就遥不可及了。

于 2013-10-16T00:36:26.513 回答
1

您必须将代码放入运行它的方法中!您只能在方法范围之外声明变量。任何需要运行的语句都必须在方法内。不要试图粗鲁,但我建议你在尝试这样的事情之前多学一点。查看此方法教程以获取更多信息。

public class PacketReader
{
    Socket MainSocket;

    public static void Main()
    {
        MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);                               // acts like MainSocket was never declared

        // Bind the socket to the selected IP address
        MainSocket.Bind(newIPEndPoint(IPAddress.Parse(cmbInterfaces.Text),0));

        // Set the socket options
        MainSocket.SetSocketOption(SocketOptionLevel.IP,  //Applies only to IP packets
                           SocketOptionName.HeaderIncluded, //Set the include header
                           true);                           //option to true

        byte[] byTrue = newbyte[4]{1, 0, 0, 0};
        byte[] byOut = newbyte[4];

        //Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
        MainSocket.IOControl(IOControlCode.ReceiveAll,  //SIO_RCVALL of Winsock
                     byTrue, byOut);

        //Start receiving the packets asynchronously
        MainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
                    newAsyncCallback(OnReceive), null);
    }
}
于 2013-10-16T00:37:46.800 回答
0

如果你正在做这样的事情:

Public class YourClass
{
   Socket MainSocket;

   MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

   //other members and methods and so on...
}

例如这样做...

Public class YourClass
{
   Socket MainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

   //other members and methods and so on...
}

那么如果你想要剩下的,你可以(取决于你的设计和目标)将它放在构造函数或方法中......

于 2013-10-16T00:24:40.067 回答