Jeg har denne kode, hvor "antal" skal tælle en op hver gang en client logger på serveren. Men dette sker ikke. antal bliver nul stillet vært gang der kommer en ny client.. Hvordan går jeg antal til at tælle op?
Class ServerText Public Shared Sub Main(ByVal args As String()) ' lyt på port 12345 Dim server As TcpListener = New TcpListener(IPAddress.Any, 12345) server.Start()
While True ' accepter connection fra client Dim cli As TcpClient = server.AcceptTcpClient ' start tråd til at håndtere client Dim tch As TextClientHandler = New TextClientHandler(cli) Dim t As Thread = New Thread(AddressOf tch.Run) t.Start() End While End Sub End Class
Class TextClientHandler Dim antal As Integer
Private cli As TcpClient Private read As StreamReader Private write As StreamWriter
Public Sub New(ByVal cli As TcpClient) ' initialiser læse og skrive klasser Me.cli = cli read = New StreamReader(cli.GetStream) write = New StreamWriter(cli.GetStream) End Sub
Public Sub Run() antal = antal + 1 MessageBox.Show(antal)
read.Close() write.Close() cli.Close() End Sub End Class
Imports System Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Threading
Module Module1 Class ServerText Public Shared Sub Main(ByVal args As String()) ' lyt på port 12345 Dim server As TcpListener = New TcpListener(IPAddress.Any, 12345) server.Start()
While True ' accepter connection fra client Dim cli As TcpClient = server.AcceptTcpClient ' start tråd til at håndtere client Dim tch As TextClientHandler = New TextClientHandler(cli) Dim t As Thread = New Thread(AddressOf tch.Run) t.Start() End While End Sub End Class
Dim antal As Integer
Class TextClientHandler Private cli As TcpClient Private read As StreamReader Private write As StreamWriter
Public Sub New(ByVal cli As TcpClient) ' initialiser læse og skrive klasser Me.cli = cli read = New StreamReader(cli.GetStream) write = New StreamWriter(cli.GetStream) End Sub
Public Sub Run() antal = antal + 1 MessageBox.Show(antal)
read.Close() write.Close() cli.Close() End Sub End Class End Module
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.