Newsgroup: it.comp.lang.visual-basic
Titolo: [VB.NET] Servizio Chat server

Sto uscendo pazzo !!
Qualcuno mi puï¿œ tradurre questo progetto c# in 
vb.net
 ?
http://www.codeproject.com/KB/IP/Chat_Application/Chat_Application.zip
Per favore, ho provato a tradurlo io ma non va.
Il servizio parte, non da errori in compilazione ma il client non si 
connette :)
Grazie di cuore :)
Alverman

 ha scritto nel messaggio 
news:201001200...@mynewsgate.net...

Se proprio non (p/v)uoi investire un po' di tempo apprendendo il C#, prova 
questo simpatico tool:
http://www.developerfusion.com/tools/convert/csharp-to-vb
Bye
Raf 

 
http://www.developerfusion.com/tools/convert/csharp-to-vb
Conosco conosco ...... ho usato quello per convertirlo !!
Grazie comunque per la dritta
Alverman

Insomma, non c'ï¿œ modo di far partire questo server dal servizio !!
In on start ho messo questo ma quando lancio il servizio si inchioda:
        Const portNumber As Integer = 1544
        Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
            'Accept the pending client connection and 
return             'a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt
(tcpClient.ReceiveBufferSize))
            ' Return the data received from the client to the console.
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            Dim responseString As String = "Connected to server."
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes
(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            'Any communication with the remote client using the 
TcpClient can go here.
            'Close TcpListener and TcpClient.
            tcpClient.Close()
            tcpListener.Stop()
    End Sub
Mi dite perchï¿œ per favore ?
Grazie, Alverman

Altra prova che non funziona :)
Mi date una mano per favore ?
Allego tutto quello che c'ï¿œ nel servizio:
Imports System.Threading
Imports System.Net.Sockets
Imports System.Diagnostics.EventLog
Public Class Service5
    Dim clientsList As New Hashtable
    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim serverSocket As New TcpListener(8888)
        Dim clientSocket As TcpClient
        Dim infiniteCounter As Integer
        Dim counter As Integer
        Dim b As New handleClient
        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)
            msg(dataFromClient + " Joined chat room ")
            Dim client As New handleClient
            client.startClient(clientSocket, dataFromClient, clientsList)
        Next
        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
    End Sub
    Protected Overrides Sub OnStop()
    End Sub
    Sub msg(ByVal mesg As String)
        EventLog.WriteEntry(mesg)
        '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 = System.Text.Encoding.ASCII.GetBytes
(uName + " says : " + msg)
            Else
                broadcastBytes = System.Text.Encoding.ASCII.GetBytes(msg)
            End If
            broadcastStream.Write(broadcastBytes, 0, 
broadcastBytes.Length)
            broadcastStream.Flush()
        Next
    End Sub
    Public Class handleClient
        Inherits Service5
        Dim clientSocket As TcpClient
        Dim clNo As String
        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 Class
Sono 4 giorni che ci giro attorno e ne sto uscendo pazzoooooooooooooooo!!
Alverman

Quando lancio il servizio dai servizi la barretta verde nel tentavio di 
avviare il servizio arriva alla fine e da un'errore:
1053 Il servizio non ha risposto alla richiesta di avvio o controllo nei 
tempi previsti.
Ma il server funziona ugualmente!!
Nel servizio pero non posso fermarlo o riavviarlo.
Mi date una mano almeno su questo per favore ?
Dai che ci siamo :)
Grazie, Alverman

 ha scritto nel messaggio 
news:201001211...@mynewsgate.net...

Guarda nel registro eventi (Application e System), potrebbe esserci 
un'eccezione non gestita da parte del tuo servizio...
Ciao
Raf 

 
Sembra tutto apposto !!
In system:
Un servizio ï¿œ stato installato nel sistema.
Nome servizio:  Service5
Nome file servizio:  "C:\NewLife\ServerService5\ServerService5
\bin\Release\serverservice5.exe"
Tipo servizio:  servizio in modalitï¿œ utente
Tipo avvio servizio:  Avvio su richiesta
Account servizio:  LocalSystem
In application:
Chat Server Started ....
Che glielo faccio scrivere io dal servizio

 ha scritto nel messaggio 
news:201001211...@mynewsgate.net...

Metti un gestore degli errori con logging nell'OnStart: se il servizio non 
riesce a partire, probabilmente ï¿œ perchï¿œ va in errore lï¿œ.
E poi compila in modalitï¿œ Debug, non Release.
Ciao
Raf 

 riesce a partire, probabilmente ï¿œ perchï¿œ va in errore lï¿œ.
ho messo tutto sotto try 
e visualizzo l'eventuale errore con 
        Catch ex As Exception
            EventLog.WriteEntry(ex.Message)
        End Try
puï¿œ bastare per gestire gli errori ?
Ma anche facendo cosï¿œ non mi visualizza nessun errore

Su on start ho questo codice:
       Try
            Dim infiniteCounter As Integer
            Dim counter As Integer
            serverSocket.Start()
            msg("Chat Server Started ....")
            counter = 0
            infiniteCounter = 0
            For infiniteCounter = 1 To 2
                msg("Sono all'interno del loop")
                infiniteCounter = 1
                counter += 1
                clientSocket = serverSocket.AcceptTcpClient()
                Dim bytesFrom(10024) As Byte
                Dim dataFromClient As String
                msg("Sono a metï¿œ del loop")
                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)
                msg(dataFromClient + " Joined chat room ")
                Dim client As New handleClient
                client.startClient(clientSocket, dataFromClient, clientsList)
                msg("Sono alla fine del loop")
            Next
            msg("Sono fuori dal loop")
        Catch ex As Exception
            EventLog.WriteEntry(ex.Message)
        End Try
        clientSocket.Close()
        serverSocket.Stop()
    End Sub
Ho messo dei messaggi per vedere dove si trova
Appena lanciato il servizio vedo: Sono all'interno del loop
mentre gli altri messaggi li vedo solo dopo che mi connetto al servizio.
Probabilmente si blocca appena dentro il loop ma non so come fare a risolvere 
sta cosa !!
Alverman

     End Sub
Per me deve stare in un finally, e comunque come fai a fare una close o
una stop senza testare lo stato dei due oggetti?
E se va in errore su serverSocket.Start(), allora serverSocket.Stop() ti
manda in crash il servizio.
Per quanto riguarda il tuo caso, sei sicuro che il servizio esca
dall'OnStart?
Perchï¿œ se non esce dall'OnStart viene fuori proprio l'errore che segnalavi.
Ciao,
Nicola

 Nicola
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

 E' quello il problema ..... ma come lo risolvo ?
Multithreading.
Apri un nuovo thread e metti in ascolto quello.
Nicola

 
Non ce la faccio :(
Un'altro aiuto per favore o impazzisco 
Alverman


Per caso hai preso spunto da questo: 
http://vb.net-informations.com/communications/vb.net_chat_server_program.htm
 
?
Ciao, Vincenzo

 Ciao, Vincenzo
Si :)

Ho fatto cosï¿œ ma non va perchï¿œ non riesco a risolvere l'errore  
Reference to a non-shared member requires an object reference nella 
class HandleClinet su broadcast e msg:
Se faccio inherits Service1 in public Class Handleclinet riesco a 
compilare.
Se installo il servizio si installa correttamente.
Il server accetta multiutenze ma se invio del testo al server da un 
qualsiasi client non mi ritorna niente !!
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text
Public Class Service1
    Dim trd = New Thread(AddressOf Start)
    Dim clientsList As New Hashtable
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set 
things
        ' in motion so your service can do its work.
        trd.start()
    End Sub
    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your 
service.
        trd.stop()
    End Sub
    Sub Start()
        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)
            msg(dataFromClient + " Joined chat room ")
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
        Next
    End Sub
    Sub msg(ByVal mesg As String)
        mesg.Trim()
        EventLog.WriteEntry(" >> " + 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
                    msg(ex.ToString)
                End Try
            Next
        End Sub
    End Class
End Class
Grazie per l'aiuto.
Alverman

 ha scritto nel messaggio 
news:201001251...@mynewsgate.net...

Se trovo 5 minuti provo a farlo io il servizio ;-)
Ciao, Vincenzo 

 Un'altro aiuto per favore o impazzisco 
*** Nella classe principale:
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Inserire qui il codice necessario per avviare il proprio
servizio. Il metodo deve effettuare
        ' le impostazioni necessarie per il funzionamento del servizio.
        StartListening()
    End Sub
    Protected Overrides Sub OnStop()
        ' Inserire qui il codice delle procedure di chiusura necessarie
per arrestare il proprio servizio.
    End Sub
    Sub StartListening()
        'SDIServer.StartListening(Me.ServiceName)
        Dim Td As New Thread(AddressOf Listener.StartListening)
        Td.Start(Me.ServiceName)
    End Sub
**** Nella classe Listener
Public Class Listener
 Public Shared Sub StartListening(ByVal ServiceName As Object)
        'Imposto la variabile NomeServizio con il nome del servizio attuale
        NomeServizio = ServiceName.ToString
        Try
	'Qui fai ciï¿œ che ti pare. E' il punto di partenza di tutte le
funzionalitï¿œ del tuo servizio.
        Catch ex As Exception
            EventLog.WriteEntry(NomeServizio, "Error: " + ex.ToString(),
System.Diagnostics.EventLogEntryType.[Error])
            Console.WriteLine("Exception: {0}", ex)
        End Try
    End Sub 'Main
End Class

 
In 5 minuti !?
Magari :)