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 :

Gestion Socket


Sujet :

VB.NET

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 10
    Par défaut Gestion Socket
    Bonjour a tous,
    je cherche a faire un petit programme, tout compte qui:
    - Ouvre un port,
    - A chaque packet recu, on inscrit sa valeur dans richtextbox2

    et c'est tout
    j'ai compris comment on peut ouvrir un port mais pour mettre la valeur de tout les packet recu, plus compliquer.

    merci de m'aider

  2. #2
    Membre éclairé Avatar de hugoclo
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    615
    Détails du profil
    Informations personnelles :
    Âge : 49
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 615
    Par défaut
    voici le code dont je m sert Attention UDP
    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
    Imports System.Net.Sockets
    Imports System.Text
    
    #Region "Déclarations & structures"
        Private m_UdpClient As UdpClient
        Private Delegate Sub m_DelegateUdpReceive(ByVal str As String, ByVal sIP As String)
        Private UDPPORTNO As Integer
        Private result As List(Of String)
        Private sRemoteIP As String = Nothing 
    
    a mettre dans FORM LOAD
                    UDPPORTNO = TON NUMERO DE PORT
                    Invalidate()
                    m_UdpClient = New UdpClient(UDPPORTNO) 'UDP port create..
                    m_UdpClient.BeginReceive(AddressOf UdpReceiveMessage, Nothing) 'BeginReceive
                    Exit Property
    end sub
    
    
    #Region " Méthodes privées "
        Private Sub UdpReceiveMessage(ByVal IAr As IAsyncResult)
            Dim receiveBytes() As Byte
            Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
            receiveBytes = m_UdpClient.EndReceive(IAr, RemoteIpEndPoint)
            Dim strReceived As String = Nothing
            strReceived = Encoding.Default.GetString(receiveBytes)
            Dim para() As Object = {strReceived, RemoteIpEndPoint.Address.ToString}
            Me.Invoke(New m_DelegateUdpReceive(AddressOf Me.prRecerved), para) 'Thread receive winform 
            m_UdpClient.BeginReceive(AddressOf UdpReceiveMessage, Nothing) 'BeginReceive continue
        End Sub
        Private Sub prRecerved(ByVal paraSTR As String, ByVal pRemoteIP As String)
            Dim localAdd As IPAddress = IPAddress.Parse(Dns.GetHostAddresses(My.Computer.Name)(0).ToString)
            Dim myIP As String = Nothing
            myIP = localAdd.ToString
            Dim udpClient As New UdpClient(myIP, UDPPORTNO + 1)
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(paraSTR)
            Try
                udpClient.Send(sendBytes, sendBytes.Length)
            Catch e As Exception
                Console.WriteLine(e.ToString())
            End Try
            ta_richtext_box.text  = ta_richtext_box.text & paraSTR
    
        End Sub
    
    
        Private Sub ConnectedUsers()
            'UDP users
            Dim localAdd As IPAddress = IPAddress.Parse(Dns.GetHostAddresses(My.Computer.Name)(0).ToString)
            Dim myIP As String = Nothing
            myIP = localAdd.ToString
            If myIP = Nothing Then Exit Sub
            Dim pNum As Integer
            Dim strSplit() As String = Nothing
            strSplit = Split(myIP, ".")
            Dim tUdpClient As UdpClient
            tUdpClient = New UdpClient
            Dim sendBytes As Byte() = Encoding.Default.GetBytes("/CON" & myIP.ToString & "$" & "NickName")
    
            Me.Cursor = Cursors.WaitCursor
            For pNum = 1 To 254
                '192.xxx.xxx.1~254
                sRemoteIP = strSplit(0) & "." & strSplit(1) & "." & strSplit(2) & "." & pNum
                If sRemoteIP <> myIP Then
                    Try
                        Application.DoEvents()
                        tUdpClient.Connect(sRemoteIP, UDPPORTNO)
                        tUdpClient.Send(sendBytes, sendBytes.Length)
                    Catch ex As Exception
                        Debug.Print(ex.Message)
                    End Try
                End If
            Next pNum
            Erase strSplit
            tUdpClient.Close()
            tUdpClient = Nothing
            Me.Cursor = Cursors.Default
        End Sub
    end region

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 10
    Par défaut
    Je comprend pas comme t'inscrire la valeur de tout les packet reçu dans richtextbox2 ...
    Un peu plus de précision ?

  4. #4
    Membre éclairé Avatar de hugoclo
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    615
    Détails du profil
    Informations personnelles :
    Âge : 49
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Décembre 2007
    Messages : 615
    Par défaut
    la variable "paraSTR" correspond aux packet de données arrivé sur ton port
    donc pour la mettre dans ton richtextbox2 :
    remplace la ligne en rouge dans ce code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Private Sub prRecerved(ByVal paraSTR As String, ByVal pRemoteIP As String)
            Dim localAdd As IPAddress = IPAddress.Parse(Dns.GetHostAddresses(My.Computer.Name)(0).ToString)
            Dim myIP As String = Nothing
            myIP = localAdd.ToString
            Dim udpClient As New UdpClient(myIP, UDPPORTNO + 1)
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(paraSTR)
            Try
                udpClient.Send(sendBytes, sendBytes.Length)
            Catch e As Exception
                Console.WriteLine(e.ToString())
            End Try
            ta_richtext_box.text  = ta_richtext_box.text & paraSTR
    
        End Sub
    par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    richtextbox2.text = richtextbox2.text & paraSTR
    .
    Avec ce code ton richtextbox va s'implemanter à chaque arrivée de données.

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    10
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 10
    Par défaut
    Salut, je suppose que tout le monde connait wpe pro
    esceque on peut se servir de se code pour

    incrire la valeur des packet envoyer vers une ip genre 255.255.255.255:255 ?
    et les inscrire dans richtextbox3 ?

    sinon tu aurait un bon code pour faire cela ?
    merci d'avance

Discussions similaires

  1. gestion socket process votre avis
    Par seal3 dans le forum C++
    Réponses: 9
    Dernier message: 09/08/2010, 13h49
  2. Gestion des Sockets Protocole TCP/IP
    Par Julien_C++ dans le forum C++Builder
    Réponses: 6
    Dernier message: 04/08/2006, 15h12
  3. Réponses: 2
    Dernier message: 12/10/2004, 13h04
  4. Gestion de sockets: fonction Accept
    Par keupon dans le forum MFC
    Réponses: 12
    Dernier message: 22/01/2004, 18h48
  5. probleme de gestion de clients avec des sockets....
    Par ludvo dans le forum Réseau
    Réponses: 6
    Dernier message: 25/09/2003, 12h37

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