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

ASP.NET Discussion :

Connection protocol TCP


Sujet :

ASP.NET

  1. #1
    Membre éprouvé
    Avatar de topolino
    Profil pro
    Inscrit en
    Juillet 2003
    Messages
    1 901
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2003
    Messages : 1 901
    Par défaut Connection protocol TCP
    Bonjour,

    est il possible de se connecter avec les asp.net a du tcp ?

    Merci

  2. #2
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Salut,

    Se connecter avec les asp.net a du tcp? Ca veut dire quoi exactement? Tu as TcpClient mais faudrait que tu donnes plus de détails.

    A+
    "Winter is coming" (ma nouvelle page d'accueil)

  3. #3
    Membre éprouvé
    Avatar de topolino
    Profil pro
    Inscrit en
    Juillet 2003
    Messages
    1 901
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2003
    Messages : 1 901
    Par défaut
    Je veux recuperer des datas qui se trouvent sur un server dont le protocole est du tcp.

    Le meme code fonctionne en application Console mais impossible avec les asp.net :

    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
     
    public string SetServerConnection()
            {
                try
                {
                    server = new TcpClient("00.00.00.00", 1000);
                    networkStream = server.GetStream();
                }
     
                catch (SocketException)
                {
                    return "Unable to connect to server";
                }
     
                return server.Connected.ToString();
            }
     
     public string SetLogin()
            {
                try
                {
                    if (server.Connected)
                    {
                        input = "Command de login - pwd";
     
                        ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
                        byte[] message = aSCIIEncoding.GetBytes(input);
     
                        networkStream.Write(message, 0, message.Length);
                        byte[] bb = new byte[256];
     
                        int k = networkStream.Read(bb, 0, bb.Length);
     
                        for (int i = 0; i < k; i++)
                        {
                            if (bb[i] != 16 && bb[i] != 17)
                            {
                                result += Convert.ToChar(bb[i]);
                            }
                        }
     
                        return result;
                    }
                    return "You are not connected";
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }

    J'ai l'impression que je recupere uniquement que le stream de la SetServerConnection et non de SetLogin.


    Je comprends plus rien !!!!

  4. #4
    Expert confirmé
    Avatar de Immobilis
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2004
    Messages
    6 559
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 559
    Par défaut
    Alors en commençant par ici: http://www.developpez.net/forums/d71...t/#post4185461

    Le serveur:
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using Neptune.Model;
    using System.Xml.Serialization;
    using System.Xml;
    using System.IO;
     
    namespace Neptune.TcpListner
    {
        class CustomTcpListner
        {
            static void Main(string[] args)
            {
                TcpListener server = null;
     
                try
                {
                    // Set the TcpListener on port 13000.
                    Int32 port = 13000;
                    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
     
                    // TcpListener server = new TcpListener(port);
                    server = new TcpListener(localAddr, port);
     
                    // Start listening for client requests.
                    server.Start();
     
                    // Buffer for reading data
                    Byte[] bytes = new Byte[256];
                    String data = null;
     
                    // Enter the listening loop.
                    while (true)
                    {
                        Console.Write("Waiting for a connection... ");
     
                        // Perform a blocking call to accept requests.
                        // You could also user server.AcceptSocket() here.
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");
     
                        data = null;
     
                        // Get a stream object for reading and writing
                        using (NetworkStream stream = client.GetStream())
                        {
                            int i;
     
                            // Loop to receive all the data sent by the client.
                            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                            {
     
                                // Translate data bytes to a ASCII string.
                                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                                Console.WriteLine("Received: {0}", data);
     
                                HelloEntity hello = null;
                                XmlSerializer serializer = new XmlSerializer(typeof(HelloEntity));
                                XmlReader xr = XmlReader.Create(new StringReader(data));
                                if (serializer.CanDeserialize(xr))
                                    hello = (HelloEntity)serializer.Deserialize(xr);
     
     
                                // Process the data sent by the client.
                                data = string.Format("Hello {0} you are born on {1}. You are {2}m tall.",
                                    hello.Name, hello.Birth.ToShortDateString(), hello.Height);
     
                                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
     
                                // Send back a response.
                                stream.Write(msg, 0, msg.Length);
                                Console.WriteLine("Sent: {0}", data);
                            }
                        }
                        // Shutdown and end connection
                        client.Close();
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }
                Console.WriteLine("\nHit enter to continue...");
                Console.Read();
            }
        }
    }
    Le client ASP.Net:
    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
    using System;
    using System.IO;
    using System.Net.Sockets;
    using System.Text;
    using System.Xml.Serialization;
    using Neptune.Model;
     
    namespace WebApplication
    {
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                HelloEntity hello = new HelloEntity() { Birth = new DateTime(1918, 11, 11), Height = 1.72, Name = "Toto" };
                XmlSerializer serializer = new XmlSerializer(typeof(HelloEntity));
                MemoryStream mem = new MemoryStream();
                serializer.Serialize(mem, hello);
                string str = Encoding.UTF8.GetString(mem.ToArray());
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(str);
     
                Connect("127.0.0.1", 13000, data);
            }
     
            private void Connect(String server, Int32 port, Byte[] data)
            {
                TcpClient client = null;
                try
                {
                    client = new TcpClient();
                    client.SendTimeout = 3000;
                    client.Connect(server, port);
     
                    if (client.Connected)
                    {
                        using (NetworkStream stream = client.GetStream())
                        {
                            stream.Write(data, 0, data.Length);
     
                            data = new Byte[256];
     
                            String responseData = String.Empty;
     
                            Int32 bytes = stream.Read(data, 0, data.Length);
                            responseData = Encoding.ASCII.GetString(data, 0, bytes);
                            Response.Write(string.Format("Received: {0}", responseData));
                        }
                    }
                }
                catch (ArgumentNullException e)
                {
                    Response.Write(string.Format("ArgumentNullException: {0}", e));
                }
                catch (SocketException e)
                {
                    Response.Write(string.Format("SocketException: {0}", e));
                }
                finally
                {
                    client.Close();
                }
            }
     
        }
    }
    Et ça marche
    "Winter is coming" (ma nouvelle page d'accueil)

Discussions similaires

  1. pilotes d'installation de protocole TCP/IP
    Par miriame dans le forum Administration
    Réponses: 4
    Dernier message: 11/01/2006, 13h19
  2. Réponses: 1
    Dernier message: 18/12/2005, 19h19
  3. [Protocole TCP] Utilitaire pour Tracer une session
    Par =JBO= dans le forum Développement
    Réponses: 3
    Dernier message: 04/06/2005, 19h28
  4. raw socket et protocole TCP/IP
    Par robertmouac dans le forum Développement
    Réponses: 3
    Dernier message: 09/03/2005, 23h09
  5. Ping sous protocole TCP (et non UDP)
    Par ovdz dans le forum Développement
    Réponses: 2
    Dernier message: 19/06/2003, 14h10

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