?????????????socket???????????????????:
??????????
#include <stdio.h>
#include <Winsock2.h> //windows socket??????
#pragma comment( lib?? "ws2_32.lib" )// ????Winsock2.h?????????
void main()
{
//?????winsocket
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1?? 1 );//????????????λ?????????????????λ???
err = WSAStartup( wVersionRequested?? &wsaData );//??winsock DLL??????????????????г??????Э??Winsock??汾???????????????????
if ( err != 0 )
{
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )//LOBYTE???????16????????λ??HIBYTE???????16????????????????????????
{
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET??SOCK_STREAM??0);//????socket??AF_INET?????Internet??????SOCK_STREAM???socket?????????????tcp??0???????Э???TCP/IP
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);    //htonl??????????????????????????????(to network long)
//INADDR_ANY?????????0.0.0.0??????
//???????????????“??????”??”
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(4000);//htons??????????????????????????????(to network short)
bind(sockSrv??(SOCKADDR*)&addrSrv??sizeof(SOCKADDR));//????????????????????socket??????????????????socket
listen(sockSrv??5);//socket?????????????????????
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv??(SOCKADDR*)&addrClient??&len);//??????????????????addrClient??????????????????????IP???????????????socket???????????????????????
char sendBuf[50];
sprintf(sendBuf??"Welcome %s to here!"??inet_ntoa(addrClient.sin_addr));//inet_ntoa???????????????????????????
send(sockConn??sendBuf??strlen(sendBuf)+1??0);
char recvBuf[50];
recv(sockConn??recvBuf??50??0);
printf("%s "??recvBuf);
closesocket(sockConn);
Sleep(2000);//2000????
}
WSACleanup();
}