定义结构体:
struct SA
{
int nFileSize; //文件大小
void init()
{
nFileSize =0;
}
}mysa;通过字节流发送图片文件,文件大小为: 44095k
发送过程如下:
long lFileSize = file.GetLength();mysa.init();
mysa.nFileSize = lFileSize;
send(m_hSocket,(char*)&mysa,sizeof(mysa),0);服务器段接收:
char temp[1024];
for(int i=0; i < 1024; i++)
{
strmsg.Format("%d ", temp[i]);
str1 += strmsg;
}
AfxMessageBox("收到数据:" + str1);显示如下:
-52 -52 63 -84
问题:
文件大小为:44095k
1、通过-52 -52 63 -84 子节流如何转换得出44095k的结果;
2、int类型为四个字节,能否在发送前让所有字节清0,现在前两个字节都是-52,表示为没数据,但需要事先都设置为0
struct SA
{
int nFileSize; //文件大小
void init()
{
nFileSize =0;
}
}mysa;通过字节流发送图片文件,文件大小为: 44095k
发送过程如下:
long lFileSize = file.GetLength();mysa.init();
mysa.nFileSize = lFileSize;
send(m_hSocket,(char*)&mysa,sizeof(mysa),0);服务器段接收:
char temp[1024];
for(int i=0; i < 1024; i++)
{
strmsg.Format("%d ", temp[i]);
str1 += strmsg;
}
AfxMessageBox("收到数据:" + str1);显示如下:
-52 -52 63 -84
问题:
文件大小为:44095k
1、通过-52 -52 63 -84 子节流如何转换得出44095k的结果;
2、int类型为四个字节,能否在发送前让所有字节清0,现在前两个字节都是-52,表示为没数据,但需要事先都设置为0
memset(&size,0,sizeof(size));
recv(recvSock,(char*)&size,sizeof(size),0);
如果非要把四个char转换成一个int,这没必要吧,何必自找麻烦
MSDN,多看看哦
int fileSize = *( (int*)temp );
char temp[1024];
// 数据接受
...
struct SA *pSa = (struct SA*)temp;
printf("文件大小为:\r\n",pSa->nFileSize);