FEZ Cobra, WebRequest issue, error code 10038

Hello!

I am building a pubnub client on fez cobra. Everything works ok for the first time, but then, if I cancel listener for the pubnub channel
and relaunch it, I am getting socket error with code 10038 indefenitely … for it to work again, I have to restart the whole app.
I am using ethernet for internet connection.

I found some posts about that error on this forum, but no solution was given.

The error code reads as following:

[quote]Socket operation on nonsocket.
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.[/quote]

I have no clue what does that mean whatsoever :S

And here is the code for the listener thread …

private void ChannelListener(object channelSettings)
{
    var channel = (ChannelSubscription) channelSettings;
    channel.Status = Status.Running;
    var ct = channel.CancellationToken;

    string timetoken = "0";
    var urlComponents = new ArrayList {"subscribe", this.subscribeKey, channel.Name, "0", timetoken};

    while (true)
    {
        try
        {
            urlComponents[4] = timetoken;
            var url = this.BuildAndEncodeUrl(urlComponents);

            var request = (HttpWebRequest) WebRequest.Create(url);
            request.KeepAlive = true;
            request.Timeout = 200000;
            request.ReadWriteTimeout = 200000;

            ct.Register(request.Dispose);

            HttpWebResponse response = null;

            try
            {
                ct.ThrowIfCancellationRequested();
                response = (HttpWebResponse) request.GetResponse();
            }
            catch (WebException)
            {
                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }
                else
                {
                    continue;
                }
            }

            string json;
            using (response)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                json = reader.ReadToEnd();
            }

            ct.ThrowIfCancellationRequested();
            ArrayList objectData = JsonSerializer.Deserialize(json);
            if (objectData.Count == 2)
            {
                if (!StringEx.IsNullOrEmpty((string) objectData[1]))
                    timetoken = (string) objectData[1];

                ct.ThrowIfCancellationRequested();
                var message = (ArrayList) objectData[0];
                if (message.Count > 0)
                {
                    channel.MessageQueue.Enqueue(new IncommingMessage(message, timetoken));
                }
            }
        }
        catch (OperationCanceledException)
        {
            Debug.Print(StringEx.Format("Listener for {0} channel has unsubscribed", channel.Name));
            channel.Status = Status.Stopped;
            return;
        }
        catch (Exception ex)
        {
            Debug.Print(StringEx.Format("Listener for {0} channel has failed ... Exception = {1}", channel.Name, ex.Message));
        }
    }
}

What is wrong with the following code and what should I do the resolve the issue?
Thank you very much for any help!

Hi Pajci007, welcome to the forums.

The error says that your socket object has been discarded, but you’re trying to use it again. You have to figure out where you discard it and not do that :slight_smile:

It would seem that your fault is when you “cancel listener for the pubnub channel”. Whatever that code is doing is the fault. Where’s your code for that?

Then I believe I shouldn’t call dispose on WebRequest object.
The code for that is this:

ct.Register(request.Dispose);

I mimic the CancellationToken, which is included in windows .NET … so when I call cancel on cancellation token, request.Dispose() is called,
to immediately stop channel listener … I was looking for request.Abort(), but that method is not implemented, so I thought dispose will do the same.

If this is the case, why does dispose method of webrequest causes this error?

I solved the issue. The problem was, that when cobra lost connection with either zigbee usb dongle or ethernet, I shut down the whole service, and then
when the required devices were connected again, I restarted everything. What was wrong with my restart sequence was this piece of code:

NetworkInterface netif = NetworkInterface.GetAllNetworkInterfaces()[0];
if (!netif.IsDhcpEnabled)
{
    netif.EnableDhcp();
}
else
{
    netif.RenewDhcpLease();
}

Everytime I restarted service, RenewDhcpLease was called, which somehow corrupted socket … I was planning to remove this
anyway, as dhcp is enabled the first time I start the service, so there is no need to renew it, right? I deleted the else clause
and everything is working now as it is supposed to :stuck_out_tongue: