Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ####################################################################################################
- ' ############################# CODE SOURCE UPLOAD LE 29/04/2012 - 15h40 #############################
- ' ##### http://www.commentcamarche.net/forum/affich-25062715-vb-net-sockets-crash-de-mon-serveur #####
- ' ####################################################################################################
- Imports System.IO
- Imports System.Net
- Imports System.Text
- Imports System.Net.Sockets
- Imports System.Threading
- Public Class Form1
- Delegate Sub SetTextCallback(ByVal text As String)
- Private Sub Message(ByVal text As String)
- If TextBox1.InvokeRequired Then
- Dim d As New SetTextCallback(AddressOf Message)
- Invoke(d, New Object() {text})
- Else
- Me.TextBox1.Text &= vbCrLf & text
- End If
- End Sub
- Delegate Sub SetListViewCallback(ByVal p1 As String, ByVal p2 As String)
- Private Sub ListView(ByVal p1 As String, ByVal p2 As String)
- Dim SubItemList As ListViewItem = New ListViewItem(New String() {p1, p2})
- If ListView1.InvokeRequired Then
- Dim d As New SetListViewCallback(AddressOf ListView)
- Invoke(d, New Object() {p1, p2})
- Else
- ListView1.Items.Add(SubItemList)
- End If
- End Sub
- Private Sub waitForClient()
- Dim serverSocket As New TcpListener(IPAddress.Parse("127.0.0.1"), 8888)
- Dim clientSocket As TcpClient
- Dim counter As Integer
- serverSocket.Start()
- counter = 0
- While (True)
- counter += 1
- clientSocket = serverSocket.AcceptTcpClient()
- Message("Client N°" + Convert.ToString(counter) + " -- Connected!")
- startClient(clientSocket, Convert.ToString(counter))
- End While
- clientSocket.Close()
- serverSocket.Stop()
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Dim mainThread As Threading.Thread = New Threading.Thread(AddressOf waitForClient)
- mainThread.Start()
- End Sub
- ' =======================================================================================
- Dim Command As String = "[NONE]"
- Dim Destinataire As Integer = 0
- Dim clientSocket As TcpClient
- Dim clientNumber As String
- Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clientNumberF As String)
- clientSocket = inClientSocket
- clientNumber = clientNumberF
- Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
- ctThread.Start()
- End Sub
- Private Sub doChat()
- Dim bytesFrom(10024) As Byte
- Dim dataFromClient As String
- Dim sendBytes As [Byte]()
- Dim serverResponse As String
- While 1
- Try
- Dim networkStream As NetworkStream = clientSocket.GetStream()
- networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
- dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
- dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
- If dataFromClient.Contains("header") Then
- Dim strArr() As String
- strArr = dataFromClient.Split("|")
- ListView(strArr(1), strArr(2))
- End If
- If Destinataire = clientNumber Or Destinataire = 0 Then
- serverResponse = Command
- Command = "[NONE]"
- Destinataire = 0
- Else
- serverResponse = "[NONE]"
- End If
- sendBytes = Encoding.ASCII.GetBytes(serverResponse)
- networkStream.Write(sendBytes, 0, sendBytes.Length)
- networkStream.Flush()
- If Not serverResponse.Contains("[NONE]") Then
- Message("To Client N°" & clientNumber & ": " & serverResponse)
- End If
- Thread.Sleep(500)
- Catch ex As Exception
- MsgBox(ex.ToString)
- End Try
- End While
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment