本文整理汇总了C#中Socket.SetSocketOption方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.SetSocketOption方法的具体用法?C# Socket.SetSocketOption怎么用?C# Socket.SetSocketOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.SetSocketOption方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main (string [] args)
{
int port;
Random random = new Random ();
Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
do {
port = random.Next (0xc350, 0xffdc);
IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
try {
socket.Bind (localEP);
break;
} catch {
}
} while (true);
socket.Close ();
IPEndPoint LocalEP = new IPEndPoint (IPAddress.Loopback, port);
Socket ListeningSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
ListeningSocket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
ListeningSocket.Bind (LocalEP);
ListeningSocket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption (IPAddress.Parse ("239.255.255.250"), IPAddress.Loopback));
ListeningSocket.Close ();
}
示例2: resetSocket
void resetSocket()
{
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//设置该scoket实例的发送形式
mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 3000);
mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
示例3: query
public void query(zzHostInfo pHostInfo)
{
Socket lSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
lSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
lSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
lSocket.Bind(new IPEndPoint(IPAddress.Any, pHostInfo.port));
Query(stunServerAddress, stunServerPort, lSocket);
}
示例4: ReuseUnicastPort_CreateSocketSetOption_NoSocketsReuseUnicastPortSupport_Throws
public void ReuseUnicastPort_CreateSocketSetOption_NoSocketsReuseUnicastPortSupport_Throws()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Throws<SocketException>(() =>
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
}
else
{
Assert.Throws<PlatformNotSupportedException>(() =>
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
}
}
示例5: Awake
void Awake()
{
//=======================================================
// Socket create.
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000);
m_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
//=======================================================
// Socket connect.
try
{
IPAddress ipAddr = System.Net.IPAddress.Parse(iPAdress);
IPEndPoint ipEndPoint = new System.Net.IPEndPoint(ipAddr, kPort);
m_Socket.Connect(ipEndPoint);
}
catch (SocketException SCE)
{
Debug.Log("Socket connect error! : " + SCE.ToString() );
return;
}
//=======================================================
// Send data write.
StringBuilder sb = new StringBuilder(); // String Builder Create
sb.Append("Test 1 - By Mac!!");
sb.Append("Test 2 - By Mac!!");
try
{
//=======================================================
// Send.
SenddataLength = Encoding.Default.GetByteCount(sb.ToString());
Sendbyte = Encoding.Default.GetBytes(sb.ToString());
m_Socket.Send(Sendbyte, Sendbyte.Length, 0);
//=======================================================
// Receive.
m_Socket.Receive(Receivebyte);
ReceiveString = Encoding.Default.GetString(Receivebyte);
ReceivedataLength = Encoding.Default.GetByteCount(ReceiveString.ToString());
Debug.Log("Receive Data : " + ReceiveString + "(" + ReceivedataLength + ")");
}
catch (SocketException err)
{
Debug.Log("Socket send or receive error! : " + err.ToString() );
}
}
示例6: Success
public void Success()
{
if (Socket.OSSupportsIPv4)
{
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
IPPacketInformation packetInformation;
SocketFlags flags = SocketFlags.None;
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
int len = receiver.ReceiveMessageFrom(new byte[1024], 0, 1024, ref flags, ref remoteEP, out packetInformation);
Assert.Equal(1024, len);
Assert.Equal(sender.LocalEndPoint, remoteEP);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, packetInformation.Address);
sender.Dispose();
}
}
}
示例7: ClientToClient
public ClientToClient()
{
_connection = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_connection.ExclusiveAddressUse = false;
_connection.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_receiveBuffer = new byte[_receiveBufferSize];
}
示例8: Success
public void Success()
{
ManualResetEvent completed = new ManualResetEvent(false);
if (Socket.OSSupportsIPv4)
{
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
args.SetBuffer(new byte[1024], 0, 1024);
args.Completed += OnCompleted;
args.UserToken = completed;
Assert.True(receiver.ReceiveMessageFromAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");
Assert.Equal(1024, args.BytesTransferred);
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
sender.Dispose();
}
}
}
示例9: ReuseUnicastPort_CreateSocketSetOption_NoSocketsReuseUnicastPortSupport_Throws
public void ReuseUnicastPort_CreateSocketSetOption_NoSocketsReuseUnicastPortSupport_Throws()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Assert.Throws<SocketException>(() =>
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1));
}
示例10: ReuseUnicastPort_CreateSocketSetOptionToOneAndGetOption_SocketsReuseUnicastPortSupport_OptionIsOne
public void ReuseUnicastPort_CreateSocketSetOptionToOneAndGetOption_SocketsReuseUnicastPortSupport_OptionIsOne()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort, 1);
int optionValue = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort);
Assert.Equal(1, optionValue);
}
示例11: Main
static void Main ()
{
IPAddress mcastAddress = IPAddress.Parse ("FF01::1");
IPEndPoint bindEp = new IPEndPoint (IPAddress.IPv6Any, 555);
Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
s.Bind (bindEp);
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, 0);
s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (mcastAddress));
s.Connect (new IPEndPoint (mcastAddress, 4567));
for (int i = 0; i < 5; i++) {
s.Send (Encoding.ASCII.GetBytes ("hello"));
Thread.Sleep (500);
}
}
示例12: Main
static int Main ()
{
IPAddress mcastAddress = IPAddress.Parse ("FF01::1");
IPEndPoint bindEp = new IPEndPoint (IPAddress.IPv6Any, 4567);
Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
s.Bind (bindEp);
s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption (mcastAddress));
byte [] buffer = new byte [10];
int count = s.Receive (buffer);
if (count != 5)
return 1;
if (Encoding.ASCII.GetString (buffer, 0, count) != "hello")
return 2;
return 0;
}
示例13: SetupListening
static Socket SetupListening(int listeningPort)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var endPoint = new IPEndPoint(IPAddress.Any, listeningPort);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.Blocking = false;
socket.Bind (endPoint);
socket.Listen (32);
return socket;
}
示例14: Main
static int Main ()
{
// Opening socket like discovery does.
socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
socket.Bind (new IPEndPoint (IPAddress.Any, 2917));
socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption (IPAddress.Parse ("224.4.0.1"), IPAddress.Any));
Thread socketThread = new Thread (new ThreadStart (SocketReceive));
socketThread.Start ();
socketThreadRunning = true;
Thread.Sleep (50); // Give the thread a moment to be in it's socket.Receive() call.
socket.Close ();
Thread.Sleep (50); // Give it a moment to end.
if (socketThreadRunning)
return 1;
return 0;
}
示例15: ReceiveData
// -------------------------------------------------------------------------
void ReceiveData()
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, multicastPort);
EndPoint wtf = (EndPoint) anyIP;
try {
Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse(multicastIP)));
udpSocket.Bind(anyIP);
while (Thread.CurrentThread.IsAlive)
{
if (udpSocket.Poll(1, SelectMode.SelectRead)) {
dataMutex.WaitOne();
byte[] buffer = new byte[1500];
int trueSize = udpSocket.ReceiveFrom(buffer, ref wtf);
byte[] trimmedBuffer = new byte[trueSize];
Array.Copy(buffer, trimmedBuffer, trueSize);
dataList.Add(trimmedBuffer);
dataMutex.ReleaseMutex();
}
Thread.Sleep(10);
}
} catch(Exception e) {
print(e.ToString());
}
}