套接字

#include<stdio.h>
#include<Winsock2.h>
void main()
{
	WORD wVersionRequested;
WSADATA wsaData;
int err;
 
wVersionRequested = MAKEWORD( 2, 2 );
 
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 ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 )
 {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    WSACleanup( );
    return; 
}
SOCKET recvsocket=socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in addr;

addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addr.sin_family=AF_INET;
addr.sin_port=htons(6008);
bind(recvsocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));

char recvBuf[100];
int len=sizeof(SOCKADDR);

while(1)
{recvfrom(recvsocket,recvBuf,100,0,(SOCKADDR*)&addr,&len);

printf("%s\n",recvBuf);}
}
 

你可能感兴趣的:(套接字)