Ubuntu服务器(32位)和桌面(32位)在套接字编程方面有什么区别
我正在开发一个基于服务器/客户端的项目。我几乎完成了我的服务器端代码。
我在 Ubuntu 桌面上的 EclipseCDT 中开发服务器应用程序,一切正常。
但是,当将我的应用程序部署到Ubuntu服务器(我尝试了服务器10.04/10.10)时,服务器应用程序可以正常启动(等待连接),但同一客户端无法连接到服务器。
我使用 Socket 来接收和发送来自客户端的数据。
Peter
P.S.:如果我在我的服务器计算机上安装 sudo apt-get install ubuntu-desktop ,那么一切都会恢复正常。
=================================================== =========================
源代码的新发现:
LabelStartBlocking:
int newScoketId = ::accept(socketId, 0, 0); // socketId == 3 ::accept is define in socket.h
// waiting for connection
LabelResume: // if new connection coming
// Do something with newSocketId
Ubuntu 桌面版和服务器版之间的行为差异是:
在 Ubuntu 桌面版上,当服务器启动时,它被套接字例程 ::accept
阻塞在 LabelStartBlocking
处;然后,如果有新连接到达,服务器将在LabelResume
处恢复并使用返回值newSocketId
创建一个新的套接字连接;
然而,在 Ubuntu Server 版本上,当服务器启动时,它也会被套接字例程 ::accept
阻塞在 LabelStartBlocking
处,但如果有新连接到达,服务器将获胜无法在 LabelResume
处恢复,并且无法创建新的套接字连接。
你们能帮我一下吗?
彼得
I am working on a server/client based project. I almost finished my server side code.
I develop the server app in EclipseCDT on Ubuntu Desktop, and everything just works fine.
But when deploy my app to a Ubuntu Server (I tried Server 10.04/10.10), the server app can start normally (waiting for connection), but the same client just cannot connect to the server.
I use Socket for receiving and sending data to/from the client.
Peter
P.S.: if I install sudo apt-get install ubuntu-desktop
on my server machine, then everything works fine again.
===========================================================================
New Findings from the source code:
LabelStartBlocking:
int newScoketId = ::accept(socketId, 0, 0); // socketId == 3 ::accept is define in socket.h
// waiting for connection
LabelResume: // if new connection coming
// Do something with newSocketId
The behavior difference between Ubuntu Desktop and Server is:
On Ubuntu Desktop version, when the server starts, it is blocked at LabelStartBlocking
with the socket routine ::accept
; and then if a new connection arrives, the server will resume at LabelResume
and create a new socket connection using the return value newSocketId
;
However, on Ubuntu Server version, when the server starts, it is also blocked at LabelStartBlocking
with the socket routine ::accept
, but if a new connection arrives, the server won't resume at LabelResume
, and the new socket connection CANNOT be created.
Can you guys help me out?
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您的关注。
我终于想通了。
如果同一主机名 (
/etc/hosts
) 有多个 IP 地址,则旧代码将失败。示例
/etc/hosts
文件:我跟踪了调用堆栈,发现传递给程序的 IP 地址(10.50.10.251)被转换为主机名,然后主机名又被转换回来到 IP 地址(用于绑定),但是不同的地址,这就是为什么我的服务器程序无法接受任何客户端连接。
如果其他人有类似问题,希望它能有所帮助。
彼得
Thanks for your attention.
I finally figured it out.
If there are more than one IP addresses for the same hostname (
/etc/hosts
), the old code will fail.Example
/etc/hosts
file:I traced the calling stack, and I found that, the IP address (10.50.10.251) passed to the program is translated into hostname, and then later the hostname is translated back to IP address (for binding), but a DIFFERENT one, that's why my server program cannot accept any client connection.
Hope it helps if any others have the similar issue.
Peter