Click here to Skip to main content
15,918,808 members
Home / Discussions / C#
   

C#

 
GeneralRe: Very Simple HTML Parser C# Pin
jschell22-Jul-10 8:27
jschell22-Jul-10 8:27 
AnswerRe: Very Simple HTML Parser C# Pin
V.21-Jul-10 20:59
professionalV.21-Jul-10 20:59 
GeneralRe: Very Simple HTML Parser C# Pin
Luc Pattyn21-Jul-10 21:05
sitebuilderLuc Pattyn21-Jul-10 21:05 
GeneralRe: Very Simple HTML Parser C# Pin
V.21-Jul-10 22:53
professionalV.21-Jul-10 22:53 
QuestionSeriously silly sockets - please assist Pin
GrenMeera21-Jul-10 9:56
GrenMeera21-Jul-10 9:56 
AnswerRe: Seriously silly sockets - please assist Pin
Jimmanuel21-Jul-10 10:34
Jimmanuel21-Jul-10 10:34 
GeneralRe: Seriously silly sockets - please assist Pin
GrenMeera22-Jul-10 5:11
GrenMeera22-Jul-10 5:11 
GeneralRe: Seriously silly sockets - please assist Pin
Jimmanuel22-Jul-10 7:26
Jimmanuel22-Jul-10 7:26 
Glad to help Smile | :)

I know you didn't ask for it, but I refactored your snippet into a thread-safe, error handling version as an exercise. I haven't actually run it, but I believe it'll work for you (or at least be close) given that the state object passed as the last parameter to BeginRecieve is _recvState:

C#
private static void ReceiveCallback(IAsyncResult ar)
{
    // End Receive
    StateObject stateObject = (StateObject)ar.AsyncState;

    if (stateObject == null)
    {
        return;
    }

    lock (stateObject)
    {
        if (stateObject.Socket == null)
        {
            // done!
            return;
        }

        int bytesReceived = 0;

        try
        {
            bytesReceived = stateObject.Socket.EndReceive(ar);
        }
        catch (SocketException)
        {
            // non-recoverable
            return;
        }
        catch (ObjectDisposedException)
        {
            // non-recoverable
            return;
        }

        if (bytesReceived > 0)
        {
            // Parse Data
            SocketConnection.ParseReceiveBuffer(stateObject);
        }

        // Begin Receive
        stateObject.Socket.BeginReceive(stateObject.DataBuffer, 0, stateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), stateObject);
    }
}

public void CloseConnections()
{
    // Receive
    lock(_recvState)
    {
        if (_recvSocket != null)
        {
            _recvSocket.Shutdown(SocketShutdown.Both);
            _recvSocket.Close();
            _recvSocket = null;
            _recvState.Socket = null; // setting this to null will signify that we've closed the connection
            _recvState = null; // don't reset this to null, if we do then we can't lock on it without first checking it
        }
    }

    // then something similar for sending . . 
}


Actually I have to thank you, your original post caused me to go back into some old code and re-look at my own async socket class. Have you ever gone back and looked at something you wrote a long time ago and wondered what was going through your mind when you originally wrote it? I just had one of those moments. It's time for some bug fixing! Smile | :)
Badger | [badger,badger,badger,badger...]

AnswerRe: Seriously silly sockets - please assist Pin
LimitedAtonement21-Jul-10 10:42
LimitedAtonement21-Jul-10 10:42 
GeneralRe: Seriously silly sockets - please assist Pin
GrenMeera22-Jul-10 5:18
GrenMeera22-Jul-10 5:18 
GeneralRe: Seriously silly sockets - please assist Pin
jschell22-Jul-10 8:38
jschell22-Jul-10 8:38 
QuestionRe: Seriously silly sockets - please assist Pin
Jimmanuel22-Jul-10 9:20
Jimmanuel22-Jul-10 9:20 
QuestionOnly in Release config, SetWindowsHookEx throws error 80004005 Pin
LimitedAtonement21-Jul-10 9:36
LimitedAtonement21-Jul-10 9:36 
AnswerRe: Only in Release config, SetWindowsHookEx throws error 80004005 Pin
Dave Kreskowiak21-Jul-10 10:22
mveDave Kreskowiak21-Jul-10 10:22 
Question? : Operator Pin
I Believe In GOD21-Jul-10 7:00
I Believe In GOD21-Jul-10 7:00 
AnswerRe: ? : Operator PinPopular
harold aptroot21-Jul-10 7:02
harold aptroot21-Jul-10 7:02 
GeneralRe: ? : Operator Pin
I Believe In GOD21-Jul-10 7:06
I Believe In GOD21-Jul-10 7:06 
GeneralRe: ? : Operator Pin
harold aptroot21-Jul-10 7:13
harold aptroot21-Jul-10 7:13 
GeneralRe: ? : Operator Pin
I Believe In GOD21-Jul-10 7:12
I Believe In GOD21-Jul-10 7:12 
AnswerRe: ? : Operator Pin
Luc Pattyn21-Jul-10 7:02
sitebuilderLuc Pattyn21-Jul-10 7:02 
GeneralRe: ? : Operator Pin
I Believe In GOD21-Jul-10 7:10
I Believe In GOD21-Jul-10 7:10 
GeneralRe: ? : Operator Pin
Roger Wright21-Jul-10 7:54
professionalRoger Wright21-Jul-10 7:54 
GeneralRe: ? : Operator Pin
Luc Pattyn21-Jul-10 8:05
sitebuilderLuc Pattyn21-Jul-10 8:05 
GeneralRe: ? : Operator Pin
Roger Wright21-Jul-10 8:35
professionalRoger Wright21-Jul-10 8:35 
GeneralRe: ? : Operator Pin
Eddy Vluggen21-Jul-10 9:12
professionalEddy Vluggen21-Jul-10 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.