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
| Public Class Serveur
Private Sub Serveur_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ecoute As TcpListener = Nothing
Dim ProcessEcoute As Thread = New Thread(New ThreadStart(AddressOf New Ecoute(ecoute).Run))
ProcessEcoute.Start()
End Sub
Public Sub AfficheEtat(ByVal Trame As String)
Etat.Rtf = "{\rtf1\ansi " & Trame & ".}"
End Sub
End Class
Public Class Service
Private liaisonClient As TcpClient
Private nbClient As Integer
Private [IN] As StreamReader
Private OUT As StreamWriter
Public Sub New(ByVal liaisonClient As TcpClient, ByVal nbClient As Integer)
Me.liaisonClient = liaisonClient
Me.nbClient = nbClient
End Sub
Public Sub Run()
Try
[IN] = New StreamReader(liaisonClient.GetStream())
OUT = New StreamWriter(liaisonClient.GetStream())
OUT.AutoFlush = True
Dim demande As String = Nothing
Dim reponse As String = Nothing
demande = [IN].ReadLine
While Not (demande Is Nothing)
...
reponse = "[" + demande + "]"
OUT.WriteLine(reponse)
demande = [IN].ReadLine
End While
liaisonClient.Close()
Catch e As Exception
...
End Try
End Sub
End Class
Public Class Ecoute
Private ecoute As TcpListener
Private nbClient As Integer = 0
Private port As Integer = 10000
Private Trame As String
Public Sub New(ByVal ecoute As TcpListener)
Me.ecoute = ecoute
End Sub
Public Sub Run()
Try
ecoute = New TcpListener(IPAddress.Parse("127.0.0.1"), port)
ecoute.Start()
Trame = "Démarrage du serveur (Ecoute en Cours)"
Serveur.AfficheEtat(Trame)
Dim liaisonClient As TcpClient = Nothing
While True
liaisonClient = ecoute.AcceptTcpClient()
nbClient = nbClient + 1
Trame = "connexion du client : " & nbClient
Serveur.AfficheEtat(Trame)
Dim tache As Thread = New Thread(New ThreadStart(AddressOf New Service(liaisonClient, nbClient).Run))
tache.Start()
End While
Catch ex As Exception
MsgBox("Une erreur est survenue sur la socket d'écoute : " + ex.Message, MsgBoxStyle.OkOnly, "Erreur")
End Try
End Sub
End Class |
Partager