Gå til innhold

[Løst][VB 2008] Legge til items i listview1 - hjelp + admin hjelp i chat script.


Anbefalte innlegg

Hei!

 

Prøver meg på chat script via LAN. Og skal legge til navnene som connecter i listview1.

Alle meldinger blir sendt til en CMD vindu som kjører på egen maskin som igjen viser det i klient vinduet.

Men vil også legge til navnene i listview1 for de som connecter.

 

Kode for å hente meldinger:

   Private Sub getMessage()

       For infiniteCounter = 1 To 2

           infiniteCounter = 1

           serverStream = clientSocket.GetStream()

           Dim buffSize As Integer

           Dim inStream(10024) As Byte

           buffSize = clientSocket.ReceiveBufferSize

           serverStream.Read(inStream, 0, buffSize)

           Dim returndata As String = _

           System.Text.Encoding.ASCII.GetString(inStream)

           readData = "" + returndata

           msg()

       Next

   End Sub

 

Koden for server vinduet:

 

Imports System.Net.Sockets

Imports System.Text

Module Module1

   Dim clientsList As New Hashtable

   Sub Main()

       Dim serverSocket As New TcpListener(8888)

       Dim clientSocket As TcpClient

       Dim infiniteCounter As Integer

       Dim counter As Integer

 

       serverSocket.Start()

       msg("Chat Server Started ....")

       counter = 0

       infiniteCounter = 0

       For infiniteCounter = 1 To 2

           infiniteCounter = 1

           counter += 1

           clientSocket = serverSocket.AcceptTcpClient()

 

           Dim bytesFrom(10024) As Byte

           Dim dataFromClient As String

 

           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("$"))

 

           clientsList(dataFromClient) = clientSocket

 

           broadcast(dataFromClient + " Joined ", dataFromClient, False)

           broadcast(dataFromClient, dataFromClient, False)

 

           msg(dataFromClient + " Joined chat room ")

           Dim client As New handleClinet

           client.startClient(clientSocket, dataFromClient, clientsList)

       Next

 

       clientSocket.Close()

       serverSocket.Stop()

       msg("exit")

       Console.ReadLine()

   End Sub

 

   Sub msg(ByVal mesg As String)

       mesg.Trim()

       Console.WriteLine(" >> " + mesg)

   End Sub

   Private Sub broadcast(ByVal msg As String, _

   ByVal uName As String, ByVal flag As Boolean)

       Dim Item As DictionaryEntry

       For Each Item In clientsList

           Dim broadcastSocket As TcpClient

           broadcastSocket = CType(Item.Value, TcpClient)

           Dim broadcastStream As NetworkStream = _

                   broadcastSocket.GetStream()

           Dim broadcastBytes As [byte]()

 

           If flag = True Then

               broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)

           Else

               broadcastBytes = Encoding.ASCII.GetBytes(msg)

           End If

 

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

           broadcastStream.Flush()

       Next

   End Sub

   Public Class handleClinet

       Dim clientSocket As TcpClient

       Dim clNo As String

       Dim clientsList As Hashtable

 

       Public Sub startClient(ByVal inClientSocket As TcpClient, _

       ByVal clineNo As String, ByVal cList As Hashtable)

           Me.clientSocket = inClientSocket

           Me.clNo = clineNo

           Me.clientsList = cList

           Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)

           ctThread.Start()

       End Sub

 

       Private Sub doChat()

           Dim infiniteCounter As Integer

           Dim requestCount As Integer

           Dim bytesFrom(10024) As Byte

           Dim dataFromClient As String

           Dim sendBytes As [byte]()

           Dim serverResponse As String

           Dim rCount As String

           requestCount = 0

           For infiniteCounter = 1 To 2

               infiniteCounter = 1

               Try

                   requestCount = requestCount + 1

                   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("$"))

                   msg("From client - " + clNo + " : " + dataFromClient)

                   rCount = Convert.ToString(requestCount)

 

                   broadcast(dataFromClient, clNo, True)

               Catch ex As Exception

                   MsgBox(ex.ToString)

               End Try

           Next

       End Sub

 

   End Class

End Module

 

 

Kode for klient vinduet:

 

 

Imports System.Net.Sockets

Imports System.Text

Public Class Form1

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

   Dim serverStream As NetworkStream

   Dim readData As String

   Dim infiniteCounter As Integer

 

   Private Sub Button1_Click(ByVal sender As System.Object, _

       ByVal e As System.EventArgs) Handles Button1.Click

       Dim outStream As Byte() = _

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

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

       serverStream.Flush()

   End Sub

 

   Private Sub msg()

       If Me.InvokeRequired Then

           Me.Invoke(New MethodInvoker(AddressOf msg))

       Else

           TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData

       End If

   End Sub

 

   Private Sub Button2_Click(ByVal sender As System.Object, _

   ByVal e As System.EventArgs) Handles Button2.Click

       Try

           readData = "Conected to Chat Server ..."

           msg()

           clientSocket.Connect("127.0.0.1", 8888)

           Label1.Text = "Client Socket Program - Server Connected ..."

           serverStream = clientSocket.GetStream()

           clientsList.Items.Add(TextBox3.Text)

 

           Dim outStream As Byte() = _

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

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

           serverStream.Flush()

 

           Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)

           ctThread.Start()

 

       Catch ex As Exception

           TextBox1.Text = "Serveren er nede!"

       End Try

   End Sub

 

 

 

Håper noen vil ta seg tid ^^

 

EDIT:

Det med admin kan vi ta senere ^^

Endret av Gjest
Lenke til kommentar
Videoannonse
Annonse

Kommer ikke langt når man copy\paster koder fra andre nettsteder.

 

Iallefall ikke når man er helt fersk i VB.

 

Start fra begynnelsen og lær litt etter litt.

 

Sockets må du også lære deg til dette. :)

Endret av Merkoen
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...