Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,95 +10,96 @@ using namespace System::Threading;
public ref class Sync_Send_Receive
{
public:
static void SetSocketOptions()
{
IPHostEntry^ lipa = Dns::Resolve( "host.contoso.com" );
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );
Socket^ s = gcnew Socket( lep->Address->AddressFamily, SocketType::Stream,ProtocolType::Tcp );

//<Snippet1>
// Specifies that send operations will time-out
// if confirmation is not received within 1000 milliseconds.
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000 );

// Specifies that the Socket will linger for 10 seconds after Close is called.
LingerOption^ lingerOption = gcnew LingerOption( true,10 );

s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption );
//</Snippet1>

s->Connect( lep );

array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );

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

// Blocks until send returns.
int i = s->Send( msg );

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive( bytes );

//Displays to the screen.
Console::WriteLine( Encoding::ASCII->GetString( bytes ) );
s->Shutdown( SocketShutdown::Both );
Console::Write( "If data remains to be sent, this application will stay open for " );
Console::WriteLine( safe_cast<LingerOption^>(s->GetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Linger ))->LingerTime.ToString() );
s->Close();
//</Snippet2>
}

static void CheckProperties()
{
IPHostEntry^ lipa = Dns::Resolve( "host.contoso.com" );
IPEndPoint^ lep = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );

//<Snippet3>
Socket^ s = gcnew Socket( lep->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );

//Uses the AddressFamily, SocketType, and ProtocolType properties.
Console::Write( "I just set the following properties of socket: \n" );
Console::Write( "Address Family = {0}", s->AddressFamily.ToString() );
Console::Write( "\nSocketType = {0}", s->SocketType.ToString() );
Console::WriteLine( "\nProtocolType = {0}", s->ProtocolType.ToString() );
//</Snippet3>

//<Snippet4>
s->Connect( lep );

// Uses the RemoteEndPoint property.
Console::WriteLine( "I am connected to {0} on port number {1}",
IPAddress::Parse( ( ( (IPEndPoint^)(s->RemoteEndPoint) )->Address)->ToString() ),
( (IPEndPoint^)(s->RemoteEndPoint) )->Port.ToString() );

// Uses the LocalEndPoint property.
Console::Write( "My local IpAddress is : {0}\nI am connected on port number {1}",
IPAddress::Parse( ( ( (IPEndPoint^)(s->LocalEndPoint) )->Address)->ToString() ),
( (IPEndPoint^)(s->LocalEndPoint) )->Port.ToString() );
//</Snippet4>

//<Snippet5>
//Uses low level method IOControl to set this socket to blocking mode.
int code = 0x8004667E;
array<Byte>^ inBuf = gcnew array<Byte>(4);

inBuf[ 0 ] = 0;

array<Byte>^ outBuf = gcnew array<Byte>(4);

s->IOControl( code, inBuf, outBuf );

//Checks to see that this worked.
if ( s->Blocking )
{
Console::WriteLine( "Socket was set to Blocking mode successfully" );
}
//</Snippet5>
}
static void SetSocketOptions()
{
IPHostEntry^ lipa = Dns::Resolve("host.contoso.com");
IPEndPoint^ lep = gcnew IPEndPoint(lipa->AddressList[0], 11000);
Socket^ s = gcnew Socket(lep->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp);

//<Snippet0>
// Specifies that send operations will time-out
// if confirmation is not received within 1000 milliseconds.
s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::SendTimeout, 1000);
//</Snippet0>

//<Snippet1>
// Specifies that the Socket will linger for 10 seconds after Close is called.
LingerOption^ lingerOption = gcnew LingerOption(true, 10);
s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption);
//</Snippet1>

s->Connect(lep);

array<Byte>^ msg = Encoding::ASCII->GetBytes("This is a test");

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

// Blocks until send returns.
int i = s->Send(msg);

// Blocks until read returns.
array<Byte>^ bytes = gcnew array<Byte>(1024);

s->Receive(bytes);

// Displays to the screen.
Console::WriteLine(Encoding::ASCII->GetString(bytes));
s->Shutdown(SocketShutdown::Both);
Console::Write("If data remains to be sent, this application will stay open for ");
Console::WriteLine(safe_cast<LingerOption^>(s->GetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger))->LingerTime.ToString());
s->Close();
//</Snippet2>
}

static void CheckProperties()
{
IPHostEntry^ lipa = Dns::Resolve("host.contoso.com");
IPEndPoint^ lep = gcnew IPEndPoint(lipa->AddressList[0], 11000);

//<Snippet3>
Socket^ s = gcnew Socket(lep->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp);

// Uses the AddressFamily, SocketType, and ProtocolType properties.
Console::Write("I just set the following properties of socket: \n");
Console::Write("Address Family = {0}", s->AddressFamily.ToString());
Console::Write("\nSocketType = {0}", s->SocketType.ToString());
Console::WriteLine("\nProtocolType = {0}", s->ProtocolType.ToString());
//</Snippet3>

//<Snippet4>
s->Connect(lep);

// Uses the RemoteEndPoint property.
Console::WriteLine("I am connected to {0} on port number {1}",
IPAddress::Parse((((IPEndPoint^)(s->RemoteEndPoint))->Address)->ToString()),
((IPEndPoint^)(s->RemoteEndPoint))->Port.ToString());

// Uses the LocalEndPoint property.
Console::Write("My local IpAddress is : {0}\nI am connected on port number {1}",
IPAddress::Parse((((IPEndPoint^)(s->LocalEndPoint))->Address)->ToString()),
((IPEndPoint^)(s->LocalEndPoint))->Port.ToString());
//</Snippet4>

//<Snippet5>
// Uses low level method IOControl to set this socket to blocking mode.
int code = 0x8004667E;
array<Byte>^ inBuf = gcnew array<Byte>(4);

inBuf[0] = 0;

array<Byte>^ outBuf = gcnew array<Byte>(4);

s->IOControl(code, inBuf, outBuf);

// Checks to see that this worked.
if (s->Blocking)
{
Console::WriteLine("Socket was set to Blocking mode successfully");
}
//</Snippet5>
}
};

int main(){}
int main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,88 @@

public class Sync_Send_Receive
{
public static void SetSocketOptions ()
public static void SetSocketOptions()
{
IPHostEntry lipa = Dns.Resolve ("host.contoso.com");
IPEndPoint lep = new IPEndPoint (lipa.AddressList[0], 11000);
Socket s = new Socket (lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

//<Snippet1>
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);
Socket s = new Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//<Snippet0>
// Send operations will time-out if confirmation
// is not received within 1000 milliseconds.
s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
//</Snippet0>

//<Snippet1>
// The socket will linger for 10 seconds after Socket.Close is called.
LingerOption lingerOption = new LingerOption (true, 10);

s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
var lingerOption = new LingerOption(true, 10);
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
//</Snippet1>

//</Snippet1>
s.Connect (lep);
s.Connect(lep);

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

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

// blocks until send returns
int i = s.Send (msg);
int i = s.Send(msg);

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

s.Receive (bytes);
s.Receive(bytes);

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

public static void CheckProperties ()
public static void CheckProperties()
{
IPHostEntry lipa = Dns.Resolve ("host.contoso.com");
IPEndPoint lep = new IPEndPoint (lipa.AddressList[0], 11000);
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

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

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

//</Snippet3>
//<Snippet4>
s.Connect (lep);
//</Snippet3>
//<Snippet4>
s.Connect(lep);

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

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

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

inBuf[0] = 0;

byte[] outBuf = new byte[4];

s.IOControl (code, inBuf, outBuf);
s.IOControl(code, inBuf, outBuf);

//Check to see that this worked.
// Check to see that this worked.
if (s.Blocking)
{
Console.WriteLine ("Socket was set to Blocking mode successfully");
Console.WriteLine("Socket was set to Blocking mode successfully");
}
//</Snippet5>
//</Snippet5>
}

public static void Main ()
public static void Main()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ Public Class Sync_Send_Receive

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

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

'<Snippet1>
' The socket will linger for 10 seconds after Socket.Close is called.
Dim lingerOption As New LingerOption(True, 10)
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)

'</Snippet1>
'</Snippet1>
s.Connect(lep)
Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")

Expand Down
6 changes: 3 additions & 3 deletions xml/System.Net.Sockets/Socket.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13380,9 +13380,9 @@ The <xref:System.Net.Sockets.Socket.SetRawSocketOption(System.Int32,System.Int32
## Examples
The following code example sets the <xref:System.Net.Sockets.LingerOption> and <xref:System.Net.Sockets.Socket.Send%2A> time-out values.

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

]]></format>
</remarks>
Expand Down