locked
Socket.SetSocketOption does not exist in the current context RRS feed

  • Question

  • Hi,

    I created a Windows Form Application in C# using Visual Studio 2015. My intentions was to create a simple chat client application using the sockets in the same computer to talk to each other.

    After the statements - private void InitializeComponent()

    I typed

     // the Socket sck is created      

      Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
      

      //But, VS complained that 'sck.SetSocketOption' does not exist in the current context

      sck.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

     I looked up Internet and could not find any error close to this. Please help. Thanks!

    Monday, June 13, 2016 11:17 PM

Answers

  • Hi Jiuan Jen Jong,

    >>” After the statements - private void InitializeComponent()”

    Do you mean you declare the “Socket sck” in the “.Designer.cs” file?

    According the error message “does not exist in the current context” I guess you place the statement line in the method “InitializeComponent().

    If so, I suggest you should move the line “Socket sck = new Socket(…)” outside the method “InitializeComponent()” to make sure the “sck” as a class field, not a temporary variable in a method.

        partial class Form5
        {
            ///…
            private void InitializeComponent()
            {
                ///…
            }
    
            Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    
        }

    Best Regards,

    Albert Zhang

    • Proposed as answer by CoolDadTx Wednesday, June 15, 2016 2:07 PM
    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:45 AM
    Wednesday, June 15, 2016 5:22 AM
  • Following up with Albert's statement, the SetSocketOption call needs to be inside a method somewhere and not where you are defining the field.

    Also note that InitializeComponent is called when your form is created, even at design time by the designer. Therefore any runtime behavior that you want to implement should really be done inside the form's OnLoad method. This method isn't called at design time but is called before the form is rendered to the user so it has the same net effect but is better suited for runtime-only logic.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:45 AM
    Wednesday, June 15, 2016 2:09 PM

All replies

  • TcpClient/TcpListener is a simpler, higher-level API than using Socket directly.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, June 13, 2016 11:22 PM
  • Hi Jiuan Jen Jong,

    >>” After the statements - private void InitializeComponent()”

    Do you mean you declare the “Socket sck” in the “.Designer.cs” file?

    According the error message “does not exist in the current context” I guess you place the statement line in the method “InitializeComponent().

    If so, I suggest you should move the line “Socket sck = new Socket(…)” outside the method “InitializeComponent()” to make sure the “sck” as a class field, not a temporary variable in a method.

        partial class Form5
        {
            ///…
            private void InitializeComponent()
            {
                ///…
            }
    
            Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    
        }

    Best Regards,

    Albert Zhang

    • Proposed as answer by CoolDadTx Wednesday, June 15, 2016 2:07 PM
    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:45 AM
    Wednesday, June 15, 2016 5:22 AM
  • Following up with Albert's statement, the SetSocketOption call needs to be inside a method somewhere and not where you are defining the field.

    Also note that InitializeComponent is called when your form is created, even at design time by the designer. Therefore any runtime behavior that you want to implement should really be done inside the form's OnLoad method. This method isn't called at design time but is called before the form is rendered to the user so it has the same net effect but is better suited for runtime-only logic.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by DotNet Wang Monday, June 27, 2016 9:45 AM
    Wednesday, June 15, 2016 2:09 PM