Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am connected to a TCP server, and constantly have to send data to the server. I am sending data asynchronously, using the socket.BeginSend method. The first attempt at sending the data is successfull, but there after anything that I try to send does not get delivered to the server. It acts almost as if the Socket is disconeected, but I do check for that, and it is not disconnected. I have no idea of what can cause this, so any help or suggestions would be appreciated.

sending method:

C#
public virtual void SendPDU(byte[] PDU)
        {
            try
            {
                if (socket.Connected)
                {
                    Console.WriteLine("Sending: " + PDU.ToString());
                    socket.BeginSend(PDU, 0, PDU.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
                }
                else
                {
                    
                    Console.WriteLine("Socket is not connected to a SMSC");
                    
                }
            }
            catch (Exception e)
            { 
                ExceptionLogger.Logg(e);
                Console.WriteLine("***********Exception Logged***********");
            }
        }

Here is the callback delegate:

C#
private void SendCallback(IAsyncResult ar)
        {
            try
            {
                Socket clientSocket = (Socket)ar.AsyncState;

                int bytesSent = clientSocket.EndSend(ar);
                Console.WriteLine("Sent {0} bytes to SMSC", bytesSent);
                //clientSocket.Dispose();
            }
            catch (Exception e)
            {
                
                Console.WriteLine("***********Exception Logged***********");
                ExceptionLogger.Logg(e);
            }
        }
Posted
Updated 19-Jun-12 4:34am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jun-12 10:35am    
Async socket API? Why? Better use synchronous API with threads, to have simpler sequential algorithms and more control.
--SA
Andrew797 19-Jun-12 14:03pm    
Even when using synchronous, sending the second time does not send data to the server. although the socket is connected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900