C# Socket Exception : an attempt was made to access a socket in a way forbidden by its access permissions -
this code using. code got internet, said working fine. comments don't understand why not working me. , 1 more thing using application user mode not in administrator mode.
private void btnstart_click(object sender, eventargs e) { if (cmbinterfaces.text == "") { messagebox.show("select interface capture packets.", "mjsniffer", messageboxbuttons.ok, messageboxicon.error); return; } try { if (!bcontinuecapturing) { //start capturing packets... btnstart.text = "&stop"; bcontinuecapturing = true; //for sniffing socket capture packets has raw socket, //address family being of type internetwork, , protocol being ip console.writeline("1"); mainsocket = new socket(addressfamily.internetwork, sockettype.raw, protocoltype.ip); console.writeline("2"); //bind socket selected ip address mainsocket.bind(new ipendpoint(ipaddress.parse(cmbinterfaces.text), 0)); console.writeline("3"); //set socket options mainsocket.setsocketoption(socketoptionlevel.ip, //applies ip packets socketoptionname.headerincluded, //set include header true); //option true console.writeline("4"); byte[] bytrue = new byte[4] {1, 0, 0, 0}; byte[] byout = new byte[4]{1, 0, 0, 0}; //capture outgoing packets //socket.iocontrol analogous wsaioctl method of winsock 2 mainsocket.iocontrol(iocontrolcode.receiveall, //equivalent sio_rcvall constant //of winsock 2 bytrue, byout); //start receiving packets asynchronously mainsocket.beginreceive(bytedata, 0, bytedata.length, socketflags.none, new asynccallback(onreceive), null); } else { btnstart.text = "&start"; bcontinuecapturing = false; //to stop capturing packets close socket mainsocket.close (); } } catch (socketexception ex) { console.writeline("5"); messagebox.show(ex.message, "mjsniffer", messageboxbuttons.ok, messageboxicon.error); } catch (exception ex) { console.writeline("6"); messagebox.show(ex.message, "mjsniffer", messageboxbuttons.ok, messageboxicon.error); } }
and 1 more thing using application user mode not in administrator mode.
this can't work. following written win32 api, since that's .net calls down into, same applies:
to use socket of type
sock_raw
requires administrative privileges. users running winsock applications use raw sockets must member of administrators group on local computer, otherwise raw socket calls fail error code ofwsaeacces
. on windows vista , later, access raw sockets enforced @ socket creation. in earlier versions of windows, access raw sockets enforced during other socket operations.
(my emphasis)
Comments
Post a Comment