1.å¦ä½ç解MFCç¨åºåWIN32ç¨åºçåºå«ï¼
2.CSocketä¹UDPç¼ç¨
å¦ä½ç解MFCç¨åºåWIN32ç¨åºçåºå«ï¼
ä½ çç解ä¸å¯¹
Win apiåªæ¯æ¥å£ï¼åªè½è¢«ç¨å°å«çä¸è¥¿éææ¾çåºä»·å¼ï¼æ¯å¦ç¨å¨äºMFCãDLLã串å£ãSOCKETï¼COM+ã
èMFCæ¯åºæ¬ç±»åºï¼æ¯å±äºC++çèç´ï¼WIN APIåºæ¬ä¸é½æ¯ç»æç±»åçåºç¨ï¼éé¢æ²¡æç±»ï¼æ以MFCå¯ä»¥è°ç¨APIï¼APIä¸è½è°ç¨MFCï¼
éè¿ä¼ å£ï¼å©ç¨MFCå¯ä»¥å°C++ç¨åºåç§çWINçç¯å¢ï¼MFCåªè½å¨WINç¯å¢ä¸ä½¿ç¨ï¼APIä¹æ¯
Win APIæ¯ Windowsç³»ç»ä¸ä½ Application Program Interface åºç¨ç¨åºæ¥å£ï¼MFCæ¯Microsoft Function Class 微软å½æ°ç±»åºï¼ åè æ´å åºå±ï¼å¨MFCç¨åºéï¼å¯ä»¥è°ç¨WIN APIçï¼MFCæ¬æ¥å°±ç¨äºå¾å¤WIN API,hfs源码MFCç¼åçç¨åºå¯ä»¥ç¨å¨åç§Windowsç¯å¢ä¸ç
CSocketä¹UDPç¼ç¨
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1);
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return;
}
SOCKET sersocket=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN seraddr;
seraddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons();
bind(sersocket,(SOCKADDR*)&seraddr,sizeof(SOCKADDR));
SOCKADDR clientaddr;
int len=sizeof(SOCKADDR);
char revbuf[];
char sendbuf[];
recvfrom(sersocket,revbuf,,0,(SOCKADDR*)&clientaddr,&len);
printf("%s\n",revbuf);
scanf("%s",&sendbuf);
sendto(sersocket,sendbuf,strlen(sendbuf)+1,0,(SOCKADDR*)&clientaddr,len);
closesocket(sersocket);
WSACleanup();
}
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,"ws2_.lib")
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1);
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return;
}
SOCKET sockclient=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN clientaddr;
clientaddr.sin_addr.S_un.S_addr=inet_addr(".0.0.1");
clientaddr.sin_family=AF_INET;
clientaddr.sin_port=htons();
int len=sizeof(SOCKADDR);
char revbuf[];
char sendbuf[];
printf("请è¾å ¥å 容ï¼\n");
while(1)
{
scanf("%s",&sendbuf);
sendto(sockclient,sendbuf,strlen(sendbuf)+1,0,(SOCKADDR*)&clientaddr,len);
recvfrom(sockclient,revbuf,,0,(SOCKADDR*)&clientaddr,&len);
printf("%s\n",revbuf);
}
closesocket(sockclient);
WSACleanup();
}
大åå°å¼ï¼CSocketåªæ¯è¿è¡äºå°è£ èå·²ï¼åçæ¯ä¸æ ·çï¼ç¼ç¨è¦æè·¯çµæ´»æè¡ã