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

VB.NET Discussion :

TCPClient et TCPListener


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Inscrit en
    Mars 2008
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 9
    Par défaut TCPClient et TCPListener
    Bonjour a tous. J'ai essayé une interaction client-serveur avec respectivement
    ces codes
    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
     
    Imports System.Net.Sockets
    Imports System.Text
    Class TCPSrv
        Shared Sub Main()
            ' Must listen on correct port- must be same as port client wants to connect on.
            Const portNumber As Integer = 8000
            Dim tcpListener As New TcpListener(portNumber)
            tcpListener.Start()
            Console.WriteLine("Waiting for connection...")
            Try
                'Accept the pending client connection and return 
                'a TcpClient initialized for communication. 
                Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
                Console.WriteLine("Connection accepted.")
                ' Get the stream
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                ' Read the stream into a byte array
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Return the data received from the client to the console.
                Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine(("Client sent: " + clientdata))
                Dim responseString As String = "Connected to server."
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                Console.WriteLine(("Message Sent /> : " + responseString))
                'Any communication with the remote client using the TcpClient can go here.
                'Close TcpListener and TcpClient.
                tcpClient.Close()
                tcpListener.Stop()
                Console.WriteLine("exit")
                Console.ReadLine()
            Catch e As Exception
                Console.WriteLine(e.ToString())
                Console.ReadLine()
            End Try
        End Sub
       End Class
    Client :
    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
     
    Imports System.Net.Sockets
    Imports System.Text
    Class TCPCli
        Shared Sub Main()
     
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect("127.0.0.1", 8000)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
                ' Do a simple write.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                ' Read the NetworkStream into a byte buffer.
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Output the data received from the host to the console.
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine(("Host returned: " + returndata))
            Else
                If Not networkStream.CanRead Then
                    Console.WriteLine("cannot not write data to this stream")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        Console.WriteLine("cannot read data from this stream")
                        tcpClient.Close()
                    End If
                End If
            End If
            ' pause so user can view the console output
            Console.ReadLine()
        End Sub
    End Class
    Mais malheuresement j'obtiens un message d'erreur.
    Pouvez-vous me dire ce qui ne va pas ? Merci

  2. #2
    Membre éprouvé

    Homme Profil pro
    kiné passionné de dev
    Inscrit en
    Mars 2006
    Messages
    1 570
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : kiné passionné de dev

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 570
    Billets dans le blog
    1
    Par défaut
    Commence par donner le message d'erreur

  3. #3
    Membre habitué
    Inscrit en
    Mars 2008
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 9
    Par défaut
    Voici le message :
    Fichiers attachés Fichiers attachés

  4. #4
    Membre expérimenté Avatar de gderenne
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2007
    Messages
    250
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Juillet 2007
    Messages : 250
    Par défaut
    Salut,

    A ce moment là, il faut donner un lien vers le UNRAR pour décompresser ton message d'erreur...

    Le mieux aurait été de copier/coller le texte dans une balise QUOTE...

  5. #5
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Je pense que tu essaies de créer un TcpListener sur un port qui est déjà en écoute... regarde s'il n'y a pas un autre programme qui écoute sur le port 8000 (commande "netstat -a")

Discussions similaires

  1. Serveur: TcpListener et TcpClient
    Par molo2003 dans le forum C#
    Réponses: 6
    Dernier message: 14/08/2010, 14h55
  2. Réponses: 0
    Dernier message: 11/08/2009, 22h57
  3. Réponses: 1
    Dernier message: 20/05/2009, 09h55
  4. TCPClient et TCPListener
    Par mehdi_862000 dans le forum Framework .NET
    Réponses: 12
    Dernier message: 26/05/2008, 16h33
  5. Réponses: 2
    Dernier message: 25/04/2008, 19h31

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