1
socket: the combination of an IP address and a port number is called a socket.
winsock: windows sockets API的简称。
winsock 有两个版本: winsock1和winsock2.
这些API是在一个DLL 里实现的,所以你如果要使用这些函数的话,首先要
包含相应的头文件winsock.h 或winsock2.h. 然后链接 wsock32.lib或者ws2_32.lib.
我们平时动态载入一个DLL的话是使用LoadLibrary()函数。但是在这里我们使用OS提供的
WSAStartUP()函数。
在使用完后调用WSACleanUp().
2
wsagetlasterror()的返回值可以在winsock.h或winsock2.h中查找到。
3
wsastartup()函数中注意两点:
Do not use the next two fields, iMaxSockets and iMaxUdpDg . They are supposed to be the maximum number of concurrently open sockets and the maximum datagram size; however, to find the maximum datagram size you should query the protocol information through WSAEnumProtocols (see Chapter 2). The maximum number of concurrent sockets isn't some magic number—it depends more on the physical resources available.
Upon return, the wHighVersion of the WSADATA structure in the function WSAStartUP will be the latest version supported by the library on the current system.
4
accpet()何时返回?
ps:MFC中的socket相关的类..
5 send()函数的执行过程:
send返回成功只代表数据已经被发送,但是不保证已经到达对方的机器中.
如果你想保证数据被对方成功接收,你要使用自己的协议来保证.
虽然TCP可以是reliable的,但是send不是,当然微软可以把send做成是对方确认接收到后才返回,但是那样严重影响效率。
6 以send函数为例,以时间为线索来讲述socket I/O models.
最初windows提供了socket函数来创建一个socket, 默认情况下这个socket是同步的,我们可以调用ioctlsocket把它设置成异步的。
然后我们可以调用send函数进行数据的发送。 同步的socket不执行结束是不会返回的,所以会blocking, 虽然可以使用多线程来化解blocking
,但是这样如果在程序中有多个sockets的话,太多的线程会占用太多的系统资源。异步的sockets也同样需要不断的循环来实现操作。为了解决以上问题,windows提供了其它几种机制来避免这种问题,每一种机机制中包含几个对应的函数:
一
the select model:
int select( int nfds,
fd_set FAR * readfds,
fd_set FAR * writefds,
fd_set FAR * exceptfds,
const struct timeval FAR * timeout );
fd_set是一个sockets的集合, 当某个socket可读或可写时,select函数返回,你可以通过fd_set知道哪个socket可以执行什么操作。
把这个select函数放在一个while循环中,这样就避免了阻塞的问题。而且也不用开多线程了。
二
The WSAAsyncSelect Model
Winsock provides a useful asynchronous I/O model that allows an application to receive Windows message–based notification of network events on a socket. This is accomplished by calling the WSAAsyncSelect function after creating a socket. Before we continue, however, we need to make one subtle distinction. The WSAAsyncSelect and WSAEventSelect models provide asynchronous notification of the capability to read or write data. It does not provide asynchronous data transfer like the overlapped and completion port models.
When your application calls WSAAsyncSelect on a socket, the socket mode is automatically changed from blocking to the non-blocking mode
多个sockets可以使用同一个winproc,wParam代表是哪个socket的事件。
LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
三
The WSAEventSelect Model
Winsock provides another useful asynchronous event notification I/O model that is similar to the WSAAsyncSelect model that allows an application to receive event-based notification of network events on one or more sockets. This model is similar to the WSAAsyncSelect model because your application receives and processes the same network events listed in Table 5-3 that the WSAAsyncSelect model uses. The major difference with this model is that network events are notified via an event object handle instead of a window procedure.