exception in socket .ctor()

Hello,

Sometimes, just sometimes, my socket code throws an exception with following message:
"Socket operation on nonsocket"
...or something like that, I work on the polish language pack, so this is just a translation. Wierd thing is that this exception is thrown in the socket's constructor! A bit of code:

using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.Icmp))
{
// the exception is thrown in constructor!
s.WhateverFunc1();
s.WhateverFunc2();
}

What may cause such behaviour

Some, perhaps helpful, information:
- application runs on WinXPSP2 NET1.1SP1 Intel4HT (pseudo MP machine, hmmm...)
- it is multithreaded, this piece of code runs periodicaly in System.Timers.Timer Elapsed handler
- I suspect it has something to do with GC and/or p/invoke. This is just one of my bugs:), a full description is here:
http://forums.microsoft.com/MSDN/showpost.aspx postid=337902&siteid=1

Thank you for any help,
Jan



Answer this question

exception in socket .ctor()

  • jfkrueger119584

    Aside from windows xp firewall, no. Remember that most of the time the code works fine. In most cases it is run periodicaly every 15 seconds, and the exception occurs three, four times a day.

    I did found a severe bug in the code today, but unfortunatelly it doesn't explain the constructor exception. I'll write a bit of code (or pseudocode), I'm not very good at explaining things:)

    class Ping
    {
    static readonly byte[] ReciveBuffer = new byte[1024];
    public static void Ping(string target, int packetsNo)
    {
    // preparations, preparations...
    byte[] sendBuff = SerializedICMPPacket();
    while(packetsNo-- > 0)
    {
    using (Socket socket = new Socket(adressType, socketType, protocolType)
    // ...exception sometimes
    {
    socket.SendTo(sendBuff, target);
    Array.Clear(ReceiveBuffer);
    socket.ReceiveFrom(ReceiveBuffer, target);
    //... analyze results
    }
    }
    }
    }

    class PingCaller //possible multiple instances
    {
    string target;
    int interval;
    System.Timers.Times timer = new Timer();

    public PingCaller(string target, int interval)
    {
    this.target = target;
    this.timer.interval = interval;
    }

    public StartPinging()
    {
    timer.Enabled = true;
    }

    void TimerElapsed(/* i don't remember the signature */)
    {
    timer.Enabled = false;
    Ping.Ping(this.target, 4);
    timer.Enabled = true;
    }
    }

    Well, it's obvious it's not a release version:) Anyway, I hope it describes general idea. The bug I found today is obvious: static variable ReceiveBuffer in the Ping class is not thread-safe, so I can get zeroed or worse results in the analyze-buffer step. But like I said it still does't explain the constructor exception.

    Jan

  • PeterPanTech

    Please post the stack trace and exception message received when this occurs.

  • perhelge201

    Here you are.

    Stack trace:

    System.Net.Sockets.SocketException: Probowano przeprowadzi prob na obiekcie, ktory nie jest gniazdem;
    at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType);
    at Ping.Ping.PingHost(String strPingHost, Int32 iPingBytes, UInt32 lngPingCount, UInt32 lngPingTimeout, ZSAPingNodeMessage& msgToFill);
    at SIM.Comsa2.ZSAPingNode.PingExe.RunPingExe(UInt32 timeout, String host, UInt32 packets) B d wykonania Ping.exe

    The message "Probowano przeprowadzi prob na obiekcie, ktory nie jest gniazdem" translates roughly into "socket operation on non socket"

    I don't run Ping.exe process; I create ICMP packet and send it over the socket. "Ping.exe" it's just a name for one of my components (a stupid one, I know).

    Jan


  • Mark Janecek

    Do you have any anti virus or other firewalls, filters, etc

  • exception in socket .ctor()