This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

Network Communication in C# through TCP-IP

In this tutorial, you will learn how to create a basic client-server application in C#, using the TCP/IP network protocol.

1. The basics

Nowadays, the main protocol used in network communication is the Internet Protocol (or IP). It has two sub-protocols: TCP (Transfer Control Protocol) and UDP (User Datagram Protocol). The difference between them is that UDP sends and receives to/from specific destinations whenever a physical connection is available between the two hosts, whereas TCP maintains a logical connection through which data flows.

In this article, we will take a look at the client-server architecture of TCP.

2. The client-server principle

TCP communication is done between two hosts, one of which is a server, the other one being a client. The difference is that a server can have multiple clients attached to it, whereas a client can connect to one server. The two hosts must each have IP addresses and a common port number (a logical channel through which they communicate). Apart from the standard ports, any port that is not already in use by a server can be used.

3. The server

Let's now analyze a server application. A server must take certain steps before being able to communicate. The first one is to create a "socket" (the base object with which to communicate):


System.Net.Sockets.Socket Listener = new Socket(AddressFamily.InterNetwork,SocketType?.Stream,ProtocolType?.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, Convert.ToInt16(port_number, 10));
Listener.Bind(ipLocal);
Listener.Listen(10);

The socket is created, then it is bound to a specific endpoint (a mix between an IP address (the address we want to listen on, in this case all of them) and the port number on which to listen) with the "Bind" member. After that, it starts listening and waits until there are clients that want to connect. The maximum number of connectible clients is 10, in our case, and it is sent as a parameter to the "Listen" function.

The next portion is a little tricky. To get the maximum boost out of a server, a coder should use asynchronous sockets to manipulate data. The trouble with them is that they register many callback functions, therefore slowing things down a bit. The advantage is that they are later managed a lot easier.

Next, we must register a callback for an event. Any time a user connects, the server must either put him on hold or accept him. Let's say the function we will defgine as the callback is called "OnClientConnect". We have:


Listener.BeginAccept(new AsyncCallback(OnClientConnect), null);

And the callback function with its helper function "WaitForData":


byte[] buffer;
Socket Client;

private void OnClientConnect(IAsyncResult asyn)
{
Client = Listener.EndAccept(asyn);
buffer = new byte100;
WaitForData(Client, buffer);
}

private void WaitForData()
{
Client.BeginReceive(buffer, 0, buffer.Length, SocketFlags?.None, new AsyncCallback(OnDataReceived), null);
}

The code might be difficult to understand, since it involves some callbacks. Let's explain each code line one by one. The first callback "OnClientConnect" announces the server socket that it has ended accepting the current client and that it can start accepting another one. It creates another socket, "Client", that represents the communication interface with the current client. It than initializes the "buffer" array, so that it will be able to hold the data received from the socket. It then calls its helper function, "WaitForData", so that it will create another callback for the "Data received" event, called "OnDataReceived":


private void OnDataReceived(IAsyncResult asyn)
{
string szData;
int iRx = 0 ;
iRx = Client.EndReceive(asyn);
char[] chars = new chariRx+1;
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
szData = new string(chars);

// Do whatever one desires with the szData string which represents the received data

WaitForData();
}

After doing that, the callback function will take over. When data is received, it converts it to a string to make it readable. After that, the "WaitForData" is called again to signal the fact that new data can be received. And so on in an infinite chain.

The application is a lot more complicated than that, in fact. Coping with multiple clients, exception handling, etc. are just a few things that must be taken into consideration when creating a server application. Full source code for this application can be found at http://www.mortis.afraid.org/index/csharp/examples/tcpasyncserver/.

Needless to say, MSDN should be consulted (http://msdn.microsoft.com/), since network communication is a topic that cannot be easily covered through only one article. A coder should be able to easily understand C# code syntax and should be willing to experiment, since there are many issues that one may come upon.

4. The client

The job of the client is a little easier than that of the server. It does only need to create a socket and connect it to a remote host. Practically speaking, its code is the following:


Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType?.Stream, ProtocolType?.Tcp);
string szIPSelected = "192.168.1.1";
string szPort = "12345";
int alPort = System.Convert.ToInt16 (szPort,10);

System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
string szData = "Hello there. This is the text that will be sent.";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData);
m_socClient.Send(byData);

A lot easier, indeed. It just needs to connect, then send. The code lines are self-explanatory, so no need to comment on them.

5. Final comments

As you may have noticed, our server only receives, and our client only sends. Swapping the roles (if the variable names are properly interchanged) should yield no problem.

Since only network communication basics can be covered in an article, please consult the C# SDK and MSDN for full command usage. After all, network communication takes a lot of creativity when it comes to the way it is organized.

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger