mfc >> Sockets - When To Receive

by Arlis Rose » Tue, 02 Dec 2003 05:53:39 GMT

Hey everyone. I'm writting a simple chat-like client that uses straight
CSockets (with Cfiles and CArchives of course) and I've run into a bit of a
problem. Initialization and connection run great, the problem is how do I
know if there is data waiting at the socket to be read? Basically if I send
across a message to my server, then call CArchive.readstring() it works
great, however if they are no messages currently waiting, then the program
simply hits an infinite loop :( I know that the method is looping in the
Read method, but I have no idea why.... Okay, so is there someway of telling
if there is data waiting to be read? (NOTE - I have tried to use
CArchive.IsBufferEmpty() but it always returns 1 whether there is data
waiting or not) :( ANy help is greatly appreciated.




mfc >> Sockets - When To Receive

by Scott McPhillips [MVP] » Tue, 02 Dec 2003 09:45:18 GMT






OnReceive will be called when there is data waiting to be read. It is a
virtual function, which means you have to use a class that you derive
from CSocket. Then your OnReceive will be called by the socket, and you
can call Receive from OnReceive.

--
Scott McPhillips [VC++ MVP]




Similar Threads

1. strange problems with Socket.Send/Socket.Receive - CSharp/C#

2. Will Socket Send block receive?

Hi,

My understanding is that for the same socket, assuming send and receive run 
at two different threads, I have to use BeginReceive to avoid the blocking on 
send part. However, I can jsut use Send without worrying about any threading 
issue in case send and beginreceive happens at the same time on the socket?
Am I correct on this?

Thanks

Chris

3. UDP socket problem - not receiving data form server - CSharp/C#

4. Closing socket during async Receive

I am trying to write a Tcp "Server" that opens a class that wraps a tcp
socket when a new connection is made (Listener.AcceptSocket()).
Everything is going swimmingly except when I try to close the socket
during a read and I get the following error:

<error_msg>
An unhandled exception of type 'System.Net.Sockets.SocketException'
occurred in system.dll

Additional information: The I/O operation has been aborted because of
either a thread exit or an application request
</error_msg>

The following is the delegate function for the BeginReceive call:

private void ReadStream(IAsyncResult ar)
{
    if(IsAvailable)
    {
        lock(_socClient) //*** - Futile attempt to stop the exception
        {

            int intByteCount = _socClient.EndReceive(ar);//**** - THIS
IS WHERE I GET MY EXCEPTION

            if(intByteCount > 0)
            {
                _tmrTimeout.Stop();

                ProcessInput(_abytBuffer, intByteCount);//Adds any
ASCII text to a string buffer

                if(IsAvailable)
                {
                    _socClient.BeginReceive(_abytBuffer,0,
                        BUFFER_LENGTH,SocketFlags.None,
                        _acbBeginReadCallback,_socClient);
                }
                _tmrTimeout.Start();
            }
            else
            {
                Close(true);
            }
        }
    }
    else
    {
        Close(false);
    }
}


My shut down routine looks like this:

public bool IsAvailable
{
    get
    {
        if((!_bShutdown) && (_socClient != null) &&
_socClient.Connected)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public void Close(bool bSendStatusUpdate)
{
    if(IsAvailable)
    {
        lock(_socClient)
        {
            _bShutdown = true; //An attempt to indicate what is going
on... SO didn't work

            _socClient.Shutdown(SocketShutdown.Both);
            _socClient.Close();
        }
        //_socClient = null;
    }
    _tmrTimeout.Stop();

    if(bSendStatusUpdate)
    {
        OnClientStatusChanged(new ClientStatusChangedEventArgs(
            CommStatusType.CLOSED,RemoteEndPoint,"Closed"));
    }
}

#region IDisposable Members

public void Dispose()
{
    if(IsAvailable)
    {
        Close(false);
    }
}

#endregion

I thought that maybe I can manually call the EndReceive function from
the Close() routine but I cannot declare a IAsyncResult object. Any
suggestions to close the connection would be great.

Once again, thank you to the Google community for saving my behind.
I'll try to answer a couple of other messages in return.

Cheers,

Russ

5. {simple} sockets socket.Receive question - CSharp/C#

6. Receiving all Multicast traffic via Socket?

Hi C# users,
I have problems with .net's socket classes.
I want a socket to receive all IP-traffic (e.g. "new  
Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP"? I  
would like to be able to receive IGMP, UDP and alikes...) that has got a  
multicast destination address. Maybe somebody has a c# snipped at hand or  
could point out the way to configure such a socket?
Best regards and thanks in advance,
Michael S.

7. receiving a C struct via sockets - CSharp/C#

8. sockets: receive and transmit in one time ?

Hello
I have a thread1 in which i am in infinite loop reading data from 
socket. But sometimes i want to transmit data using that socket (thread2 
decides about it).
Can i simply from thread2 call:
networkStream.Write();
where networkStream is associated with socket from thread1.
That socket is in that moment waiting on networkStream.Read() in thread1.
Will it work ?
How should i solve this problem ?


Thanx