Skip to content

Commit 2a6ecce

Browse files
authored
Socket Option overload example clarification (#8616)
2 parents 6d4783a + a618f8b commit 2a6ecce

File tree

4 files changed

+140
-138
lines changed
  • snippets
    • cpp/VS_Snippets_Remoting/Socket_Socket_Options/CPP
    • csharp/System.Net.Sockets/LingerOption/LingerTime
    • visualbasic/VS_Snippets_Remoting/Socket_Socket_Options/VB
  • xml/System.Net.Sockets

4 files changed

+140
-138
lines changed

snippets/cpp/VS_Snippets_Remoting/Socket_Socket_Options/CPP/source.cpp

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,95 +10,96 @@ using namespace System::Threading;
1010
public ref class Sync_Send_Receive
1111
{
1212
public:
13-
static void SetSocketOptions()
14-
{
15-
IPHostEntry^ lipa = Dns::Resolve( "host.contoso.com" );
16-
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );
17-
Socket^ s = gcnew Socket( lep->Address->AddressFamily, SocketType::Stream,ProtocolType::Tcp );
18-
19-
//<Snippet1>
20-
// Specifies that send operations will time-out
21-
// if confirmation is not received within 1000 milliseconds.
22-
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000 );
23-
24-
// Specifies that the Socket will linger for 10 seconds after Close is called.
25-
LingerOption^ lingerOption = gcnew LingerOption( true,10 );
26-
27-
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption );
28-
//</Snippet1>
29-
30-
s->Connect( lep );
31-
32-
array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
33-
34-
//<Snippet2>
35-
Console::Write( "This application will timeout if Send does not return within " );
36-
Console::WriteLine( Encoding::ASCII->GetString( s->GetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4 ) ) );
37-
38-
// Blocks until send returns.
39-
int i = s->Send( msg );
40-
41-
// Blocks until read returns.
42-
array<Byte>^ bytes = gcnew array<Byte>(1024);
43-
44-
s->Receive( bytes );
45-
46-
//Displays to the screen.
47-
Console::WriteLine( Encoding::ASCII->GetString( bytes ) );
48-
s->Shutdown( SocketShutdown::Both );
49-
Console::Write( "If data remains to be sent, this application will stay open for " );
50-
Console::WriteLine( safe_cast<LingerOption^>(s->GetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger ))->LingerTime.ToString() );
51-
s->Close();
52-
//</Snippet2>
53-
}
54-
55-
static void CheckProperties()
56-
{
57-
IPHostEntry^ lipa = Dns::Resolve( "host.contoso.com" );
58-
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );
59-
60-
//<Snippet3>
61-
Socket^ s = gcnew Socket( lep->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
62-
63-
//Uses the AddressFamily, SocketType, and ProtocolType properties.
64-
Console::Write( "I just set the following properties of socket: \n" );
65-
Console::Write( "Address Family = {0}", s->AddressFamily.ToString() );
66-
Console::Write( "\nSocketType = {0}", s->SocketType.ToString() );
67-
Console::WriteLine( "\nProtocolType = {0}", s->ProtocolType.ToString() );
68-
//</Snippet3>
69-
70-
//<Snippet4>
71-
s->Connect( lep );
72-
73-
// Uses the RemoteEndPoint property.
74-
Console::WriteLine( "I am connected to {0} on port number {1}",
75-
IPAddress::Parse( ( ( (IPEndPoint^)(s->RemoteEndPoint) )->Address)->ToString() ),
76-
( (IPEndPoint^)(s->RemoteEndPoint) )->Port.ToString() );
77-
78-
// Uses the LocalEndPoint property.
79-
Console::Write( "My local IpAddress is : {0}\nI am connected on port number {1}",
80-
IPAddress::Parse( ( ( (IPEndPoint^)(s->LocalEndPoint) )->Address)->ToString() ),
81-
( (IPEndPoint^)(s->LocalEndPoint) )->Port.ToString() );
82-
//</Snippet4>
83-
84-
//<Snippet5>
85-
//Uses low level method IOControl to set this socket to blocking mode.
86-
int code = 0x8004667E;
87-
array<Byte>^ inBuf = gcnew array<Byte>(4);
88-
89-
inBuf[ 0 ] = 0;
90-
91-
array<Byte>^ outBuf = gcnew array<Byte>(4);
92-
93-
s->IOControl( code, inBuf, outBuf );
94-
95-
//Checks to see that this worked.
96-
if ( s->Blocking )
97-
{
98-
Console::WriteLine( "Socket was set to Blocking mode successfully" );
99-
}
100-
//</Snippet5>
101-
}
13+
static void SetSocketOptions()
14+
{
15+
IPHostEntry^ lipa = Dns::Resolve("host.contoso.com");
16+
IPEndPoint^ lep = gcnew IPEndPoint(lipa->AddressList[0], 11000);
17+
Socket^ s = gcnew Socket(lep->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp);
18+
19+
//<Snippet0>
20+
// Specifies that send operations will time-out
21+
// if confirmation is not received within 1000 milliseconds.
22+
s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000);
23+
//</Snippet0>
24+
25+
//<Snippet1>
26+
// Specifies that the Socket will linger for 10 seconds after Close is called.
27+
LingerOption^ lingerOption = gcnew LingerOption(true, 10);
28+
s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption);
29+
//</Snippet1>
30+
31+
s->Connect(lep);
32+
33+
array<Byte>^ msg = Encoding::ASCII->GetBytes("This is a test");
34+
35+
//<Snippet2>
36+
Console::Write("This application will timeout if Send does not return within ");
37+
Console::WriteLine(Encoding::ASCII->GetString(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 4)));
38+
39+
// Blocks until send returns.
40+
int i = s->Send(msg);
41+
42+
// Blocks until read returns.
43+
array<Byte>^ bytes = gcnew array<Byte>(1024);
44+
45+
s->Receive(bytes);
46+
47+
// Displays to the screen.
48+
Console::WriteLine(Encoding::ASCII->GetString(bytes));
49+
s->Shutdown(SocketShutdown::Both);
50+
Console::Write("If data remains to be sent, this application will stay open for ");
51+
Console::WriteLine(safe_cast<LingerOption^>(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger))->LingerTime.ToString());
52+
s->Close();
53+
//</Snippet2>
54+
}
55+
56+
static void CheckProperties()
57+
{
58+
IPHostEntry^ lipa = Dns::Resolve("host.contoso.com");
59+
IPEndPoint^ lep = gcnew IPEndPoint(lipa->AddressList[0], 11000);
60+
61+
//<Snippet3>
62+
Socket^ s = gcnew Socket(lep->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp);
63+
64+
// Uses the AddressFamily, SocketType, and ProtocolType properties.
65+
Console::Write("I just set the following properties of socket: \n");
66+
Console::Write("Address Family = {0}", s->AddressFamily.ToString());
67+
Console::Write("\nSocketType = {0}", s->SocketType.ToString());
68+
Console::WriteLine("\nProtocolType = {0}", s->ProtocolType.ToString());
69+
//</Snippet3>
70+
71+
//<Snippet4>
72+
s->Connect(lep);
73+
74+
// Uses the RemoteEndPoint property.
75+
Console::WriteLine("I am connected to {0} on port number {1}",
76+
IPAddress::Parse((((IPEndPoint^)(s->RemoteEndPoint))->Address)->ToString()),
77+
((IPEndPoint^)(s->RemoteEndPoint))->Port.ToString());
78+
79+
// Uses the LocalEndPoint property.
80+
Console::Write("My local IpAddress is : {0}\nI am connected on port number {1}",
81+
IPAddress::Parse((((IPEndPoint^)(s->LocalEndPoint))->Address)->ToString()),
82+
((IPEndPoint^)(s->LocalEndPoint))->Port.ToString());
83+
//</Snippet4>
84+
85+
//<Snippet5>
86+
// Uses low level method IOControl to set this socket to blocking mode.
87+
int code = 0x8004667E;
88+
array<Byte>^ inBuf = gcnew array<Byte>(4);
89+
90+
inBuf[0] = 0;
91+
92+
array<Byte>^ outBuf = gcnew array<Byte>(4);
93+
94+
s->IOControl(code, inBuf, outBuf);
95+
96+
// Checks to see that this worked.
97+
if (s->Blocking)
98+
{
99+
Console::WriteLine("Socket was set to Blocking mode successfully");
100+
}
101+
//</Snippet5>
102+
}
102103
};
103104

104-
int main(){}
105+
int main() {}

snippets/csharp/System.Net.Sockets/LingerOption/LingerTime/source.cs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,88 +7,88 @@
77

88
public class Sync_Send_Receive
99
{
10-
public static void SetSocketOptions ()
10+
public static void SetSocketOptions()
1111
{
12-
IPHostEntry lipa = Dns.Resolve ("host.contoso.com");
13-
IPEndPoint lep = new IPEndPoint (lipa.AddressList[0], 11000);
14-
Socket s = new Socket (lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
15-
16-
//<Snippet1>
12+
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
13+
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);
14+
Socket s = new Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
15+
//<Snippet0>
1716
// Send operations will time-out if confirmation
1817
// is not received within 1000 milliseconds.
19-
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
18+
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
19+
//</Snippet0>
2020

21+
//<Snippet1>
2122
// The socket will linger for 10 seconds after Socket.Close is called.
22-
LingerOption lingerOption = new LingerOption (true, 10);
23-
24-
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
23+
var lingerOption = new LingerOption(true, 10);
24+
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
25+
//</Snippet1>
2526

26-
//</Snippet1>
27-
s.Connect (lep);
27+
s.Connect(lep);
2828

29-
byte[] msg = Encoding.ASCII.GetBytes ("This is a test");
29+
byte[] msg = Encoding.ASCII.GetBytes("This is a test");
3030

31-
//<Snippet2>
32-
Console.WriteLine ("This application will timeout if Send does not return within " + Encoding.ASCII.GetString (s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));
31+
//<Snippet2>
32+
Console.WriteLine("This application will timeout if Send does not return within " + Encoding.ASCII.GetString(s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 4)));
3333

3434
// blocks until send returns
35-
int i = s.Send (msg);
35+
int i = s.Send(msg);
3636

3737
// blocks until read returns
3838
byte[] bytes = new byte[1024];
3939

40-
s.Receive (bytes);
40+
s.Receive(bytes);
4141

42-
//Display to the screen
43-
Console.WriteLine (Encoding.ASCII.GetString (bytes));
44-
s.Shutdown (SocketShutdown.Both);
45-
Console.WriteLine ("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString ());
46-
s.Close ();
47-
//</Snippet2>
42+
// Display to the screen
43+
Console.WriteLine(Encoding.ASCII.GetString(bytes));
44+
s.Shutdown(SocketShutdown.Both);
45+
Console.WriteLine("If data remains to be sent, this application will stay open for " + ((LingerOption)s.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger)).LingerTime.ToString());
46+
s.Close();
47+
//</Snippet2>
4848
}
4949

50-
public static void CheckProperties ()
50+
public static void CheckProperties()
5151
{
52-
IPHostEntry lipa = Dns.Resolve ("host.contoso.com");
53-
IPEndPoint lep = new IPEndPoint (lipa.AddressList[0], 11000);
52+
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
53+
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);
5454

5555
//<Snippet3>
56-
Socket s = new Socket (lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
56+
Socket s = new Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
5757

58-
//Using the AddressFamily, SocketType, and ProtocolType properties.
59-
Console.WriteLine ("I just set the following properties of socket: " + "Address Family = " + s.AddressFamily.ToString () + "\nSocketType = " + s.SocketType.ToString () + "\nProtocolType = " + s.ProtocolType.ToString ());
58+
// Using the AddressFamily, SocketType, and ProtocolType properties.
59+
Console.WriteLine("I just set the following properties of socket: " + "Address Family = " + s.AddressFamily.ToString() + "\nSocketType = " + s.SocketType.ToString() + "\nProtocolType = " + s.ProtocolType.ToString());
6060

61-
//</Snippet3>
62-
//<Snippet4>
63-
s.Connect (lep);
61+
//</Snippet3>
62+
//<Snippet4>
63+
s.Connect(lep);
6464

6565
// Using the RemoteEndPoint property.
66-
Console.WriteLine ("I am connected to " + IPAddress.Parse (((IPEndPoint)s.RemoteEndPoint).Address.ToString ()) + "on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString ());
66+
Console.WriteLine("I am connected to " + IPAddress.Parse(((IPEndPoint)s.RemoteEndPoint).Address.ToString()) + "on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString());
6767

6868
// Using the LocalEndPoint property.
69-
Console.WriteLine ("My local IpAddress is :" + IPAddress.Parse (((IPEndPoint)s.LocalEndPoint).Address.ToString ()) + "I am connected on port number " + ((IPEndPoint)s.LocalEndPoint).Port.ToString ());
69+
Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)s.LocalEndPoint).Address.ToString()) + "I am connected on port number " + ((IPEndPoint)s.LocalEndPoint).Port.ToString());
7070

71-
//</Snippet4>
71+
//</Snippet4>
7272
//<Snippet5>
73-
//Use low level method IOControl to set this socket to blocking mode.
73+
// Use low level method IOControl to set this socket to blocking mode.
7474
int code = unchecked((int)0x8004667E);
7575
byte[] inBuf = new byte[4];
7676

7777
inBuf[0] = 0;
7878

7979
byte[] outBuf = new byte[4];
8080

81-
s.IOControl (code, inBuf, outBuf);
81+
s.IOControl(code, inBuf, outBuf);
8282

83-
//Check to see that this worked.
83+
// Check to see that this worked.
8484
if (s.Blocking)
8585
{
86-
Console.WriteLine ("Socket was set to Blocking mode successfully");
86+
Console.WriteLine("Socket was set to Blocking mode successfully");
8787
}
88-
//</Snippet5>
88+
//</Snippet5>
8989
}
9090

91-
public static void Main ()
91+
public static void Main()
9292
{
9393
}
9494
}

snippets/visualbasic/VS_Snippets_Remoting/Socket_Socket_Options/VB/source.vb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ Public Class Sync_Send_Receive
1414

1515
Dim s As New Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
1616

17-
'<Snippet1>
18-
'Send operations will time-out if confirmation is
17+
'<Snippet0>
18+
'Send operations will time-out if confirmation is
1919
' not received within 1000 milliseconds.
2020
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000)
21+
'</Snippet0>
2122

23+
'<Snippet1>
2224
' The socket will linger for 10 seconds after Socket.Close is called.
2325
Dim lingerOption As New LingerOption(True, 10)
2426
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)
25-
26-
'</Snippet1>
27+
'</Snippet1>
2728
s.Connect(lep)
2829
Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
2930

xml/System.Net.Sockets/Socket.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13392,9 +13392,9 @@ The <xref:System.Net.Sockets.Socket.SetRawSocketOption(System.Int32,System.Int32
1339213392
## Examples
1339313393
The following code example sets the <xref:System.Net.Sockets.LingerOption> and <xref:System.Net.Sockets.Socket.Send%2A> time-out values.
1339413394

13395-
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_Remoting/Socket_Socket_Options/CPP/source.cpp" id="Snippet1":::
13396-
:::code language="csharp" source="~/snippets/csharp/System.Net.Sockets/LingerOption/LingerTime/source.cs" id="Snippet1":::
13397-
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Remoting/Socket_Socket_Options/VB/source.vb" id="Snippet1":::
13395+
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_Remoting/Socket_Socket_Options/CPP/source.cpp" id="Snippet0":::
13396+
:::code language="csharp" source="~/snippets/csharp/System.Net.Sockets/LingerOption/LingerTime/source.cs" id="Snippet0":::
13397+
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Remoting/Socket_Socket_Options/VB/source.vb" id="Snippet0":::
1339813398

1339913399
]]></format>
1340013400
</remarks>

0 commit comments

Comments
 (0)