首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >x86和x86-64之间的套接字和可移植性

x86和x86-64之间的套接字和可移植性
EN

Stack Overflow用户
提问于 2012-09-03 14:02:16
回答 1查看 116关注 0票数 3

让我们以下面的服务器代码为例来说明我的疑问:

代码语言:javascript
复制
/* some code */
void *filebuffer = NULL;
/* some other code */

for (size_to_send = fsize; size_to_send > 0; ){
  rc = sendfile(f_sockd, fd, &offset, size_to_send);
  if (rc <= 0){
    perror("sendfile");
    onexit(f_sockd, m_sockd, fd, 3);
  }
  size_to_send -= rc;
}

/*其他代码*/

和下面的客户端代码:

代码语言:javascript
复制
/* some code */
void *filebuffer;
/*some other code */
for(size_to_receive = fsize; size_to_receive > 0;){
  nread = read(f_sockd, filebuffer, size_to_receive);
  if(nread < 0){
    perror("read error on retr");
    onexit(f_sockd, 0, 0, 1);
  }
  if(write(fd, filebuffer, nread) != nread){
    perror("write error on retr");
    onexit(f_sockd, 0, 0, 1);
  }
  size_to_receive -= nread;
}
/* other code */

我的问题是:如果服务器在x86机器上(小端),而客户端在x64机器上(小端),指针大小不同(4-8字节)会导致问题吗?

如果是,我如何解决?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-03 14:07:48

不,这不是问题,因为您实际上不会通过套接字发送指针,而只是一个字节流。我看到的唯一问题是,如果文件是二进制数据并且其中包含64位整数,而接收平台是32位而不支持64位整数(例如long long),那么这是非常不可能的,除非您是在嵌入式系统上接收。

PS。在接收循环中,您将检查读取错误,但不会检查要由另一端关闭的套接字(当read返回0时)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12242739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档