本文整理汇总了C#中System.Net.Sockets.Socket.IOControl方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.IOControl方法的具体用法?C# Socket.IOControl怎么用?C# Socket.IOControl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.IOControl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
/// <summary>
/// Starts to listen
/// </summary>
/// <param name="config">The server config.</param>
/// <returns></returns>
public override bool Start(IServerConfig config)
{
try
{
m_ListenSocket = new Socket(this.EndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
m_ListenSocket.Bind(this.EndPoint);
uint IOC_IN = 0x80000000;
uint IOC_VENDOR = 0x18000000;
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
byte[] optionInValue = { Convert.ToByte(false) };
byte[] optionOutValue = new byte[4];
m_ListenSocket.IOControl((int)SIO_UDP_CONNRESET, optionInValue, optionOutValue);
var eventArgs = new SocketAsyncEventArgs();
eventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(eventArgs_Completed);
eventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
int receiveBufferSize = config.ReceiveBufferSize <= 0 ? 2048 : config.ReceiveBufferSize;
var buffer = new byte[receiveBufferSize];
eventArgs.SetBuffer(buffer, 0, buffer.Length);
m_ListenSocket.ReceiveFromAsync(eventArgs);
return true;
}
catch (Exception e)
{
OnError(e);
return false;
}
}
示例2: SslHelper
public SslHelper(Socket socket, string host)
{
//The managed SocketOptionName enum doesn't have SO_SECURE so here we cast the integer value
socket.SetSocketOption(SocketOptionLevel.Socket, (SocketOptionName)SO_SECURE, SO_SEC_SSL);
//We need to pass a function pointer and a pointer to a string containing the host
//to unmanaged code
hookFunc = Marshal.GetFunctionPointerForDelegate(new SSLVALIDATECERTFUNC(ValidateCert));
//Allocate the buffer for the string
ptrHost = Marshal.AllocHGlobal(host.Length + 1);
WriteASCIIString(ptrHost, host);
//Now put both pointers into a byte[]
var inBuffer = new byte[8];
var hookFuncBytes = BitConverter.GetBytes(hookFunc.ToInt32());
var hostPtrBytes = BitConverter.GetBytes(ptrHost.ToInt32());
Array.Copy(hookFuncBytes, inBuffer, hookFuncBytes.Length);
Array.Copy(hostPtrBytes, 0, inBuffer, hookFuncBytes.Length, hostPtrBytes.Length);
unchecked
{
socket.IOControl((int)SO_SSL_SET_VALIDATE_CERT_HOOK, inBuffer, null);
}
}
示例3: Initialize
public static void Initialize()
{
try
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.IOControl(IOControlCode.KeepAliveValues, null, null);
SupportSocketIOControlByCodeEnum = true;
IsMono = false;
}
catch (NotSupportedException)
{
SupportSocketIOControlByCodeEnum = false;
}
catch (NotImplementedException)
{
SupportSocketIOControlByCodeEnum = false;
}
catch (Exception)
{
SupportSocketIOControlByCodeEnum = true;
}
Type t = Type.GetType("Mono.Runtime");
IsMono = t != null;
}
示例4: RunReciever
public void RunReciever()
{
ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
try
{
// Setup the Socket
ListenSocket.Bind(new IPEndPoint(IPAddress.Parse(p_HostIP), 0));
ListenSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
ListenSocket.IOControl(unchecked((int) 0x98000001), new byte[4] {1, 0, 0, 0}, new byte[4]);
while (true) //Infinite Loop keeps the Socket in Listen
{
ListenSocket.BeginReceive(p_PacketBuffer, 0, p_PacketBufferSize, SocketFlags.None,
new AsyncCallback(CallReceive), this);
while (ListenSocket.Available == 0) // If no Data Sleep the thread for 1ms then check to see if there is data to be read
{
Thread.Sleep(1);
}
}
}
catch (ThreadAbortException){}// Catch the ThreadAbort Exception that gets generated whenever a thread is closed with the Thread.Abort() method
catch (Exception e) {new ErrorHandle(e);}
finally //Shutdown the Socket when finished
{
if (ListenSocket != null)
{
ListenSocket.Shutdown(SocketShutdown.Both);
ListenSocket.Close();
}
}
}
示例5: init
public bool init()
{
bool ret = true;
try{
m_socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
uint IOC_IN = 0x80000000;
uint IOC_VENDOR = 0x18000000;
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
m_socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
EndPoint localEP = new IPEndPoint(IPAddress.Parse(m_ip), m_port);
m_socket.Bind(localEP);
ret = true;
initialized = true;
destroyed = false;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
ret = false;
initialized = false;
}
return ret;
}
示例6: Start
/// <summary>
/// Starts to listen
/// </summary>
/// <param name="config">The server config.</param>
/// <returns></returns>
public override bool Start(IServerConfig config)
{
try
{
m_ListenSocket = new Socket(this.EndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
m_ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_ListenSocket.Bind(this.EndPoint);
//Mono doesn't support it
if (Platform.SupportSocketIOControlByCodeEnum)
{
uint IOC_IN = 0x80000000;
uint IOC_VENDOR = 0x18000000;
uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
byte[] optionInValue = { Convert.ToByte(false) };
byte[] optionOutValue = new byte[4];
m_ListenSocket.IOControl((int)SIO_UDP_CONNRESET, optionInValue, optionOutValue);
}
var eventArgs = m_SaePool.Get();
m_ListenSocket.ReceiveFromAsync(eventArgs);
return true;
}
catch (Exception e)
{
OnError(e);
return false;
}
}
示例7: checkBox1_CheckedChanged
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if ((sender as CheckBox).Checked)
{
(sender as CheckBox).Text = "&Stop me";
socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
try
{
socket.Bind(new IPEndPoint(IPAddress.Parse(comboBox1.SelectedItem.ToString()), 0));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
byte[] byInc = new byte[] { 1, 0, 0, 0 };
byte[] byOut = new byte[4];
buffer = new byte[4096];
socket.IOControl(IOControlCode.ReceiveAll, byInc, byOut);
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnReceive, null);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
else
{
socket.Close();
(sender as CheckBox).Text = "&Start me";
}
}
示例8: RegisterSession
protected ISocketSession RegisterSession(Socket client, ISocketSession session)
{
if (m_SendTimeOut > 0)
client.SendTimeout = m_SendTimeOut;
if (m_ReceiveBufferSize > 0)
client.ReceiveBufferSize = m_ReceiveBufferSize;
if (m_SendBufferSize > 0)
client.SendBufferSize = m_SendBufferSize;
if(!Platform.SupportSocketIOControlByCodeEnum)
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
else
client.IOControl(IOControlCode.KeepAliveValues, m_KeepAliveOptionValues, null);
client.NoDelay = true;
client.UseOnlyOverlappedIO = true;
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
IAppSession appSession = this.AppServer.CreateAppSession(session);
if (appSession == null)
return null;
return session;
}
示例9: btnStart_Click
private void btnStart_Click(object sender, EventArgs e)
{
if (cmbInterfaces.Text == "")
{
MessageBox.Show("Select an Interface to capture the packets.", "Omkar_sniffer",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
if (!bContinueCapturing)
{
//Start capturing the packets...
btnStart.Text = "&Stop";
bContinueCapturing = true;
//For sniffing the socket to capture the packets has to be a raw socket, with the
//address family being of type internetwork, and protocol being IP
mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.IP);
//Bind the socket to the selected IP address
mainSocket.Bind(new IPEndPoint(IPAddress.Parse(cmbInterfaces.Text), 0));
//Set the socket options
mainSocket.SetSocketOption(SocketOptionLevel.IP, //Applies only to IP packets
SocketOptionName.HeaderIncluded, //Set the include the header
true); //option to true
byte[] byTrue = new byte[4] {1, 0, 0, 0};
byte[] byOut = new byte[4]{1, 0, 0, 0}; //Capture outgoing packets
//Socket.IOControl is analogous to the WSAIoctl method of Winsock 2
mainSocket.IOControl(IOControlCode.ReceiveAll, //Equivalent to SIO_RCVALL constant
//of Winsock 2
byTrue,
byOut);
//Start receiving the packets asynchronously
mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(OnReceive), null);
}
else
{
btnStart.Text = "&Start";
bContinueCapturing = false;
//To stop capturing the packets close the socket
mainSocket.Close ();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Omkar_sniffer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
示例10: create
public void create(List<string> ipv4)
{
if (ipv4 != null && ipv4.Count > 0) {
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
for(int i=0; i<ipv4.Count; i++) mSocket.Bind(new IPEndPoint(IPAddress.Parse(ipv4[i]), 0));
mSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
byte[] byTrue = new byte[4] { 1, 0, 0, 0 }, byOut = new byte[4] { 1, 0, 0, 0 };
mSocket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
mSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
}
示例11: Init
private void Init()
{
Debug.Assert(_socket == null);
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
if (_localIp != null)
_socket.Bind(new IPEndPoint(_localIp, 0));
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
var receiveAllOn = BitConverter.GetBytes(1);
_socket.IOControl(IOControlCode.ReceiveAll, receiveAllOn, null);
Read();
}
示例12: SnifferBase
public SnifferBase(IPAddress bindTo)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
_socket .Bind(new IPEndPoint(bindTo, 0));
_socket .SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); //option to true
var byTrue = new byte[] {0x1, 0x0, 0x0, 0x0};
var byOut = new byte[] {0x1, 0x0, 0x0, 0x0};
_socket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
}
示例13: CreateIcmpSocket
private void CreateIcmpSocket(IPEndPoint endPoint)
{
icmpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
LocalEndPoint = endPoint;
icmpSocket.Bind(endPoint);
PlatformID p = Environment.OSVersion.Platform;
if (p == PlatformID.Win32NT && !endPoint.Address.Equals(IPAddress.Any))
{
icmpSocket.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, new byte[] { 1, 0, 0, 0 });
}
BeginReceiveFrom();
}
示例14: SetClient
public static void SetClient(Socket client)
{
//client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
//client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, m_receiveBufferSize);
//client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, m_sendBufferSize);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
#if mono
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
#else
client.IOControl(IOControlCode.KeepAliveValues, KeepValue, null);
#endif
}
示例15: Start
/// <summary>
/// Starts listening on the specified interface.
/// </summary>
/// <exception cref="SocketException">An error occurs when trying to intercept IP packets.</exception>
public void Start() {
if (m_Monitor == null) {
try {
m_Monitor = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
m_Monitor.Bind(new IPEndPoint(IP, 0));
m_Monitor.IOControl(SIO_RCVALL, BitConverter.GetBytes((int)1), null);
m_Monitor.BeginReceive(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), null);
} catch {
m_Monitor = null;
throw new SocketException();
}
}
}