1. Este site usa cookies. Ao continuar a usar este site está a concordar com o nosso uso de cookies. Saber Mais.

Variavel com espaços a mais

Discussão em 'Programação' iniciada por wrproject, 15 de Fevereiro de 2009. (Respostas: 0; Visualizações: 379)

  1. wrproject

    wrproject Power Member

    Boas pessoal eu tenho uma aplicação cçiente-servidor.
    O cliente quando conecta envia a string "wrproject" para o servidor
    O servidor recebe a string e mostra no ecrã.
    O problema é que alem do servidor mostrar a a string "wroject" colocas espaçosa mais.
    Ja tentei usar o trim() e não funcionou.

    Embaixo o codigo do cliente e servidor.

    CLIENTE:

    Código:
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Collections.Generic[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Linq[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Text[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Net[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Net.Sockets[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Collections[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]namespace[/COLOR] ClienteTeste
    [COLOR=#000000]{[/COLOR]
        [COLOR=#FF0000]class[/COLOR] Program
        [COLOR=#000000]{[/COLOR]
            [COLOR=#0600FF]static[/COLOR] [COLOR=#0600FF]void[/COLOR] Main[COLOR=#000000]([/COLOR][COLOR=#FF0000]string[/COLOR][COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR] args[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                IPAddress ip[COLOR=#008000]=[/COLOR]IPAddress.[COLOR=#0000FF]Parse[/COLOR][COLOR=#000000]([/COLOR][COLOR=#666666]"127.0.0.1"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                TcpClient cl [COLOR=#008000]=[/COLOR] [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] TcpClient[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
              
                
                [COLOR=#0600FF]try[/COLOR]
                [COLOR=#000000]{[/COLOR]
                    cl.[COLOR=#0000FF]Connect[/COLOR][COLOR=#000000]([/COLOR]ip, [COLOR=#FF0000]500[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                    Console.[COLOR=#0000FF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#666666]"Conectado"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                    [COLOR=#FF0000]string[/COLOR] s[COLOR=#008000]=[/COLOR][COLOR=#666666]"wrproject"[/COLOR][COLOR=#008000];[/COLOR]
                    [COLOR=#FF0000]byte[/COLOR][COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR] data[COLOR=#008000];[/COLOR]
                    data [COLOR=#008000]=[/COLOR] [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] [COLOR=#000000]System.[COLOR=#0000FF]Text[/COLOR][/COLOR].[COLOR=#0000FF]ASCIIEncoding[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]GetBytes[/COLOR][COLOR=#000000]([/COLOR]s[COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                    Console.[COLOR=#0000FF]WriteLine[/COLOR][COLOR=#000000]([/COLOR]data.[COLOR=#0000FF]Length[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                    cl.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]Write[/COLOR][COLOR=#000000]([/COLOR]data,[COLOR=#FF0000]0[/COLOR],data.[COLOR=#0000FF]Length[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                    
                [COLOR=#000000]}[/COLOR]
                [COLOR=#0600FF]catch[/COLOR] [COLOR=#000000]([/COLOR]Exception[COLOR=#000000])[/COLOR]
                [COLOR=#000000]{[/COLOR]
                    Console.[COLOR=#0000FF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#666666]"Erro ao conectar"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                [COLOR=#000000]}[/COLOR]
                 
                Console.[COLOR=#0000FF]ReadKey[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
            [COLOR=#000000]}[/COLOR]
        [COLOR=#000000]}[/COLOR]
    [COLOR=#000000]}[/COLOR]
    

    SERVIDOR:
    Código:
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Collections.Generic[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Linq[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Text[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Net[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Net.Sockets[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Collections[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]using[/COLOR] [COLOR=#008080]System.Threading[/COLOR][COLOR=#008000];[/COLOR]
    [COLOR=#0600FF]namespace[/COLOR] ICPServer
    [COLOR=#000000]{[/COLOR]
        [COLOR=#FF0000]class[/COLOR] ChatClient
        [COLOR=#000000]{[/COLOR]
    
            [COLOR=#008080][I]//cliente[/I][/COLOR]
            TcpClient _client[COLOR=#008000];[/COLOR]
    
            [COLOR=#008080][I]//nick[/I][/COLOR]
            [COLOR=#FF0000]String[/COLOR] nick[COLOR=#008000];[/COLOR]
    
            [COLOR=#008080][I]//lista de utilizadores[/I][/COLOR]
            [COLOR=#0600FF]public[/COLOR] [COLOR=#0600FF]static[/COLOR] Hashtable AllClients [COLOR=#008000]=[/COLOR] [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] Hashtable[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
    
    
            [COLOR=#008080][I]//variavel que servirá para o utilizador enviar ou receber dados[/I][/COLOR]
            [COLOR=#FF0000]byte[/COLOR][COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR] data[COLOR=#008000];[/COLOR]
            
            [COLOR=#008080][I]//Recber nick[/I][/COLOR]
            [COLOR=#FF0000]bool[/COLOR] ReceiveNick [COLOR=#008000]=[/COLOR] true[COLOR=#008000];[/COLOR]
    
            [COLOR=#008080][I]//o ip do cliente[/I][/COLOR]
            [COLOR=#0600FF]public[/COLOR] [COLOR=#FF0000]string[/COLOR] _clientIP[COLOR=#008000];[/COLOR]
    
            [COLOR=#0600FF]public[/COLOR] ChatClient[COLOR=#000000]([/COLOR]TcpClient client[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                _client [COLOR=#008000]=[/COLOR] client[COLOR=#008000];[/COLOR]
    
                [COLOR=#008080][I]//get The IP[/I][/COLOR]
                _clientIP [COLOR=#008000]=[/COLOR] _client.[COLOR=#0000FF]Client[/COLOR].[COLOR=#0000FF]RemoteEndPoint[/COLOR].[COLOR=#0000FF]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
    
                [COLOR=#008080][I]//Add this ip to the hashtable[/I][/COLOR]
                AllClients.[COLOR=#0000FF]Add[/COLOR][COLOR=#000000]([/COLOR]_clientIP, [COLOR=#0600FF]this[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
    
                [COLOR=#008080][I]//data = new byte[_client.ReceiveBufferSize];[/I][/COLOR]
                _client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                data [COLOR=#008000]=[/COLOR] [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] [COLOR=#FF0000]byte[/COLOR][COLOR=#000000][[/COLOR]_client.[COLOR=#0000FF]ReceiveBufferSize[/COLOR][COLOR=#000000]][/COLOR][COLOR=#008000];[/COLOR]
                [COLOR=#008080][I]//começa uma leitura assincrona[/I][/COLOR]
                _client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]BeginRead[/COLOR][COLOR=#000000]([/COLOR]data, [COLOR=#FF0000]0[/COLOR], data.[COLOR=#0000FF]Length[/COLOR], ReceiveMessage, [COLOR=#0600FF]null[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
            [COLOR=#000000]}[/COLOR]
    
            
    
            [COLOR=#008080][I]//Método assincrono para receber clientes[/I][/COLOR]
    
            [COLOR=#0600FF]public[/COLOR] [COLOR=#0600FF]void[/COLOR] ReceiveMessage[COLOR=#000000]([/COLOR]IAsyncResult ar[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
    
                [COLOR=#FF0000]int[/COLOR] bytesRead [COLOR=#008000]=[/COLOR] [COLOR=#FF0000]0[/COLOR][COLOR=#008000];[/COLOR]
                [COLOR=#FF0000]char[/COLOR][COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR] charsToTrim [COLOR=#008000]=[/COLOR] [COLOR=#000000]{[/COLOR][COLOR=#666666]' '[/COLOR][COLOR=#000000]}[/COLOR][COLOR=#008000];[/COLOR]
    
                [COLOR=#0600FF]try[/COLOR]
                [COLOR=#000000]{[/COLOR]
    
                    [COLOR=#0600FF]lock[/COLOR] [COLOR=#000000]([/COLOR]_client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR]
                    [COLOR=#000000]{[/COLOR]
                        bytesRead [COLOR=#008000]=[/COLOR] _client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]EndRead[/COLOR][COLOR=#000000]([/COLOR]ar[COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                        [COLOR=#008080][I]//_client.GetStream().Flush();[/I][/COLOR]
    
                    [COLOR=#000000]}[/COLOR]
    
    
                    [COLOR=#008080][I]//se foram recebidos daos,o servidor irá tratar do processamento dos dados[/I][/COLOR]
                    [COLOR=#0600FF]if[/COLOR] [COLOR=#000000]([/COLOR]bytesRead [COLOR=#008000]>[/COLOR] [COLOR=#FF0000]0[/COLOR][COLOR=#000000])[/COLOR]
                    [COLOR=#000000]{[/COLOR]
                        
                        [COLOR=#0600FF]if[/COLOR][COLOR=#000000]([/COLOR]ReceiveNick[COLOR=#000000])[/COLOR]
                        [COLOR=#000000]{[/COLOR]
                            [COLOR=#FF0000]string[/COLOR] _tempnick [COLOR=#008000]=[/COLOR] [URL="http://www.google.com/search?q=new+msdn.microsoft.com"][COLOR=#008000]new[/COLOR][/URL] [COLOR=#000000]System.[COLOR=#0000FF]Text[/COLOR][/COLOR].[COLOR=#0000FF]ASCIIEncoding[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]GetString[/COLOR][COLOR=#000000]([/COLOR]data[COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                            [COLOR=#FF0000]string[/COLOR] s [COLOR=#008000]=[/COLOR] _tempnick.[COLOR=#0000FF]Trim[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                        Console.[COLOR=#0000FF]Write[/COLOR][COLOR=#000000]([/COLOR]s[COLOR=#008000]+[/COLOR][COLOR=#666666]" "[/COLOR][COLOR=#008000]+[/COLOR]s.[COLOR=#0000FF]Length[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                        ReceiveNick [COLOR=#008000]=[/COLOR] false[COLOR=#008000];[/COLOR]
                        [COLOR=#008080][I]//_client.GetStream().Flush();[/I][/COLOR]
                        [COLOR=#000000]}[/COLOR]
                      [COLOR=#000000]}[/COLOR]
    
    
                    [COLOR=#008080][I]//continue reading from client[/I][/COLOR]
                    [COLOR=#0600FF]lock[/COLOR] [COLOR=#000000]([/COLOR]_client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR]
                    [COLOR=#000000]{[/COLOR]
    
                        Int32 buffersize [COLOR=#008000]=[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000FF]Convert[/COLOR].[COLOR=#0000FF]ToInt32[/COLOR][COLOR=#000000]([/COLOR]_client.[COLOR=#0000FF]ReceiveBufferSize[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                        _client.[COLOR=#0000FF]GetStream[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR].[COLOR=#0000FF]BeginRead[/COLOR][COLOR=#000000]([/COLOR]data, [COLOR=#FF0000]0[/COLOR], buffersize, ReceiveMessage, [COLOR=#0600FF]null[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
    
    
                    [COLOR=#000000]}[/COLOR]
    
                [COLOR=#000000]}[/COLOR][COLOR=#008080][I]//fim try[/I][/COLOR]
                [COLOR=#0600FF]catch[/COLOR] [COLOR=#000000]([/COLOR]Exception[COLOR=#000000])[/COLOR]
                [COLOR=#000000]{[/COLOR]
                    Console.[COLOR=#0000FF]WriteLine[/COLOR][COLOR=#000000]([/COLOR][COLOR=#666666]"Cliente disconectou"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#008000];[/COLOR]
                [COLOR=#000000]}[/COLOR]
                [COLOR=#008080][I]//fim excepção[/I][/COLOR]
    
            [COLOR=#000000]}[/COLOR]
    
        [COLOR=#000000]}[/COLOR]
    [COLOR=#000000]}[/COLOR]
    
     

Partilhar esta Página