接收端
byte[] jieshoule = new byte[1024];
fasong.Receive(jieshoule);
string youxigengxin = Encoding.Default.GetString(jieshoule);
发送端
fixbyte = Encoding.Default.GetBytes("发送完毕");
jieshou.Send(fixbyte);   我接收端不知道发送端发的数据是多大所以我定义成1024了!可问题就在这!我发送"发送完毕"这四个字,我一查看长度也是1024的!可能是我定义的长度有问题吧!请问你们遇到这样的问题怎么解决?小弟在线等.送50分

解决方案 »

  1.   

    change 
    fasong.Receive(jieshoule);
    string youxigengxin = Encoding.Default.GetString(jieshoule);with
    int nRealRead = fasong.Receive(jieshoule);
    string youxigengxin = Encoding.Default.GetString(jieshoule, 0, nRealRead );
      

  2.   

    Socket handler = listener.Accept();
    data = null;
    // An incoming connection needs to be processed.
    while (true) 
    {
         bytes = new byte[1024];
         int bytesRec = handler.Receive(bytes);
         data += Encoding.UTF8.GetString(bytes,0,bytesRec);
    if (data.IndexOf("<EOF>") > -1) 
    {
    break;
    }
    }发送的字符串后面加<EOF>记录这个字符串的结尾位置