This project has moved. For the latest updates, please go here.

ClientSocket_WriteCompleted doesnt work

Aug 13, 2012 at 1:52 PM

Hi ClientSocket_WriteCompleted  doesnt work. But data sended to server corectly. My Client running from ActionThread and its has a loop for Create new actionThread.

 

       string description =String.Empty;
            try
            {
               
                byte[] binaryMessage = MessageUtility.Serialize(message);

                // Send the message; the state is used by ClientSocket_WriteCompleted to display
                description = message.MessageID.ToString() + "||" + message.RegisterID.ToString();
              
                ClientSocket.WriteAsync(binaryMessage, description);
               
                LogHelper.For(this).Info("Sending message " + description);
            }
            catch (Exception ex)
            {
                ResetSocket();
                messageSendHandler(description + "||" + 0 + "||" + ClientSocket.RemoteEndPoint.ToString());
                Logger("Error sending message to socket: [" + ex.GetType().Name + "] " + ex.Message);
            }
            finally
            {
               DisplayRefresh(ClientSocket, ClientSocketState);
            }

Coordinator
Aug 13, 2012 at 3:03 PM

What does ClientSocket_WriteCompleted look like, and can you also post the code where you're subscribing to this event?

Aug 14, 2012 at 6:10 AM

I have Nclient Class

// Subscribe event is

 public bool Connect(string strIpAdress,string strPort)
        {
            bool connected = false;
            try
            {
                IPAddress serverIPAddress;
                if (!IPAddress.TryParse(strIpAdress, out serverIPAddress))
                {

                    LogHelper.For(this).Error("Invalid IP address: " + strIpAdress);
                    return connected;
                }             
                int port;
                if (!int.TryParse(strPort, out port))
                {
                    LogHelper.For(this).Error("Invalid port number: " + strPort);
                
                    return connected;
                }             
                ClientSocket = new SimpleClientTcpSocket();
                ClientSocket.ConnectCompleted += ClientSocket_ConnectCompleted;
                ClientSocket.PacketArrived += ClientSocket_PacketArrived;
                ClientSocket.WriteCompleted += (args) => ClientSocket_WriteCompleted(ClientSocket, args);
                ClientSocket.ShutdownCompleted += ClientSocket_ShutdownCompleted;
                ClientSocket.ConnectAsync(serverIPAddress, port);
                ClientSocketState = SocketState.Connected;
                connected = true;
                LogHelper.For(this).Info("Connecting socket to " + (new IPEndPoint(serverIPAddress, port)).ToString());
            }
            catch (Exception ex)
            {
                ResetSocket();
                connected = false;
                LogHelper.For(this).Error("Error creating connecting socket: [" + ex.GetType().Name + "] " + ex.Message);
            }
            finally
            {
               DisplayRefresh(ClientSocket, ClientSocketState);
            }
            return connected;
        }

// my write completed event code is

 private void ClientSocket_WriteCompleted(object sender, AsyncCompletedEventArgs e)
        {
            // Check for errors
            int sentOk = 1;
            string description = (string)e.UserState;
            if (e.Error != null)
            {
                // Note: WriteCompleted may be called as the result of a normal write or a keepalive packet.

                ResetSocket();
                sentOk = 0;
                // If you want to get fancy, you can tell if the error is the result of a write failure or a keepalive
                //  failure by testing e.UserState, which is set by normal writes.
                if (e.UserState is string)
                    LogHelper.For(this).Error("Socket error during Write: [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
                else
                    LogHelper.For(this).Error("Socket error detected by keepalive: [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine);
            }
            else
            {


                LogHelper.For(this).Info("Socket write completed for message " + description );
            }
            description += "||" + sentOk.ToString() + "||" + ClientSocket.RemoteEndPoint.ToString();
            messageSendHandler(description);
            DisplayRefresh(ClientSocket, ClientSocketState);
        }

 

Aug 14, 2012 at 2:29 PM

I solved this problem. Problem is I write data to port then close connection. Write_completed never fired. Then i changed my code to i call disconnect method in end of write_completed now everthing is fine .

 

Thanks Stephen for supporting this project.

Coordinator
Aug 14, 2012 at 8:52 PM

Yup, that would certainly do it.

The way these socket classes work, as soon as you close them, no more events are triggered.

I should have thought to ask that! :)

Aug 17, 2012 at 6:31 AM

me too :)