关于CSocket的三个实际问题!

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:50:26
小弟才学Socket编程,请各位大哥大姐帮帮忙!
1.CSocket transSocket;
BOOL bRet = transSocket.Create(0,SOCK_DGRAM);
先定义了个CSocket对象,然后想创建个数据报套接字,为什么我的bRet老是0啊?create函数返回值是什么啊?

2.用sendto函数,buf必须是char *吗?我怎么将CString变成插入*啊?

3.有个SOCKET_EORROR,我看他的定义是(-1),我写的一个式子是:
int sendErr = transSocket.SendTo(buf,strlen(buf),8000,_T ("127.0.0.1"));
bool bsendOK = (SOCKET_ERROR != sendErr);
sengErr显示是-1,但是bsendOK的值表明SOCKET_ERROR与sendErr不等,请问这是为什么?

忘了说了,我用的是VS2005的C++,系统是XP SP2。忘大哥大姐多多支持啊,先谢一个,在端午节快乐!!!
怪我太急了。更正下,第二点的插入*是char *。还用我编写的是MFC对话框程序!再次谢谢!

刚才看错了MSDN上的解释是这样的
CSocket::Create
Call the Create member function after constructing a socket object to create the Windows socket and attach it.

BOOL Create(
UINT nSocketPort = 0,
int nSocketType = SOCK_STREAM,
LPCTSTR lpszSocketAddress = NULL
);

Parameters
nSocketPort
A particular port to be used with the socket, or 0 if you want MFC to select a port.

nSocketType
SOCK_STREAM or SOCK_DGRAM.

lpszSocketAddress
A pointer to a string containing the network address of the connected socket, a dotted number such as "128.56.22.8".

Return Value
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.

Remarks
Create then calls Bind to bind the socket to the specified address. The following socket types are supported:

SOCK_STREAM Provides sequenced, reliable, two-way, conn