Orci76

[VB.NET] Problème avec les Sockets (CCM)

Apr 29th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.22 KB | None | 0 0
  1. ' ####################################################################################################
  2. ' ############################# CODE SOURCE UPLOAD LE 29/04/2012 - 15h40 #############################
  3. ' ##### http://www.commentcamarche.net/forum/affich-25062715-vb-net-sockets-crash-de-mon-serveur #####
  4. ' ####################################################################################################
  5.  
  6. Imports System.IO
  7. Imports System.Net
  8. Imports System.Text
  9. Imports System.Net.Sockets
  10. Imports System.Threading
  11.  
  12. Public Class Form1
  13.     Delegate Sub SetTextCallback(ByVal text As String)
  14.     Private Sub Message(ByVal text As String)
  15.         If TextBox1.InvokeRequired Then
  16.             Dim d As New SetTextCallback(AddressOf Message)
  17.             Invoke(d, New Object() {text})
  18.         Else
  19.             Me.TextBox1.Text &= vbCrLf & text
  20.         End If
  21.     End Sub
  22.  
  23.     Delegate Sub SetListViewCallback(ByVal p1 As String, ByVal p2 As String)
  24.     Private Sub ListView(ByVal p1 As String, ByVal p2 As String)
  25.         Dim SubItemList As ListViewItem = New ListViewItem(New String() {p1, p2})
  26.         If ListView1.InvokeRequired Then
  27.             Dim d As New SetListViewCallback(AddressOf ListView)
  28.             Invoke(d, New Object() {p1, p2})
  29.         Else
  30.             ListView1.Items.Add(SubItemList)
  31.         End If
  32.     End Sub
  33.  
  34.     Private Sub waitForClient()
  35.         Dim serverSocket As New TcpListener(IPAddress.Parse("127.0.0.1"), 8888)
  36.         Dim clientSocket As TcpClient
  37.         Dim counter As Integer
  38.  
  39.         serverSocket.Start()
  40.         counter = 0
  41.         While (True)
  42.             counter += 1
  43.             clientSocket = serverSocket.AcceptTcpClient()
  44.             Message("Client N°" + Convert.ToString(counter) + " -- Connected!")
  45.             startClient(clientSocket, Convert.ToString(counter))
  46.         End While
  47.         clientSocket.Close()
  48.         serverSocket.Stop()
  49.     End Sub
  50.  
  51.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  52.         Dim mainThread As Threading.Thread = New Threading.Thread(AddressOf waitForClient)
  53.         mainThread.Start()
  54.     End Sub
  55.  
  56.     ' =======================================================================================
  57.  
  58.     Dim Command As String = "[NONE]"
  59.     Dim Destinataire As Integer = 0
  60.     Dim clientSocket As TcpClient
  61.     Dim clientNumber As String
  62.  
  63.     Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clientNumberF As String)
  64.         clientSocket = inClientSocket
  65.         clientNumber = clientNumberF
  66.         Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
  67.         ctThread.Start()
  68.     End Sub
  69.  
  70.     Private Sub doChat()
  71.         Dim bytesFrom(10024) As Byte
  72.         Dim dataFromClient As String
  73.         Dim sendBytes As [Byte]()
  74.         Dim serverResponse As String
  75.         While 1
  76.             Try
  77.                 Dim networkStream As NetworkStream = clientSocket.GetStream()
  78.                 networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
  79.                 dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
  80.                 dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
  81.                 If dataFromClient.Contains("header") Then
  82.                     Dim strArr() As String
  83.                     strArr = dataFromClient.Split("|")
  84.                     ListView(strArr(1), strArr(2))
  85.                 End If
  86.                 If Destinataire = clientNumber Or Destinataire = 0 Then
  87.                     serverResponse = Command
  88.                     Command = "[NONE]"
  89.                     Destinataire = 0
  90.                 Else
  91.                     serverResponse = "[NONE]"
  92.                 End If
  93.  
  94.                 sendBytes = Encoding.ASCII.GetBytes(serverResponse)
  95.                 networkStream.Write(sendBytes, 0, sendBytes.Length)
  96.                 networkStream.Flush()
  97.                 If Not serverResponse.Contains("[NONE]") Then
  98.                     Message("To Client N°" & clientNumber & ": " & serverResponse)
  99.                 End If
  100.                 Thread.Sleep(500)
  101.             Catch ex As Exception
  102.                 MsgBox(ex.ToString)
  103.             End Try
  104.         End While
  105.     End Sub
  106. End Class
Advertisement
Add Comment
Please, Sign In to add comment