|  
    
 Connaître comment coder un salon de discussion sans l'aide d'une base de données peut vous aider à créer un serveur /application client sans avoir à passer du temps à la conception d'une base de données. Un programme de chat est créé à l'aide de deux applications , le client et le serveur. L'application serveur se chargera de toutes les connexions client pour établir la communication . Le programme client est installé sur n'importe quel ordinateur souhaitant se connecter au serveur pour discuter avec d'autres clients. Vous pouvez créer cette application en utilisant Microsoft Visual Basic.NET , également connu sous VB.NET . Choses que vous devez   Microsoft Visual Basic Express   Voir Plus Instructions   1  Ouvrez Microsoft Visual Basic Express, cliquez sur "Nouveau projet ... " Dans le volet gauche de votre écran , puis sélectionnez " Application console. " Cliquez sur « OK ».   2   Appuyez sur "Ctrl " et "A ", puis appuyez sur «Supprimer» pour enlever tout le code existant. Copiez et collez le code ci-dessous dans votre " Module1.vb " pour créer l'application serveur : Photos   importations System.Net.Sockets     importations System.Text     Module Module1     hshTbl As New Hashtable     Sub Main ( )     svrSckt As New TcpListener ( 8888)     TClient As TcpClient     infntCntr As Integer     CNTR As Integer     svrSckt.Start ()     msg ( " serveur de démarrage pour bavarder .... " )     CNTR = 0     infntCntr = 0     infntCntr = 1 To 2     infntCntr = 1     CNTR + = 1     TClient = svrSckt.AcceptTcpClient ()     Dim btsFrom ( 10024 ) As Byte     clientData Dim As String     NetworkStream As NetworkStream = _     tClient.GetStream ()     networkStream.Read ( btsFrom , 0, CInt ( tClient.ReceiveBufferSize ) )     clientData = System.Text.Encoding.ASCII.GetString ( btsFrom )     clientData = _     clientData.Substring (0, clientData.IndexOf ("$" ) )     hshTbl ( clientData ) = TClient     diffusion ( clientData + " a rejoint " , clientData , Faux )     msg ( clientData + " a rejoint le chat room" )     client As New handleClinet     client.startClient ( TClient , clientData , Sous     Sous msg ( msg ByVal As String )     msg.Trim ()     Console.WriteLine ( ">>" + msg)     End Sub     diffusion Private Sub (msg ByVal As String , _     usrname ByVal As String , ByVal FLG As Boolean )     dicEntry As DictionaryEntry     Pour chaque dicEntry En hshTbl     broadcastSocket As TcpClient     broadcastSocket = C ( dicEntry.Value , TcpClient )     broadcastStream As NetworkStream = _     broadcastSocket.GetStream ()     broadcastBytes As [ Byte ] ()     Si FLG = true Ensuite     broadcastBytes = Encoding.ASCII.GetBytes ( usrname + " dit: " + msg)     Else     broadcastBytes = Encoding.ASCII.GetBytes (MSG)     End If     broadcastStream.Write ( broadcastBytes , 0, broadcastBytes . longueur )     broadcastStream.Flush ()     Suivant     End Sub     public Class handleClinet     TClient As TcpClient     Dim ClNO As String     hshTbl As Hashtable     public Sub startClient ( ByVal inClientSocket Comme TcpClient , _     clnNum ByVal As String , ByVal hsTbl Comme Hashtable )     Me.tClient = inClientSocket     Me.clNo = clnNum     Me.hshTbl = hsTbl     ctThread As Threading.Thread = New Threading.Thread ( AddressOf doChat )     ctThread.Start ()     End Sub     doChat Private Sub ()     infntCntr As Integer Dim     rqstCnt As Integer     Dim btsFrom ( 10024 ) As Byte     clientData Dim As String     btsSend As [ Byte ] ()     de responseSrvr Dim As String Photos   RCNT Dim As String     rqstCnt = 0     infntCntr = 1 To 2     infntCntr = 1     essayer et   rqstCnt = rqstCnt + 1     NetworkStream As NetworkStream = _     tClient.GetStream ()     networkStream.Read ( btsFrom , 0, CInt ( tClient.ReceiveBufferSize ) )     clientData = System.Text.Encoding.ASCII.GetString ( btsFrom )     clientData = _     clientData.Substring (0, clientData.IndexOf ("$" ) )     msg ( " De client :" + ClNO + ":" + clientData )     RCNT = Convert.ToString ( rqstCnt )     diffusion ( clientData , ClNO , vrai)     Catch ex As Exception     MsgBox ( ex.ToString )     Fin essayer et   Suivant     End Sub     End Class of   End Module   3   Ouvrez une nouvelle instance de Microsoft Visual Basic express, cliquez sur "Nouveau projet ..." sur le volet de gauche de votre écran , puis sélectionnez " applications Windows Forms . " Cliquez sur "OK". Cliquez sur le volet " boîte à outils " , puis double -cliquez sur " TextBox " pour ajouter une nouvelle zone de texte . Ajoutez deux zones de texte en utilisant la même méthode. Ajoutez deux boutons de le menu " Outils " .   4   Double- cliquez sur le formulaire , appuyez sur " Ctrl" et " A ", puis appuyez sur «Supprimer» pour enlever tout le code existant. Copier et coller le code ci-dessous dans votre "Form1.vb" module pour créer le programme client : Photos   importations System.Net.Sockets     Imports System.Text     public Class Form1     Dim clntSckt As New System.Net.Sockets.TcpClient ()     srvrStrm As NetworkStream     rddata Dim As String     infCntr As Integer     Private Sub Button1_Click ( expéditeur de ByVal comme System.Object , _     ByVal e As System.EventArgs ) Handles Button1.Click     StreamOut As Byte () = _     System.Text . Encoding.ASCII.GetBytes ( TextBox2.Text + "$")     srvrStrm.Write ( StreamOut , 0, streamOut.Length )     srvrStrm.Flush ()     End Sub     msg Private Sub ()     Si Me.InvokeRequired Puis     Me.Invoke (New MethodInvoker ( AddressOf msg) )     Else < br >   TextBox1.Text = TextBox1.Text + Environment.NewLine + ">>" + rddata     End If End Sub         Private Sub Button2_Click ( expéditeur de ByVal comme System.Object , _     ByVal e As System.EventArgs ) Handles Button2.Click     rddata = " Les liens entre l' serveur de chat ... "   msg (produits )     clntSckt.Connect ( "127.0.0.1" , 8888)     srvrStrm = clntSckt.GetStream ()     StreamOut As Byte () = _   < p> System.Text.Encoding.ASCII.GetBytes ( TextBox3.Text + "$")     srvrStrm.Write ( StreamOut , 0, streamOut.Length )     srvrStrm.Flush ()     ctThread As Threading.Thread = New Threading.Thread ( AddressOf getMessage )     ctThread.Start ()     End Sub Private Sub     getMessage () Photos   infCntr = 1 To 2     infCntr = 1     srvrStrm = clntSckt.GetStream ()     de buffSize As Integer     inStream Dim ( 10024 ) As Byte     buffSize = clntSckt.ReceiveBufferSize     srvrStrm.Read ( cours d'eau , 0, buffSize )     returndata As String = _   < p> System.Text.Encoding.ASCII.GetString ( cours d'eau )     rddata = "" + returndata     msg ()     Ensuite     End Sub Photos   End Class   5   Exécutez le programme serveur abord en appuyant sur la touche "F5 ". Démarrez le programme client en appuyant sur la touche "F5 ". Tapez votre nom dans " Textbox3 », puis cliquez sur " Button2 " pour établir une connexion avec le programme serveur .   6   Tapez un message dans" TextBox2 , " puis cliquez sur " Button1 "pour envoyer votre message. Ce message sera affiché dans la fenêtre de sortie de l' serveur.    
 
 |