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
| Imports System
Imports System.Net
Imports System.Text
Imports System.ComponentModel
Imports System.Threading
Public Class Form1
'---------- BackGroundWorker
'Nous allons ensuite déclarer notre BackGroundWorker, un Delegate qui nous permettra d'appeller notre fonction Increment à l'intérieur du Thread et un Event que nous lèverons à la fin du Thread.
Private WithEvents bgwCompte As BackgroundWorker
Private Delegate Function dIncrement(ByVal nbr As Integer) As Integer
Private Event bgwTermine(ByVal nbr As Integer)
'---------- Définition du serveur
Dim udpClient As New Sockets.UdpClient
'---------- Définition pour la Transmission d'un message
Dim buffer(256) As Byte
Dim data As String
Dim fluxDonnées As System.Net.Sockets.NetworkStream
Dim nbreOctets As Integer
'---------- Definitions pour gestion des erreurs
Dim msg As String = "Déconecté"
Dim ipAdresse As IPAddress
'-------- Définition des paramètres de la communication
'Adresse Ip du serveur
Dim srtAdresesIP As String = "10.10.33.102"
'Definition du numéro du port
Dim numPort As Integer = 1002
'---------- Crée le serveur TCP
Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click
Try
srtAdresesIP = textIP.Text
numPort = textPort.Text
'Mise au format de l'adresse IP
Dim ipAdresse As IPAddress = IPAddress.Parse(srtAdresesIP)
'----------- Traitement
'------- Ecoute des demandes de connexion
udpClient.Connect(ipAdresse, numPort)
' IPEndPoint object will allow us to read datagrams sent from any source.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
'Reception de la donnée
Dim buffer As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
' Converti les valeurs ASCII en chaine de caractères
data = Encoding.ASCII.GetString(buffer)
'-------Affiche le message reçu
TextMessage.Enabled = True
TextMessage.Text = data
Catch ex As Exception
msg = "Erreur " & ex.Message
LabelEtat.Text = msg
End Try
End Sub
Private Sub Deconect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deconect.Click
udpClient.Close()
fluxDonnées.Close()
LabelEtat.Text = "Déconnecté"
TextMessage.Text = ""
textIP.Text = "10.10.33.102"
textPort.Text = "1002"
TextMessage.Enabled = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
textIP.Text = srtAdresesIP
textPort.Text = numPort.ToString()
LabelEtat.Text = msg
Me.FormBorderStyle = FormBorderStyle.FixedSingle
End Sub
End Class |
Partager