Question about how Sockets work
-
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. -
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. -
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.
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 -
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 -
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.