none
如何使用ucicode於C#語言裡 RRS feed

  • 問題

  •  

    這是server端與client兩者互相傳輸資料的範例。但是我無法輸入特殊符號及中文,最後查到需要使用unicode萬國碼,請問我該修改哪裡才能讓它使用unicode??

     

    SERVER端程式範例:

    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne5b
    {
     static void Main()
     {
                TcpListener tcp = new TcpListener(7200);
         tcp.Start() ; 
         Console.WriteLine("SERVER 通訊埠 7200 啟動 ");
         Socket soc  = tcp.AcceptSocket(); 
                thread tp = new thread(soc);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
                string message;    
         do{                            
                          Console.Write("輸入傳往 CLIENT 端的資料內容 :" );
            message=Console.ReadLine();
                          byte[] buff2=Encoding.ASCII.GetBytes(message);
            soc.Send(buff2,buff2.Length,0) ;   
           }while(true );
     }

    }


    class thread

     Socket client;
     public thread(Socket s)
     {  
      client = s;
            }
     
     public void service()
     {
                   
                    int length;                              
                    byte[] buff = new byte[1000] ;              
      while (true)
      {   
           length = client.Receive(buff,1000,0);   
                         Console.Write("\n顯示從 CLIENT 端傳來的資料內容 : " ) ;                    
           Console.WriteLine(Encoding.ASCII.GetString(buff,0,length));    
      }               
      
     }
    }

     

    CLIENT端程式範例:

     

    using System;
    using System.Text;
    using System.IO ;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne4b
    {
        
         
           static void Main()
     {
                NetworkStream netstream;  
                TcpClient tcp = new TcpClient();
         Console.Write("輸入主機名稱 : " );
         string name = Console.ReadLine();
         Console.Write("輸入連接通訊埠 : " );
         int port = int.Parse(Console.ReadLine());     
       
         try{
         tcp.Connect(name , port);
         Console.WriteLine("連線成功");
            }
         catch
            {
          Console.WriteLine("主機 " + name +  " 通訊埠 " + port + " 無法上線");
          return ;
            } 
                netstream = tcp.GetStream();
                thread tp = new thread( netstream,tcp);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
      
                do{ 
                       Console.Write("輸入傳往 SERVER 端的資料 : ");
                String str= Console.ReadLine();   
                Byte[] buff =Encoding.ASCII.GetBytes(str);                 
                netstream.Write(buff, 0, buff.Length);                  
                  }while(true);
       
     } 
          
    }


    class thread

     NetworkStream client;
            TcpClient tcp2;
     public thread(NetworkStream n,TcpClient t)
     {  
                    client = n;  
                    tcp2=t;
            }
     
     public void service()
     {            
                               
      while (true)
      {
                  int length =tcp2.ReceiveBufferSize ;    
                  byte[] buff2 = new byte[length];  
                         client.Read(buff2, 0,length);   
                         string str=Encoding.ASCII.GetString(buff2,0,length);
                         length=str_length(str);
                         if(length==0)
                            return;             
                         str=str.Substring(0,length);   
                         Console.Write("\n顯示 SERVER 端傳來的資料 : ");                   
                         Console.WriteLine(str);         
      }               
      
     }

          
             public int str_length(string args)
            {    
                 int j=0;
                 char[] bb=args.ToCharArray();
                 try{
                       while(bb[j]!='\0')
                       j++;                    
                    }
                 catch(System.IndexOutOfRangeException)
                    {}     
                 return(j); 
            }


    }


     

     

    2008年6月18日 上午 05:53

解答

  •  TigerHsu 寫信:

     

    這是server端與client兩者互相傳輸資料的範例。但是我無法輸入特殊符號及中文,最後查到需要使用unicode萬國碼,請問我該修改哪裡才能讓它使用unicode??

     

    SERVER端程式範例:

    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne5b
    {
     static void Main()
     {
                TcpListener tcp = new TcpListener(7200);
         tcp.Start() ; 
         Console.WriteLine("SERVER 通訊埠 7200 啟動 ");
         Socket soc  = tcp.AcceptSocket(); 
                thread tp = new thread(soc);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
                string message;    
         do{                            
                          Console.Write("輸入傳往 CLIENT 端的資料內容 :" );
            message=Console.ReadLine();
                          byte[] buff2=Encoding.UTF8.GetBytes(message);
            soc.Send(buff2,buff2.Length,0) ;   
           }while(true );
     }

    }


    class thread

     Socket client;
     public thread(Socket s)
     {  
      client = s;
            }
     
     public void service()
     {
                   
                    int length;                              
                    byte[] buff = new byte[1000] ;              
      while (true)
      {   
           length = client.Receive(buff,1000,0);   
                         Console.Write("\n顯示從 CLIENT 端傳來的資料內容 : " ) ;                    
           Console.WriteLine(Encoding.UTF8.GetString(buff,0,length));    
      }               
      
     }
    }

     

    CLIENT端程式範例:

     

    using System;
    using System.Text;
    using System.IO ;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne4b
    {
        
         
           static void Main()
     {
                NetworkStream netstream;  
                TcpClient tcp = new TcpClient();
         Console.Write("輸入主機名稱 : " );
         string name = Console.ReadLine();
         Console.Write("輸入連接通訊埠 : " );
         int port = int.Parse(Console.ReadLine());     
       
         try{
         tcp.Connect(name , port);
         Console.WriteLine("連線成功");
            }
         catch
            {
          Console.WriteLine("主機 " + name +  " 通訊埠 " + port + " 無法上線");
          return ;
            } 
                netstream = tcp.GetStream();
                thread tp = new thread( netstream,tcp);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
      
                do{ 
                       Console.Write("輸入傳往 SERVER 端的資料 : ");
                String str= Console.ReadLine();   
                Byte[] buff =Encoding.UTF8.GetBytes(str);                 
                netstream.Write(buff, 0, buff.Length);                  
                  }while(true);
       
     } 
          
    }


    class thread

     NetworkStream client;
            TcpClient tcp2;
     public thread(NetworkStream n,TcpClient t)
     {  
                    client = n;  
                    tcp2=t;
            }
     
     public void service()
     {            
                               
      while (true)
      {
                  int length =tcp2.ReceiveBufferSize ;    
                  byte[] buff2 = new byte[length];  
                         client.Read(buff2, 0,length);   
                         string str=Encoding.UTF8.GetString(buff2,0,length);
                         length=str_length(str);
                         if(length==0)
                            return;             
                         str=str.Substring(0,length);   
                         Console.Write("\n顯示 SERVER 端傳來的資料 : ");                   
                         Console.WriteLine(str);         
      }               
      
     }

          
             public int str_length(string args)
            {    
                 int j=0;
                 char[] bb=args.ToCharArray();
                 try{
                       while(bb[j]!='\0')
                       j++;                    
                    }
                 catch(System.IndexOutOfRangeException)
                    {}     
                 return(j); 
            }


    }


     

     

    2008年6月18日 上午 06:14
    版主

所有回覆

  •  TigerHsu 寫信:

     

    這是server端與client兩者互相傳輸資料的範例。但是我無法輸入特殊符號及中文,最後查到需要使用unicode萬國碼,請問我該修改哪裡才能讓它使用unicode??

     

    SERVER端程式範例:

    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne5b
    {
     static void Main()
     {
                TcpListener tcp = new TcpListener(7200);
         tcp.Start() ; 
         Console.WriteLine("SERVER 通訊埠 7200 啟動 ");
         Socket soc  = tcp.AcceptSocket(); 
                thread tp = new thread(soc);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
                string message;    
         do{                            
                          Console.Write("輸入傳往 CLIENT 端的資料內容 :" );
            message=Console.ReadLine();
                          byte[] buff2=Encoding.UTF8.GetBytes(message);
            soc.Send(buff2,buff2.Length,0) ;   
           }while(true );
     }

    }


    class thread

     Socket client;
     public thread(Socket s)
     {  
      client = s;
            }
     
     public void service()
     {
                   
                    int length;                              
                    byte[] buff = new byte[1000] ;              
      while (true)
      {   
           length = client.Receive(buff,1000,0);   
                         Console.Write("\n顯示從 CLIENT 端傳來的資料內容 : " ) ;                    
           Console.WriteLine(Encoding.UTF8.GetString(buff,0,length));    
      }               
      
     }
    }

     

    CLIENT端程式範例:

     

    using System;
    using System.Text;
    using System.IO ;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    class ne4b
    {
        
         
           static void Main()
     {
                NetworkStream netstream;  
                TcpClient tcp = new TcpClient();
         Console.Write("輸入主機名稱 : " );
         string name = Console.ReadLine();
         Console.Write("輸入連接通訊埠 : " );
         int port = int.Parse(Console.ReadLine());     
       
         try{
         tcp.Connect(name , port);
         Console.WriteLine("連線成功");
            }
         catch
            {
          Console.WriteLine("主機 " + name +  " 通訊埠 " + port + " 無法上線");
          return ;
            } 
                netstream = tcp.GetStream();
                thread tp = new thread( netstream,tcp);
         Thread st =new Thread(new ThreadStart(tp.service));   
         st.Start();
      
                do{ 
                       Console.Write("輸入傳往 SERVER 端的資料 : ");
                String str= Console.ReadLine();   
                Byte[] buff =Encoding.UTF8.GetBytes(str);                 
                netstream.Write(buff, 0, buff.Length);                  
                  }while(true);
       
     } 
          
    }


    class thread

     NetworkStream client;
            TcpClient tcp2;
     public thread(NetworkStream n,TcpClient t)
     {  
                    client = n;  
                    tcp2=t;
            }
     
     public void service()
     {            
                               
      while (true)
      {
                  int length =tcp2.ReceiveBufferSize ;    
                  byte[] buff2 = new byte[length];  
                         client.Read(buff2, 0,length);   
                         string str=Encoding.UTF8.GetString(buff2,0,length);
                         length=str_length(str);
                         if(length==0)
                            return;             
                         str=str.Substring(0,length);   
                         Console.Write("\n顯示 SERVER 端傳來的資料 : ");                   
                         Console.WriteLine(str);         
      }               
      
     }

          
             public int str_length(string args)
            {    
                 int j=0;
                 char[] bb=args.ToCharArray();
                 try{
                       while(bb[j]!='\0')
                       j++;                    
                    }
                 catch(System.IndexOutOfRangeException)
                    {}     
                 return(j); 
            }


    }


     

     

    2008年6月18日 上午 06:14
    版主
  •  

    感謝您,我個人試過Unicode也可以。

     

    2008年6月18日 上午 06:57