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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
'*************************************************SERVEUR****************************************************'
Imports System.Net.Sockets
Imports System.Threading
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Dim Serveur As TcpListener
Dim Clients As New Hashtable
Dim ThreadServeur As Thread
Dim ClientIP As IPEndPoint
Private Structure NClient
Public SocketClient As Socket
Public ThreadClient As Thread
Public Message As String
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
Serveur = New TcpListener(IPAddress.Any, TextBox1.Text)
Serveur.Start()
ThreadServeur = New Thread(AddressOf Listening)
ThreadServeur.Start()
ListBox1.Items.Add("Server lancé et en attente de connexion...")
End Sub
Public Sub Listening()
Dim Client As New NClient
While True
Try
Client.SocketClient = Serveur.AcceptSocket
ClientIP = Client.SocketClient.RemoteEndPoint
Client.ThreadClient = New Thread(AddressOf Lecture)
Clients.Add(ClientIP, Client)
NConnexion(ClientIP)
Client.ThreadClient.Start()
Catch ex As Exception
End Try
End While
End Sub
Public Sub Lecture()
Dim Client As New NClient
Dim DATAS() As Byte
Dim IP As IPEndPoint = ClientIP
Client = Clients(IP)
While True
If Client.SocketClient.Connected Then
DATAS = New Byte(100) {}
Try
If Client.SocketClient.Receive(DATAS, DATAS.Length, 0) > 0 Then
Client.Message = Encoding.UTF7.GetString(DATAS)
Clients(IP) = Client
Data_Recue(IP)
Else
ConnexionFinie(IP)
Exit While
End If
Catch ex As Exception
ConnexionFinie(IP)
Exit While
End Try
End If
End While
Call Fermer_Le_Thread(IP)
End Sub
Public Sub Fermer_Le_Thread(ByVal IP As IPEndPoint)
Dim Client As NClient = Clients(IP)
Try
Client.ThreadClient.Abort()
Catch ex As Exception
Clients.Remove(IP)
End Try
End Sub
Private Sub NConnexion(ByVal IDTerminal As IPEndPoint)
ListBox1.Items.Add(vbCrLf & "Connexion de " & "User...")
RichTextBox1.ScrollToCaret()
ListBox1.Items.Add("User en ligne !")
SendAll("Connecté !")
End Sub
Private Sub ConnexionFinie(ByVal IDTerminal As IPEndPoint)
Try
ListBox1.Items.Add(vbCrLf & "Déconnexion de " & "User...")
RichTextBox1.ScrollToCaret()
ListBox1.Items.Remove("User en ligne !")
SendAll("Déconnecté !")
Catch ex As Exception
End Try
End Sub
Private Sub Data_Recue(ByVal IDTerminal As IPEndPoint)
RichTextBox1.AppendText(vbCrLf & "User" & " : ")
RichTextBox1.AppendText(Data_Obtenue(IDTerminal))
RichTextBox1.ScrollToCaret()
SendAll("User" & " : " & Data_Obtenue(IDTerminal))
End Sub
Public Function Data_Obtenue(ByVal IDClient As IPEndPoint) As String
Dim Client As NClient
Client = Clients(IDClient)
Return Client.Message
End Function
Public Sub CreateOne(ByVal IDClient As IPEndPoint)
Dim Client As NClient
Client = Clients(IDClient)
Client.SocketClient.Close()
Client.ThreadClient.Abort()
End Sub
Public Sub CreateAll()
Dim Client As NClient
For Each Client In Clients.Values
Client.SocketClient.Close()
Client.ThreadClient.Abort()
Next
End Sub
Public Sub SendOne(ByVal IDClient As IPEndPoint, ByVal DATAS As String)
Dim Client As NClient
Client = Clients(IDClient)
Client.SocketClient.Send(Encoding.UTF7.GetBytes(DATAS))
End Sub
Public Sub SendAll(ByVal DATAS As String)
Dim Client As NClient
For Each Client In Clients.Values
Client.SocketClient.Send(Encoding.UTF7.GetBytes(DATAS))
Next
End Sub
Public Property SelectionStart As Integer
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
SendAll("Server : " & TextBox2.Text)
RichTextBox1.AppendText(vbCrLf & "Server : " & TextBox2.Text)
RichTextBox1.ScrollToCaret()
TextBox2.Clear()
End If
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
SendAll("Sever déconnecté !")
CreateAll()
Serveur.Stop()
ThreadServeur.Abort()
End Sub
End Class
'**************************************************CLIENT*****************************************************'
Imports System.IO
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Net
Public Class Form1
Private MonStream As Stream
Dim Client As TcpClient
Dim ServeurThread As Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
CheckForIllegalCrossThreadCalls = False
Client = New TcpClient
Client.Connect(TextBox3.Text, TextBox2.Text)
MonStream = Client.GetStream
ServeurThread = New Thread(AddressOf Lecture)
ServeurThread.Start()
RichTextBox1.AppendText("Connection au Server...")
Button1.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Lecture()
Dim MonBuffer() As Byte
While True
Try
MonBuffer = New Byte(100) {}
MonStream.Read(MonBuffer, 0, MonBuffer.Length)
Dim Message As String = Encoding.UTF7.GetString(MonBuffer)
If Message.Contains("ERREUR") Then
Else
RichTextBox1.AppendText(vbCrLf & Message)
RichTextBox1.ScrollToCaret()
If Message.StartsWith("Connexion:") Then
Dim MaDevision As New ArrayList(Message.Split(":"))
ListBox1.Items.Add(MaDevision(1))
ElseIf Message.StartsWith("Déconnexion:") Then
Dim MaDevision As New ArrayList(Message.Split(":"))
ListBox1.Items.Remove(MaDevision(1))
End If
End If
Catch ex As Exception
Exit While
End Try
End While
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
If TextBox1.Text <> Nothing Then
Envoyer(TextBox1.Text)
TextBox1.Clear()
TextBox1.Clear()
End If
End If
End Sub
Public Sub Envoyer(ByVal Message As String)
Try
Dim MonBuffer() As Byte
MonBuffer = Encoding.UTF7.GetBytes(Message)
MonStream.Write(MonBuffer, 0, MonBuffer.Length)
TextBox1.Clear()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Try
Client.Close()
ServeurThread.Abort()
Catch ex As Exception
End Try
End Sub
End Class |
Partager