Kuidas teha Chat Room Visual Basic Express

võttes teadmisi, kuidas luua jututuba kasutades Microsoft Visual Basic Express saab säästa aega, kui teil on vaja luua jututuba rakendus.Visual Basic Express on saadaval tasuta kõigile huvilistele luua rakendusi kasutades VB.NET.Jututuba hõlmab kaks taotlust, server ja klient.Server töötleb kliendi ühendused ja kliendi rakendus ühendab kasutajad serveri ruumi sõnumite saatmiseks.

juhised

  1. Start Microsoft Visual Basic Express, ja seejärel "Uus projekt."Vasakul paanil ekraani, valige "Console Application."Vajuta "OK."

  2. Press "Ctrl" + "A" ja seejärel vajutage "Kustuta".Kopeeri ja kleebi järgmine kood oma "Module1.vb" luua server programm:

    Import System.Net.Sockets

    Import System.Text

    Module Module1

    Dim clientsList Nagu New Hashtable

    Sub Main ()

    Dim ServerSocket Nagu New TcpListener (8888)

    Dim clientSocket Nagu TcpClient

    Dim infiniteCounter Nagu Integer

    Dim counter Nagu Integer

    serverSocket.Start ()

    sõnum ("Chat Server Alusta ....")

    counter= 0

    infiniteCounter = 0

    For infiniteCounter = 1 2

    infiniteCounter = 1

    counter + = 1

    clientSocket = serverSocket.AcceptTcpClient ()

    Dim bytesFrom (10024) Kuna bait

    Dim dataFromClient Nagu String

    Dim networkStream Nagu NetworkStream = _

    clientSocket.GetStream ()

    networkStream.Read (bytesFrom, 0, CINT (clientSocket.ReceiveBufferSize))

    dataFromClient = System.Text.Encoding.ASCII.GetString (bytesFrom)

    dataFromClient = _

    dataFromClient.Substring (0, dataFromClient.IndexOf ("$"))

    clientsList (dataFromClient) = clientSocket

    saade (dataFromClient + "Liitu", dataFromClient, False)

    sõnum (dataFromClient + "Liitunudjututuba ")

    Dim kliendi Nagu New handleClinet

    client.startClient (clientSocket, dataFromClient, clientsList)

    Järgmine

    clientSocket.Close ()

    serverSocket.Stop ()

    sõnum (" exit ")

    Console.ReadLine ()

    End Sub

    Sub sõnum (ByVal katseline ohutuspilu Nagu String)

    mesg.Trim ()

    else ("& gt; & gt;"+ Katseline ohutuspilu)

    End Sub

    Private Sub saade (ByVal msg Nagu String, _

    ByVal uname Nagu String, ByVal lipu Boole'i)

    Dim objekti DictionaryEntry

    Iga elemendi juures clientsList

    Dim broadcastSocket NaguTcpClient

    broadcastSocket = CType (Item.Value, TcpClient)

    Dim broadcastStream Nagu NetworkStream = _

    broadcastSocket.GetStream ()

    Dim broadcastBytes Nagu [bait] ()

    Kui flag = True Siis

    broadcastBytes = kodeerimine.ASCII.GetBytes (uname + "ütleb:" + msg)

    Else

    broadcastBytes = Encoding.ASCII.GetBytes (sõnum)

    End Kui

    broadcastStream.Write (broadcastBytes, 0, broadcastBytes.Length)

    broadcastStream.Flush ()

    Järgmine

    End Sub

    Public klassi handleClinet

    Dim clientSocket Nagu TcpClient

    Dim clNo Nagu String

    Dim clientsList Nagu Hashtable

    Public Sub startClient (ByVal inClientSocket Nagu TcpClient, _

    ByVal clineNoNagu String, ByVal cList Nagu Hashtable)

    Me.clientSocket = inClientSocket

    Me.clNo = clineNo

    Me.clientsList = cList

    Dim ctThread Nagu Threading.Thread = New Threading.Thread (addressof doChat)

    ctThread.Start ()

    End Sub

    Private Sub doChat ()

    Dim infiniteCounter Nagu Integer

    Dim requestCount Nagu Integer

    Dim bytesFrom (10024) Kuna bait

    Dim dataFromClient Nagu String

    Dim sendBytes Nagu [bait] ()

    Dim serverResponse Nagu String

    Dim rCount Nagu String

    requestCount = 0

    For infiniteCounter = 1 2

    infiniteCounter = 1

    Proovige

    requestCount = requestCount + 1

    Dim networkStream Nagu NetworkStream = _

    clientSocket.GetStream ()

    networkStream.Read (bytesFrom, 0, CINT (clientSocket.ReceiveBufferSize))

    dataFromClient = System.Text.Encoding.ASCII.GetString (bytesFrom)

    dataFromClient = _

    dataFromClient.Substring (0, dataFromClient.IndexOf ("$"))

    sõnum ("From klient -" + clNo + ":" + dataFromClient)

    rCount = Convert.ToString (requestCount)

    saade (dataFromClient, clNo, True)

    Catch ex Nagu Exception

    MsgBox (ex.ToString)

    End Püüa

    Järgmine

    End Sub

    End klassi

    End Module

  3. Start Microsoft Visual Basic Express, ja seejärel "Uus projekt."Vasakul paanil ekraani, valige "Windows Forms taotlemine." Click "OK." Vajuta "Toolbox" pane, ja seejärel topeltklõpsake "tekstikasti" lisada uus tekst kasti.Lisa veel kaks teksti kasti.Lisa kaks nuppe "Toolbox" menüüs.

  4. topeltklõps kujul ja seejärel vajutage "Ctrl" + "A."Vajuta "Kustuta".Kopeeri ja kleebi järgmine kood oma "Form1.vb" moodul luua kliendi programm:

    Import System.Net.Sockets

    Import System.Text

    Public klassi Form1

    Dim clientSocket Nagu New System.Net.Sockets.TcpClient ()

    Dim serverStream Nagu NetworkStream

    Dim readData Nagu String

    Dim infiniteCounter Nagu Integer

    Private Sub Button1_Click (ByVal saatja Nagu System.Object, _

    ByVal e Nagu System.EventArgs) käepidemed Button1.Click

    DimoutStream Nagu bait () = _

    System.Text.Encoding.ASCII.GetBytes (TextBox2.Text + "$")

    serverStream.Write (outStream, 0, outStream.Length)

    serverStream.Flush ()

    End Sub

    Private Sub-sõnum ()

    Kui Me.InvokeRequired Siis

    Me.Invoke (New MethodInvoker (addressof msg))

    Else

    TextBox1.Text = TextBox1.Text + Environment.NewLine + "& gt; & gt;"+ ReadData

    End Kui

    End Sub

    Private Sub Button2_Click (ByVal saatja Nagu System.Object, _

    ByVal e Nagu System.EventArgs) käepidemed Button2.Click

    readData =" conected Vestluse Server ..."

    sõnum ()

    clientSocket.Connect (" 127.0.0.1 ", 8888)

    " Label1.Text = "Kliendi Socket Program - Server ühendatud ..."

    serverStream = clientSocket.GetStream ()

    DimoutStream Nagu bait () = _

    System.Text.Encoding.ASCII.GetBytes (TextBox3.Text + "$")

    serverStream.Write (outStream, 0, outStream.Length)

    serverStream.Flush ()

    Dim ctThread Nagu Threading.Thread = New Threading.Thread (addressof getMessage)

    ctThread.Start ()

    End Sub

    Private Sub getMessage ()

    For infiniteCounter = 1 2

    infiniteCounter = 1

    serverStream =clientSocket.GetStream ()

    Dim buffSize Nagu Integer

    Dim voogesitusevaheliste (10024) Kuna bait

    buffSize = clientSocket.ReceiveBufferSize

    serverStream.Read (voogesitusevaheliste, 0, buffSize)

    Dim returndata Nagu String = _

    süsteemi.Text.Encoding.ASCII.GetString (voogesitusevaheliste)

    readData = "" + returndata

    sõnum ()

    Järgmine

    End Sub

    End klassi

  5. Run server programmi esimene vajutades "F5",ja seejärel käivitage klient programmi.Sisestage oma nime "Textbox3" ja vajuta "Button2" ühendada server.Sisestage sõnum "TextBox2," ja seejärel "Button1", et saata sõnum.

755
0
2
Visual Basic Programmeerimine