客户端:  private  void Send(Socket client, String data)  
        {  
            // 格式转换. 
            //byte[] byteData = Encoding.UTF8.GetBytes(data);   
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
            // 开始发送数据到winform. 
            client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);  
        }  服务器端:
            //当有客户端连接时的处理 
                public void OnConnectRequest(IAsyncResult ar) 
                {
                    string content = null;// 短信内容
                    string destAddr = null;//目标号码                     //初始化一个SOCKET,用于其它客户端的连接 
                        Socket server1 = (Socket)ar.AsyncState; 
                        Client = server1.EndAccept(ar); 
                        //将要发送给连接上来的客户端的提示字符串 
                        string strDateLine = "Welcome here";
                        Byte[] byteDateLine = System.Text.Encoding.UTF8.GetBytes(strDateLine); 
                        //将提示信息发送给客户端 
                        Client.Send(byteDateLine, byteDateLine.Length, 0); 
                        //等待新的客户端连接 
                        server1.BeginAccept(new AsyncCallback(OnConnectRequest), server1); 
                        while (true) 
                        { 
                               int recv = Client.Receive(byteDateLine);
                              // string stringdata = Encoding.UTF8.GetString(byteDateLine, 0, recv); 
                               string stringdata = UTF8Encoding.UTF8.GetString(byteDateLine, 0, recv);  
                               DateTimeOffset now = DateTimeOffset.Now; 
                                //获取客户端的IP和端口 
                                string ip = Client.RemoteEndPoint.ToString(); 
                                if (stringdata == "STOP") 
                                { 
                                        //当客户端终止连接时 
                                      MessageBox.Show(ip+"已从服务器断开");
                                    break;    
                                } 
                                //显示客户端发送过来的信息 
                                destAddr = stringdata.Substring(0,11);//把socket中得到的stringdata截取前11位 组成目标号码。
                                content = stringdata.Substring(11);//把socket中得到的stringdata截取从12位 开始组成要发送的短信内容。
                                int a = stringdata.Length;
                                string len = a.ToString();
                                MessageBox.Show(len);
                                MessageBox.Show(destAddr);
                                MessageBox.Show(content);
                                MessageBox.Show(ip + "    " + now.ToString("G") + "     " + stringdata + "\r\n");
                                try
                                {
                                    if (destAddr != null && content != null)
                                    {
                                        SendMessage(destAddr, content);// 将得到目标号码和短信内容发送出去
                                    }
                                }
                                catch (Exception ex){
                                    MessageBox.Show(ex.Message);
                                
                                }
                        } 
                                                
                } 
当输入12312312测试时 在服务器端显示“12312312口 ” 我在client端和server端 都是UTF8格式的编码 

解决方案 »

  1.   

    貌似都是转成Byte发送的吧?
    而且也不是这么转的啊.....
    Byte [] PostBytes = Encoding.ASCII.GetBytes (PostData);
      

  2.   

      string stringdata = UTF8Encoding.UTF8.GetString(byteDateLine, 0, recv);  
    这个也要改
      

  3.   

    ...
      怎么改?
      我以前在client端  byte[] byteData = Encoding.ASCII.GetBytes(data);
      在service端  string stringdata = Encoding.ASCII.GetString(byteDateLine, 0, recv);  
      

  4.   

    Encoding.UTF8.GetString(byteArray)
    试试看,我用这个没乱码
      

  5.   


    byteArray? 是怎么定义的?
      

  6.   

    解决了  这样写 就可以了  Encoding.Unicode.GetBytes(data)
      

  7.   

    不至于吧, UTF8 已经包括了中文了啊。
      

  8.   

    饿... 但事实是Encoding.Unicode.GetBytes(data)就能正确显示中文了
      

  9.   

    又有个问题了 由于我不是很明白socket 在写程序是没有给字符串设定缓冲区大小 现在要怎么修改?