请教:如何将一个socket和一个文件handle联系起来?

  • 主题发起人 主题发起人 nova
  • 开始时间 开始时间

nova

知名会员
注册
2003-04-20
消息
265
荣誉分数
1
声望点数
128
请教高手:在windows 环境下如何将一个socket和一个文件handle联系起来?在Unix 和Linux中很容易用FILE *fp = fdopen ((SOCKET)sockid, "r"); 但windows (VC++6.0)似乎不能用一个文件打开一个TCP连接。
 
windows API没有那么不堪。MSDN都给你封装好了。看看CSocketFile这个class吧。
 
多谢chengchy。问题是我们现在在WINDOWS环境中仿真程序(GUI,IO等仿真是在WINDOWS下的)。等硬件来了后要移植到一种类似LINUX的操作系统中。因此得用低级socket 和file函数。有没有其它办法?
 
If you can not use MFC classes and would like to make those things more portable, try using _fdopen. This IS supported in Windows api. But still, when you port this piece code to LINUX, make sure modify the code.

You can do like this in the header file:
#define _fdopen fdopen

OR

You can modify your makefile and pass in pre-compiler options when on LINUX. likely say -DLINUX

then in the code, you can write:
#ifdefine LINUX
fdopen(.....)
#elif
_fdopen(....)
#endif
 
thanks again chengchy! I tried and it does not work. It gives me the same error as using fdopen(). The problem is that in windows, the socket handle is different from the file handle. While in all other major OS, they are same. I will take your suggestion to try to wrap CSocketFile with directive definitions.

thanks,

Nova
 
后退
顶部