Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
 
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:
 
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 19-Jun-12 4:30am
Edited 19-Jun-12 4:34am
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 19 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100