Ws2_32.lib
#include <WinSock2.h> #include <stdio.h> void main() {
WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } /* The WinSock DLL is acceptable. Proceed. */
SOCKET sockSrv = socket(AF_INET,SOCK_DGRAM, 0 ); SOCKADDR_IN addrSrv ; addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(6000); bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR);
char recvBuf[100]; recvfrom(sockSrv, recvBuf,100,0,(SOCKADDR*)&addrClient,&len); printf("%s/n",recvfrom); closesocket(sockSrv); WSACleanup();
}
--------------------------------------
WSAStartup () 用于加载套接字 1 加载套接字库 2 进行套接字库的版本协商
socket
The socket function creates a socket that is bound to a specific service provider.
SOCKET socket( int af, int type, int protocol ); 创建套接字, 第一个参数 指点地址组,AF——INET(TCPIP) 第二是 socket 的类型 (socket——stream or socket——dgram)第三个是 与特定的地址家族相关的协议Return ValuesIf no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.
-----------------------------
The