Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Question about how Sockets work

Question about how Sockets work

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jordanwb
    wrote on last edited by
    #1

    I got a question about how sockets work it's kinda confusing. public ServerConnection(IPAddress this_machine, UInt16 port) : base() { this.init_connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.ip_end_point = new IPEndPoint(this_machine, (int)port); this.init_connection.Bind(this.ip_end_point); this.init_connection.Listen(2); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection); } private void OnDataControlConnect(IAsyncResult ar) { this.data_control = (Socket)ar.AsyncState; this.data_control.BeginReceive(this.data_control_buffer, 0, 4, SocketFlags.None, new AsyncCallback(this.OnDataControlReceive), this.data_control); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataConnect), this.init_connection); } Now in the OnDataControlConnect method I cast the AsyncState method as a socket. Is the socket that was casted the same socket I passed in "this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection);"? I hope I'm being clear enough I'm still trying to understand how sockets work. Thanks.

    J 2 Replies Last reply
    0
    • J Jordanwb

      I got a question about how sockets work it's kinda confusing. public ServerConnection(IPAddress this_machine, UInt16 port) : base() { this.init_connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.ip_end_point = new IPEndPoint(this_machine, (int)port); this.init_connection.Bind(this.ip_end_point); this.init_connection.Listen(2); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection); } private void OnDataControlConnect(IAsyncResult ar) { this.data_control = (Socket)ar.AsyncState; this.data_control.BeginReceive(this.data_control_buffer, 0, 4, SocketFlags.None, new AsyncCallback(this.OnDataControlReceive), this.data_control); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataConnect), this.init_connection); } Now in the OnDataControlConnect method I cast the AsyncState method as a socket. Is the socket that was casted the same socket I passed in "this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection);"? I hope I'm being clear enough I'm still trying to understand how sockets work. Thanks.

      J Offline
      J Offline
      Jordanwb
      wrote on last edited by
      #2

      I got another question: If the client shuts down, then disconnects the socket, how does the host know to disconnect as well? I'm starting to get the hang of connecting, sending and receiving with sockets. Thanks.

      S 1 Reply Last reply
      0
      • J Jordanwb

        I got another question: If the client shuts down, then disconnects the socket, how does the host know to disconnect as well? I'm starting to get the hang of connecting, sending and receiving with sockets. Thanks.

        S Offline
        S Offline
        Shy Agam
        wrote on last edited by
        #3

        For your first question... Yes... It's the same socket... As for the second question, I'm not sure I understand... But if I do, than you should do the following in the callback function you used when calling BeginReceive(): private void ReceiveCallback(IAsyncState state) { . . . int receivedBytes = yoursocket.EndReceive(...); if (receivedBytes <= 0) { // Other side closed the connection... // Do as you wish... } else { // Analyze the received data } . . . } I wrote it the way I remember... So it may not be 100% accurate, but this is the main idea. Good luck, Shy

        J 1 Reply Last reply
        0
        • S Shy Agam

          For your first question... Yes... It's the same socket... As for the second question, I'm not sure I understand... But if I do, than you should do the following in the callback function you used when calling BeginReceive(): private void ReceiveCallback(IAsyncState state) { . . . int receivedBytes = yoursocket.EndReceive(...); if (receivedBytes <= 0) { // Other side closed the connection... // Do as you wish... } else { // Analyze the received data } . . . } I wrote it the way I remember... So it may not be 100% accurate, but this is the main idea. Good luck, Shy

          J Offline
          J Offline
          Jordanwb
          wrote on last edited by
          #4

          Thanks, your reply has helped me quite a bit.

          modified on Sunday, March 23, 2008 9:35 PM

          1 Reply Last reply
          0
          • J Jordanwb

            I got a question about how sockets work it's kinda confusing. public ServerConnection(IPAddress this_machine, UInt16 port) : base() { this.init_connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.ip_end_point = new IPEndPoint(this_machine, (int)port); this.init_connection.Bind(this.ip_end_point); this.init_connection.Listen(2); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection); } private void OnDataControlConnect(IAsyncResult ar) { this.data_control = (Socket)ar.AsyncState; this.data_control.BeginReceive(this.data_control_buffer, 0, 4, SocketFlags.None, new AsyncCallback(this.OnDataControlReceive), this.data_control); this.init_connection.BeginAccept(new AsyncCallback(this.OnDataConnect), this.init_connection); } Now in the OnDataControlConnect method I cast the AsyncState method as a socket. Is the socket that was casted the same socket I passed in "this.init_connection.BeginAccept(new AsyncCallback(this.OnDataControlConnect), this.init_connection);"? I hope I'm being clear enough I'm still trying to understand how sockets work. Thanks.

            J Offline
            J Offline
            Jordanwb
            wrote on last edited by
            #5

            I've got another question. If a host has a socket open and BeginAccept has been called. Is it possible for a Client to be able to search for open sockets bound to a certain porta and detect that socket?

            modified on Monday, March 24, 2008 12:55 PM

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups