typedef struct test
{
   char a[10];
   char b[20];
}sValue;发送端:
  sValue c;
  char buff[1024];
  strncpy(c.a,"China",sizeof(c.a));
  strncpy(c.b,"Beijing",sizeof(c.b));
  memcpy(buff,(char*)&c,sizeof(c));
  send(buff,1024)接收端:
  receive(buff);结果:
   由于给结构体c赋值的时候,“China”不够10个长度,导致buff中数据区有空
字符,在接受时只收到“China”,而没有收到“Beijing”,请问如何解决这个问题,
(最好用buff收发,而不直接使用结构体收发)!多谢高手指点一,二!!!!!

解决方案 »

  1.   

    串行化!send->receive 反串行化
      

  2.   

    发送端:
      sValue c = {0};     // init
      char buff[1024]={0};
      strncpy(c.a,"China",strlen("China"));   
      strncpy(c.b,"Beijing",strlen("Beijing"));
      memcpy(buff,(char*)&c,sizeof(c));
      send(buff,1024);接收端:
      char buff[1024] ={0};
      sValue *C = 0;
      receive(buff);
      C = (sValue*)buff;  printf( "a=%s" , C->a);
      printf( "b=%s" , C->b);
    try this , but i don't test this code ,youself do 
    .
      

  3.   

    tigerfox(风之力:=Doing.浪淘沙) 兄弟的方法我试过了,可结果还是一样,请再帮忙想想帮法!
    急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急!
      

  4.   

    this code for you:// send date to client
    // pararm : ...
    // return : ...
    int sendn(SOCKET socket,TCHAR *pBuf,int nLen)
    {
    int nLastCount,nRet;
    nLastCount = nLen;
    while(nLastCount>0)
    {
    //send  The send function sends data on a connected socket.
        //
    nRet=send(socket,pBuf,nLastCount,0);
    if( nRet==SOCKET_ERROR )
    {
    TRACE("\tSend Data Error!\n");
    return -1;
    }
    if(nRet==0)
    return nLen-nLastCount;

    pBuf += nRet;
    nLastCount -= nRet ;
    }
    return nLen;
    }// retrive date form client
    // param : ...
    // return : ...
    int readn(SOCKET socket,TCHAR *buf,UINT nLen)
    {
    int nLastCount,nRet;
    nLastCount =nLen; while( nLastCount>0 )
    {
    nRet=recv(socket,buf,nLastCount,0);

    if(nRet==SOCKET_ERROR)
    {

    TRACE("\tretrieve date Error \n");
    return -1;
    }

    //If the connection has been gracefully closed, the return value is zero.
    if(nRet==0) 
    return nLen-nLastCount;

    buf += nRet;

    nLastCount -= nRet;
    } return nLen;
    }
      

  5.   

    nRet=send(socket,pBuf,nLastCount,0);
    nRet 每次返回都为0
    所以问题仍未解决!不过还是谢谢楼上的兄弟!