Chinese (People's Republic of China)  English  Français


Supinfo-Projects.com
Supinfo Students' projects



Projects
  Last project
  Top projects
  All projects

149 User's online
3168 Projects


My Supinfo-Projects

   Login
   Create account


Synopsis

   7 Hits
   Visitors Score : 19
    (2 Voter)
   1 Comments

   Read the article

Evaluate this article

20
18
16
14
12
10
8
6
4
2
0


Comment this article

Author :

Email :

Your comment :



 
2006 - Pérennisation
TCP Client Server programming using TCP with C# .NET2
[20 mn of reading - published 6/3/2006 6:29:38 PM - Target : Confirmé]

Author

THGThomas BONNET
Student-Engineer Supinfo Paris
SUPINFO graduate year  2007

   Write to the person
   All projects of the same author
   Mini-CV of the author

1. Overview and principles

    This chapter enables you to understand how TcpClient Object works, the way to develop a basic client/server relationship and how to use Threading.

    1.1. The TCP connection

    The TCP Connection (RFC 793) permits us to manage a client/server inside an IP network, and it also permit to:

    • Create a permanent link between the client and the server
    • Make sure that all data have been sent.
    • Make sure that correct data are transferred, thanks to the error detection.

    We will not forge manually a TCP packet, we will use C# objects which are:

    • client-side: System.Net.Sockets.TcpClient
    • server-side: System.Net.Sockets.TcpListener to wait for incoming connections, then System.Net.TcpClient

    The server awaits incoming connections on a specified port and a network interface.
    Once the connection is established, the server will transfer the connected socket to a new TcpClient.

    Note: We could also create a TCP Client via a Socket by specifying the protocole Parameter: System.Net.Sockets.ProtocolTypes.TCP

    To create a TcpClient: TcpClient tcp=new TcpClient(string_IPaddress, int_port)

    1.2. Using TcpClient and NetworkStream objects.

    Once the TcpClient connected, its NetworkStream object permits us to send and receive data.
    However, before sending or receiving anything, we will have to check if the operation is possible.

    In the blue frame, the associated C# source code:

    using System.Net;
    using System.Net.Sockets;
    using System.Text
    ...
    public void connectionEstablished(TcpClient tcpClient)
    {
        //We get the TcpClient NetworkStream
        NetworkStream ns=tcpClient.GetStream();
        if (ns.CanWrite)
        {
             //Format the string using ASCII or UNICODE
             byte[] bytesent=ASCIIEncoding.ASCII.GetBytes("Salut");
             ns.Write(bytesent,0,bytesent.Length);
             Console.WriteLine("Ca a marché");
        }
        else
        {
             Console.WriteLine("Ne peut écrire");
        }
        if (ns.CanRead)
        {
             //Code...
        }
        //We close the communication channel
        ns.Close();//Note that the NetworkStream is independant from the TcpClient 
    //Therefore, you should close the two involved objects. tcpClient.Close(); }

    Note: Don't forget the TcpClient is independant from the NetworkStream. But if you just close the tcpClient, the garbage collector will close the non-referenced associated NetworkStream.

    1.3. Multi-threading

    Client-side like server-side programming must implement Threading (System.Threading.Thread). This will enable the application GUI to be refreshed correctly, and will execute connection handling methods asynchronously.

    To start a thread, you should just:

    myThread = new System.Threading.Thread(new 
       System.Threading.ThreadStart(ConnectonToServer));
    
    myThread.Start();

    ConnectionToServer represents the method to be executed by the newly created Thread, simultaneously with the principal Thread.

    The example we'll try to explain is the following (grouped by Threads):

    • Multi-connection server
      - Windows Interface
      - 1 class awaiting connections, listening to a specific TCP Port (TcpListener)
      - 1 class per client connection (TcpClient)
    • Client (1 connection to the server)
      - Window Interface
      - 1 class to communicate with the server.

    Note: These classes will be described in next chapters.

    In theory, a Thread can not access other Thread data without making safe operations and calls.

    The Framework offers a transparent method based on events and method callbacks for the multi-thread communication.

    A user-created thread cannot access a Form graphical components because it is run on a separate thread (see: ThreadPrincipal).
    The Invoke() method on the graphical components calls back a method using a delegate executed by the Thread where the object is located.

    Throwing an event example(class-side containing a different Thread from the principal thread):

    //Client code - part: TcpClient
    //Since there is only one client connection / We have marked the class as static
    public static class Connection { //create a delegate public delegate void ConnectionEstablishedEventArgs(EventArgs e); //create an event delegate (type of the delegate)
    public static event ConnectionEstablishedEventArgs ConnectionEstablished; //Create the method that will call the event delegate
    public static void OnConnectionEstablished() { ConnectionEstablished(); } //Code etc... //When the connection is establihed, throw the event:
    OnConnectionEstablished() }

    Getting the event into the form and manipulating a graphic component:

    //Client Interface Code
    public class Form1 { ... public Form1() { //Code init... //Define the method handling the event
    Connection.ConnectionEstablished +=
    new Connection.ConnectionEstablishedEventArgs(
    Connection_ConnectionEstablished); } void Connection_ConnectionEstablished() { if (this.InvokeRequired) { //Callback this function from the main Thread. this.Invoke(new ConnectionEstablishedCallBack(
    Connection_ConnectionEstablished)); } else { //We are now in the principal Thread:
    this.Text="OK!"; } } }

     To manipulate objects on separate threads, you have to use the C# keyword: lock() in order to lock a part of your code (critical section), to let the thread execute code that interfers with shared objects.

    During all the time required to complete the operations, concurrent Threads will wait for the object to be unlocked, in case they need to access the object or to enter a similar critical section code locking the same object.

    I did not face any problem when I tried to add elements in Collections (List) objects without using a lock() statement. But it is required during an object enumeration (foreach) or an object removal.

    Here is another example to understand this issue:

    // statements_lock2.cs
    using System;
    using System.Threading;
    using System.Collections.Generic;
    
    
    static class Test
    {
    //Variable used to demonstrate this thread issue. public static List<string> liste; static void Main() {
    liste = new List<string>(new string[20]); Thread[] threads = new Thread[10];





    ListManipulation listman = new ListeManipulation();


    for (int i = 0; i < 5; i++) { Thread t = new Thread(new ThreadStart(listman.Enlever)); threads[i] = t; } for (int i = 0; i < 5; i++) { threads[i].Start();//Threads are launched all together. } //Wait for the threads to quit the critical section.
    //Comment lock to test lock (Test.liste) { //Display the amount of strings remaining in the collection
    foreach (string sz in liste) { System.Console.WriteLine("* 1 string * "); } } System.Console.ReadKey(); } } class ListeManipulation { public void Enlever() { //Comment lock to check
    lock (Test.liste) Test.liste.RemoveAt(0); } }

    Test directly this code in Visual Studio and you will understand the necessity of using lock().

    More information can be found in Visual Studio Help.



    Articles de la même catégorie

     Pages : Top


    7 Hits
    0 Comments
    JMX
    [20 mn of reading - published 6/3/2006 6:19:21 PM - Target : Expert]

    More


    12 Hits
    0 Comments
    What is Web 2.0?
    [30 mn of reading - published 6/3/2006 5:43:53 PM - Target : Confirmé]

    More


    20 Hits
    0 Comments
    Web 2.0
    [30 mn of reading - published 6/3/2006 5:42:02 PM - Target : Confirmé]

    More

    Powered by Campus-Booster Technology
    Conditions d'utilisation & Copyright | Respect de la vie privée
    © Copyright 1965-2006 Supinfo Paris, Paris Academy of Computer Science
    Supinfo, Ecole Supérieure d'Informatique et Paris Academy Of Computer Science are trade marks.
    23, rue de Château LANDON - 75010 PARIS - Phone : +33 (0) 153359 700 Fax : +33 (0) 153359 701

    Web site autided by :