IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Problème d'écoute sur TCPIP


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Septembre 2005
    Messages : 197
    Par défaut Problème d'écoute sur TCPIP
    Bonjour,

    Le programme suivant me permet d'écouter si une connexion se fait et de lire le byteStream envoyé. Je détecte trèes bien les nouvelles connexions et tout est envoyé/reçu correctement.

    Par contre, si j'ai une connexion qui est persistente (i.e. qui ne se ferme pas), je ne reçois pas le byteStream. Comment puis-je modifier le code pour lire ce qui me parvient à toutes les période de temps x?

    merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
     
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;
    //using XMLGenerator;
    using XmlTools;
    using System.Collections.Generic;
     
     
    // Command Message Ping
    namespace TestCase2
    {
        class Program
        {
            private static StreamWriter sw;
            private static bool performPing = true;
     
            static void Main(string[] args)
            {
     
                try
                {
                    int myClock;
                    sw = new StreamWriter("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\Logs\\TestCase2_log_" + DateTime.Now.Millisecond.ToString() + ".xml", true);
     
                    IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
                    IPHost.AddressList[0].ToString();
                    IPAddress localAddress = IPAddress.Parse(IPHost.AddressList[0].ToString());
     
                    //IPAddress localAddress = IPAddress.Parse("127.0.0.1");
     
                    // Define the kind of socket we want: Internet, Stream, TCP
                    Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     
                    // Define the address we want to claim: the first IP address found earlier, at port 2200
                    IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 2200);
     
                    // Bind the socket to the end point
                    listenSocket.Bind(ipEndpoint);
     
                    // Start listening, only allow 10 connection to queue at the same time
                    listenSocket.Listen(10);
                    listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);
                    //Console.WriteLine("Server is waiting on socket {0}", listenSocket.LocalEndPoint);
     
     
                    myClock = 0;
                    while (true)
                    {
                        // Write a message and sleep for 2 seconds
                        //Console.WriteLine("Busy Waiting - Clock: " + myClock.ToString() + " seconds");
                        Thread.Sleep(2000);
                        myClock += 2;
                        if (myClock == 200)
                        {
                            sw.Close();
                            Environment.Exit(1);
                        }
                    }
     
                }
                catch (Exception e)
                {
                    Console.WriteLine("Caught Exception: {0}", e.ToString());
                }
            }
     
     
            public static void ReceiveCallback(IAsyncResult AsyncCall)
            {
     
     
                try
                {
                    Socket listener = (Socket)AsyncCall.AsyncState;
                    Socket client = listener.EndAccept(AsyncCall);
     
                    if (performPing)
                    {
     
     
     
                        // Sending Command Message Ping
                        Byte[] message = XmlConverter.ConvertStringToByteArray("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\CommandMessage_Ping_Request.xml");
                        sw.WriteLine("Sending Ping Command to " + client.RemoteEndPoint.ToString() + "on " + DateTime.Now.Millisecond.ToString());
                        sw.WriteLine(System.Text.Encoding.UTF8.GetString(message));
                        Console.WriteLine("Sending Ping Command to {0}", client.RemoteEndPoint);
                        //Console.WriteLine(System.Text.Encoding.UTF8.GetString(message));
                        client.Send(message);
                        performPing = false;
     
                        for (int x = 0; x < 4000; x++)
                        {
                            // Receiving
                            byte[] myBuffer = new byte[100000];
                            client.Receive(myBuffer);
                            BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
                            binWriter.Write(myBuffer);
                            binWriter.Close();
                            sw.WriteLine("Received Connection from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
                            sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
                            Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint);
                            Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
                            Console.ReadKey(true);
                            Thread.Sleep(50);
                            x += 1;
     
                        }
                    }
     
                    else
                    {
     
                        // Receiving response
                        byte[] myBuffer2 = new byte[100000];
                        client.Receive(myBuffer2);
                        //Console.WriteLine("Receiving response from {0}", client.RemoteEndPoint);
                        sw.WriteLine("Received response from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
                        sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));
     
                        BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
                        binWriter.Write(myBuffer2.Length);
                        binWriter.Write(myBuffer2);
                        binWriter.Close();
                        Console.WriteLine("Received response from {0}", client.RemoteEndPoint);
                        //Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));
                    }
     
                    //// At the end of the connection, we need to tell the OS that we can receive another call
                    listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener);
     
     
     
     
                }
     
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
     
     
     
            }
     
     
        }
    }

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7
    Par défaut
    ce que tu devrais faire c'est que

    -chaque nouvelle connexion crée un thread
    -décrire la procédure de traitement dans ton thread

    ou tu devrai peut autoriser un temp max par nouvelle connexio

Discussions similaires

  1. Réponses: 3
    Dernier message: 10/11/2008, 12h22
  2. [Debian Etch] problème d'écoute sur un port
    Par Zipyz dans le forum Sécurité
    Réponses: 1
    Dernier message: 10/12/2007, 20h53
  3. [CR] Problème de sélection sur un champ date
    Par noluc dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 21/11/2003, 16h56
  4. Problème avec RDTSC sur K6-III
    Par le mage tophinus dans le forum x86 32-bits / 64-bits
    Réponses: 17
    Dernier message: 30/09/2003, 09h43
  5. problème de float sur SQL server 2000.
    Par fidji dans le forum MS SQL Server
    Réponses: 9
    Dernier message: 24/07/2003, 14h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo