本文整理汇总了C#中System.Net.Sockets.Socket.GetSocketOption方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.GetSocketOption方法的具体用法?C# Socket.GetSocketOption怎么用?C# Socket.GetSocketOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.GetSocketOption方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetSocketOption
protected static int SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, int value)
{
if (((int)socket.GetSocketOption(level, name)) == value)
return value;
socket.SetSocketOption(level, name, value);
return (int)socket.GetSocketOption(level, name);
}
示例2: Connect
public void Connect(EndPoint endPoint)
{
byte[] data = new byte[1024];
string input, stringData;
int recv;
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
Console.WriteLine("Default timeout: {0}", sockopt);
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
Console.WriteLine("New timeout: {0}", sockopt);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, endPoint);
data = new byte[1024];
try
{
Console.WriteLine("Waiting from {0}:", endPoint.ToString());
recv = server.ReceiveFrom(data, ref endPoint);
Console.WriteLine("Message received from {0}:", endPoint.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.HostUnreachable)
throw new Exception("CLOSED");
throw new Exception("TIME_OUT");
}
Console.WriteLine("Stopping client");
server.Close();
}
示例3: SetSocketOption
protected static bool SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, bool value) {
if (((int)socket.GetSocketOption(level, name)) == (value ? 1 : 0))
return value;
socket.SetSocketOption(level, name, value);
if (((int)socket.GetSocketOption(level, name)) != 1) {
return false;
}
return true;
}
示例4: setup
public void setup(Socket listeningSocket)
{
/*Pre : client requests a video stream from a server
*Post: the setup process is begun*/
try
{
//gets dimensions of video player
dimensions = (int[]) referenceToView.Invoke(referenceToView.getVideoDimensions);
//creates new IPEndPoint
IPEndPoint temp = new IPEndPoint((listeningSocket.RemoteEndPoint as IPEndPoint).Address, 6666);
//stores endpoint
listenTo = temp as EndPoint;
//creates UDP socket
streamSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//bind it to any IP address on port: 6666
streamSocket.Bind(bindEP);
//probably not neccessary
streamSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, BitConverter.GetBytes(10000));
this.beginStreaming();
}
catch (SocketException se)
{
}
}
示例5: PeerCred
public PeerCred (Socket sock) {
if (sock.AddressFamily != AddressFamily.Unix) {
throw new ArgumentException ("Only Unix sockets are supported", "sock");
}
data = (PeerCredData)sock.GetSocketOption (SocketOptionLevel.Socket, (SocketOptionName)so_peercred);
}
示例6: GetSendWindow
public unsafe _RM_SEND_WINDOW GetSendWindow(Socket socket)
{
int size = sizeof(_RM_SEND_WINDOW);
byte[] data = socket.GetSocketOption(PgmSocket.PGM_LEVEL, (SocketOptionName)1001, size);
fixed (byte* pBytes = &data[0])
{
return *((_RM_SEND_WINDOW*)pBytes);
}
}
示例7: GetReceiverStats
public unsafe _RM_RECEIVER_STATS GetReceiverStats(Socket socket)
{
int size = sizeof(_RM_RECEIVER_STATS);
byte[] data = socket.GetSocketOption(PGM_LEVEL, (SocketOptionName)1013, size);
fixed (byte* pBytes = &data[0])
{
return *((_RM_RECEIVER_STATS*)pBytes);
}
}
示例8: NetworkStream
//
// Summary:
// Initializes a new instance of the System.Net.Sockets.NetworkStream class
// for the specified System.Net.Sockets.Socket with the specified System.Net.Sockets.Socket
// ownership.
//
// Parameters:
// ownsSocket:
// true to indicate that the System.Net.Sockets.NetworkStream will take ownership
// of the System.Net.Sockets.Socket; otherwise, false.
//
// socket:
// The System.Net.Sockets.Socket that the System.Net.Sockets.NetworkStream will
// use to send and receive data.
//
// Exceptions:
// System.IO.IOException:
// socket is not connected.-or- The value of the System.Net.Sockets.Socket.SocketType
// property of socket is not System.Net.Sockets.SocketType.Stream.-or- socket
// is in a nonblocking state.
//
// System.ArgumentNullException:
// socket is null.
public NetworkStream(Socket socket, bool ownsSocket)
{
if (socket == null) throw new ArgumentNullException();
// This should throw a SocketException if not connected
try
{
_remoteEndPoint = socket.RemoteEndPoint;
}
catch (Exception e)
{
int errCode = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
throw new IOException(errCode.ToString(), e);
}
// Set the internal socket
_socket = socket;
_socketType = (int)_socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type);
_ownsSocket = ownsSocket;
}
示例9: GetSocketOption
[Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
public void GetSocketOption3_Socket_Closed ()
{
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Close ();
try {
s.GetSocketOption (0, 0, 0);
Assert.Fail ("#1");
} catch (ObjectDisposedException ex) {
// Cannot access a disposed object
Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
}
}
示例10: DiscoverDevices
/// <summary>
/// Obtains information about available devices using a socket.
/// </summary>
/// <param name="maxDevices">The maximum number of devices to get information about.</param>
/// <param name="irdaSocket"></param>
/// <returns></returns>
public static IrDADeviceInfo[] DiscoverDevices(int maxDevices, Socket irdaSocket)
{
if (irdaSocket == null) {
throw new ArgumentNullException("irdaSocket");
}
const int MaxItemsInHugeBuffer = (Int32.MaxValue - 4) / 32;
if (maxDevices > MaxItemsInHugeBuffer || maxDevices < 0) {
throw new ArgumentOutOfRangeException("maxDevices");
}
//
byte[] buffer = irdaSocket.GetSocketOption(IrDASocketOptionLevel.IrLmp, IrDASocketOptionName.EnumDevice, 4 + (maxDevices * 32));
return ParseDeviceList(buffer);
}
示例11: MCCommReceive
void MCCommReceive()
{
var receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
receiveSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false);
receiveSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
// receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, false);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, multicastPort);
receiveSocket.Bind(localEP);
var rbuf = receiveSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
byte[] multicastAddrBytes = multicastAddr.GetAddressBytes();
byte[] ipAddrBytes = IPAddress.Any.GetAddressBytes();
byte[] multicastOpt = new byte[]
{
multicastAddrBytes[0], multicastAddrBytes[1], multicastAddrBytes[2], multicastAddrBytes[3], // WsDiscovery Multicast Address: 239.255.255.250
ipAddrBytes [0], ipAddrBytes [1], ipAddrBytes [2], ipAddrBytes [3]
};
receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOpt);
// mySocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ipAddrBytes);
EndPoint senderEP = new IPEndPoint(multicastAddr, multicastPort);
int len = 0;
byte[] dataBytes = new byte[RECEIVEBUFSIZE];
bool mcgJoining = false;
lock (this)
{
joinFlag = true;
mcgJoining = joinFlag;
}
while (mcgJoining)
{
try
{
len = receiveSocket.Receive(dataBytes, dataBytes.Length, SocketFlags.None);
if (dataBytes[0] == 0xff)
{
if (dataBytes[1] == 0xfa)
{
var ackMsgId = new byte[len - 2];
Array.Copy(dataBytes, 2, ackMsgId, 0, len - 2);
ackWaiting.Set();
}
}
else
{
len = ((int)(dataBytes[0]) << 8) + dataBytes[1];
byte[] buf = new byte[len];
for (int i = 0; i < len; i++)
{
buf[i] = dataBytes[i + 2];
}
if (OnMulticastMessageReceived != null)
{
OnMulticastMessageReceived(buf, len, (IPEndPoint)senderEP);
}
}
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
lock (this)
{
mcgJoining = joinFlag;
}
}
receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, multicastOpt);
}
示例12: SocketTest1_TCP_SendTimeout
public MFTestResults SocketTest1_TCP_SendTimeout()
{
///<summary>
///1. Starts a server socket listening for TCP
///2. Starts a client socket connected to the server
///3. Verifies that data can be correctly sent and recieved
///4. Verifies Send timeout is functional
///</summary>
///skip this test for the emulator since setting socket options is not supported.
if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
return MFTestResults.Skip;
MFTestResults testResult = MFTestResults.Fail;
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
int sendTimeout = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
Log.Comment("Get the SendTimeout size on the server = " + sendTimeout);
int newTimeout = 1000;
Log.Comment("Set the receiveTimeout size on the server = " + newTimeout);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, newTimeout);
int sendTimeout2 = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
Log.Comment("The new SendTimeout size on the server=" + sendTimeout2);
Log.Comment("Testing with port 0");
client.Bind(new IPEndPoint(IPAddress.Loopback, 0));
server.Bind(new IPEndPoint(IPAddress.Loopback, 0));
IPEndPoint epClient = (IPEndPoint)client.LocalEndPoint;
IPEndPoint epServer = (IPEndPoint)server.LocalEndPoint;
server.Listen(1);
client.Connect(epServer);
using (Socket sock = server.Accept())
{
byte[] recBytes = new byte[12000];
Log.Comment("Send to a closed server to cause a timeout.");
try
{
for( int i=0; i<1000; ++i)
{
int bytesSent = client.Send(recBytes);
}
Log.Comment("Receiving bytes.");
testResult = MFTestResults.Fail;
}
catch (SocketException e)
{
Log.Comment("correctly threw exception after Send Timeout on server: " + e);
testResult = MFTestResults.Pass;
}
catch (Exception e)
{
Log.Comment("incorrectly threw exception: " + e);
testResult = MFTestResults.Fail;
}
}
}
catch (SocketException e)
{
if (e.ErrorCode == (int)SocketError.ProtocolOption)
{
testResult = MFTestResults.Skip;
}
else
{
Log.Comment("Caught exception: " + e.Message);
Log.Comment("ErrorCode: " + e.ErrorCode.ToString());
testResult = MFTestResults.Fail;
}
}
catch (Exception e)
{
Log.Comment("Caught exception: " + e.Message);
testResult = MFTestResults.Fail;
}
finally
{
server.Close();
client.Close();
}
return testResult;
}
示例13: getSendBufferSize
public static int getSendBufferSize(Socket socket)
{
int sz;
try
{
sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
}
catch(SocketException ex)
{
closeSocketNoThrow(socket);
throw new Ice.SocketException(ex);
}
return sz;
}
示例14: ProcessConnect
protected void ProcessConnect(Socket socket, object state, SocketAsyncEventArgs e)
{
if (e != null && e.SocketError != SocketError.Success)
{
e.Dispose();
m_InConnecting = false;
OnError(new SocketException((int)e.SocketError));
return;
}
if (socket == null)
{
m_InConnecting = false;
OnError(new SocketException((int)SocketError.ConnectionAborted));
return;
}
//To walk around a MonoTouch's issue
//one user reported in some cases the e.SocketError = SocketError.Succes but the socket is not connected in MonoTouch
if (!socket.Connected)
{
m_InConnecting = false;
#if SILVERLIGHT || NETFX_CORE
var socketError = SocketError.ConnectionReset;
#else
var socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
#endif
OnError(new SocketException((int)socketError));
return;
}
if (e == null)
e = new SocketAsyncEventArgs();
e.Completed += SocketEventArgsCompleted;
Client = socket;
m_InConnecting = false;
#if !SILVERLIGHT && !NETFX_CORE
try
{
//Set keep alive
Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}
catch
{
}
#endif
OnGetSocket(e);
}
示例15: CheckError
private void CheckError(int sentBytes, int length, Socket socket)
{
// if (sentBytes != length)
// _logger.Warn("Not all bytes sent");
var socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
if (socketError != SocketError.Success)
{
_logger.ErrorFormat("Error on send : {0}", socketError);
}
}