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收发,而不直接使用结构体收发)!多谢高手指点一,二!!!!!
{
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收发,而不直接使用结构体收发)!多谢高手指点一,二!!!!!
解决方案 »
- 如何将wstring字符串写入txt文件
- 我用CreateProcess运行了一个批处理文件,但是我不想让它在后台运行或者不显示那个窗口,应该怎么做?
- 求悬浮框(像Flashget那种)代码?
- 各位高手请帮帮忙
- 请各位高手帮帮忙,我真的很着急啊!
- 怎样在没有安装vc 6.0的机子运行我的mfc程序?
- 请问高级格式化和低级格式化的实现原理?
- ★★★★★高手请进→→→高分求答●●●●●
- 请问哪儿可以找到标准c的库函数手册和MFC类库函数手册
- 请教不同平台的DLL的处理问题,高手看过来!!只有这些分
- 如何在继承自CView的View类中加入SCROLL以及如何设置字体颜色,谢谢。
- 如何修改属性页的“确定”的响应
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
.
急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急!
// 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;
}
nRet 每次返回都为0
所以问题仍未解决!不过还是谢谢楼上的兄弟!