Networking in C#

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Networking in C#

NetworkStream Class
To create a NetworkStream, you must provide a connected Socket. You can also specify what FileAccess
permission the NetworkStream has over the provided Socket. By default, closing the NetworkStream
does not close the provided Socket. If you want the NetworkStream to have permission to close the
provided Socket, you must specify true for the value of the ownsSocket parameter.
Use the Write and Read methods for simple single thread synchronous blocking I/O. If you want to process
your I/O using separate threads, consider using the BeginWrite and EndWrite methods, or the BeginRead
and EndRead methods for communication.

Socket Class
The Socket class follows the .NET Framework naming pattern for asynchronous methods. For example, the
synchronous Receive method corresponds to the asynchronous BeginReceive and EndReceive methods.
If your application only requires one thread during execution, use the following methods, which are designed
for synchronous operation mode.
 If you are using a connection-oriented protocol such as TCP, your server can listen for connections
using the Listen method. The Accept method processes any incoming connection requests and returns a
Socket that you can use to communicate data with the remote host. Use this returned Socket to call
the Send or Receive method. Call the Bind method prior to calling the Listen method if you want to
specify the local IP address and port number. Use a port number of zero if you want the underlying
service provider to assign a free port for you. If you want to connect to a listening host, call the
Connect method. To communicate data, call the Send or Receive method.
 If you are using a connectionless protocol such as UDP, you do not need to listen for connections at
all. Call the ReceiveFrom method to accept any incoming datagrams. Use the SendTo method to send
datagrams to a remote host.

To process communications using separate threads during execution, use the following methods, which are
designed for asynchronous operation mode.
 If you are using a connection-oriented protocol such as TCP, use the Socket, BeginConnect, and
EndConnect methods to connect with a listening host. Use the BeginSend and EndSend or BeginReceive
and EndReceive methods to communicate data asynchronously. Incoming connection requests can be
processed using BeginAccept and EndAccept.
 If you are using a connectionless protocol such as UDP, you can use BeginSendTo and EndSendTo
to send datagrams, and BeginReceiveFrom and EndReceiveFrom to receive datagrams.

When you are finished sending and receiving data, use the Shutdown method to disable the Socket. After
calling Shutdown, call the Close method to release all resources associated with the Socket.

The Socket class allows you to configure your Socket using the SetSocketOption method. Retrieve these
settings using the GetSocketOption method.

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the
Listen method. You do not need to call Bind before using the Connect method unless you need to use a
specific local endpoint. You can use the Bind method on both connectionless and connection-oriented
protocols.
Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data.
If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress..::.Any as
the address parameter, and the underlying service provider will assign the most appropriate network
address.

If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this
case, the service provider will assign an available port number between 1024 and 5000.

If you use the above approach, you can discover what local network address and port number has been
assigned by calling the LocalEndPoint. If you are using a connection-oriented protocol, LocalEndPoint will not
return the locally assigned network address until after you have made a call to the Connect or EndConnect
method. If you are using a connectionless protocol, you will not have access to this information until you
have completed a send or receive.

public void Listen(


int backlog
)

Listen causes a connection-oriented Socket to listen for incoming connection attempts. The backlog
parameter specifies the number of incoming connections that can be queued for acceptance.

Accept synchronously extracts the first pending connection request from the connection request queue of
the listening socket, and then creates and returns a new Socket

The Dns class is a static class that retrieves information about a specific host from the Internet Domain
Name System (DNS).
The host information from the DNS query is returned in an instance of the IPHostEntry class. If the specified
host has more than one entry in the DNS database, IPHostEntry contains multiple IP addresses and aliases
Synchronous Client Socket Example

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketClient {

public static void StartClient() {


// Data buffer for incoming data.
byte[] bytes = new byte[1024];

// Connect to a remote device.


try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);

// Create a TCP/IP socket.


Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Connect the socket to the remote endpoint. Catch any errors.


try {
sender.Connect(remoteEP);

Console.WriteLine("Socket connected to {0}",


sender.RemoteEndPoint.ToString());

// Encode the data string into a byte array.


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

// Send the data through the socket.


int bytesSent = sender.Send(msg);

// Receive the response from the remote device.


int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec));

// Release the socket.


sender.Shutdown(SocketShutdown.Both);
sender.Close();

} catch (ArgumentNullException ane) {


Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}

} catch (Exception e) {
Console.WriteLine( e.ToString());
}
}

public static int Main(String[] args) {


StartClient();
return 0;
}
}
Synchronous Server Socket Example

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener {

// Incoming data from the client.


public static string data = null;

public static void StartListening() {


// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.


// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

// Create a TCP/IP socket.


Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Bind the socket to the local endpoint and


// listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(10);

// Start listening for connections.


while (true) {
Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
Socket handler = listener.Accept();
data = null;

// An incoming connection needs to be processed.


while (true) {
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1) {
break;
}
}

// Show the data on the console.


Console.WriteLine( "Text received : {0}", data);

// Echo the data back to the client.


byte[] msg = Encoding.ASCII.GetBytes(data);

handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}

} catch (Exception e) {
Console.WriteLine(e.ToString());
}

Console.WriteLine("\nPress ENTER to continue...");


Console.Read();

public static int Main(String[] args) {


StartListening();
return 0;
}
}
Asynchronous networking
Use an AsyncCallback delegate to process the results of an asynchronous operation in a separate thread.
The AsyncCallback delegate represents a callback method that is called when the asynchronous operation
completes. The callback method takes an IAsyncResult parameter, which is subsequently used to obtain the
results of the asynchronous operation.

ManualResetEvent allows threads to communicate with each other by signaling. Typically, this
communication concerns a task which one thread must complete before other threads can proceed.

When a thread begins an activity that must complete before other threads proceed, it calls Reset to put
ManualResetEvent in the non-signaled state. This thread can be thought of as controlling the
ManualResetEvent. Threads that call WaitOne on the ManualResetEvent will block, awaiting the signal.
When the controlling thread completes the activity, it calls Set to signal that the waiting threads can
proceed. All waiting threads are released.

New Asynchronous networking (.NET 3.5)


The Begin/End design pattern currently implemented by the Socket class for asynchronous socket I/O requires
aSystem.IAsyncResult object be allocated for each asynchronous socket operation.

In the new Socket class enhancements, asynchronous socket operations are described by


reusable System.Net.Sockets.SocketAsyncEventArgs class objects allocated and maintained by the application.

The pattern for performing an asynchronous socket operation with this class consists of the following steps:

1. Allocate a new SocketAsyncEventArgs context object, or get a free one from an application pool.

2. Set properties on the context object to the operation about to be performed (the completion callback method, the data
buffer, the offset into the buffer, and the maximum amount of data to transfer, for example).

3. Call the appropriate socket method (xxxAsync) to initiate the asynchronous operation.

4. If the asynchronous socket method (xxxAsync) returns true, in the callback, query the context properties for completion
status.

5. If the asynchronous socket method (xxxAsync) returns false, the operation completed synchronously. The context
properties may be queried for the operation result.

6. Reuse the context for another operation, put it back in the pool, or discard it.

The lifetime of the new asynchronous socket operation context object is determined by references by the application code and
asynchronous I/O references. It is not necessary for the application to retain a reference to an asynchronous socket operation
context object after it is submitted as a parameter to one of the asynchronous socket operation methods. It will remain referenced
until the completion callback returns. However it is advantageous for the application to retain the reference to the context so that it
can be reused for a future asynchronous socket operatio

You might also like