1.CSocketä¹UDPç¼ç¨
2.å¦ä½ç解MFCç¨åºåWIN32ç¨åºçåºå«ï¼
3.为ä½è¦å¦ä¹ MFCï¼
CSocketä¹UDPç¼ç¨
#include <stdio.h>
#include <Winsock2.h>
#pragma comment(lib,hive 查看函数源码"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åªæ¯è¿è¡äºå°è£ èå·²ï¼åçæ¯ä¸æ ·çï¼ç¼ç¨è¦æè·¯çµæ´»æè¡ã
å¦ä½ç解MFCç¨åºåWINç¨åºçåºå«ï¼
ä½ çç解ä¸å¯¹
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,MFCç¼åçç¨åºå¯ä»¥ç¨å¨åç§Windowsç¯å¢ä¸ç
为ä½è¦å¦ä¹ MFCï¼
è¿æä¸ä¸ªé¢åæ们ä¸è½å¿½ç¥ï¼é£å°±æ¯ä¸ªäººè½¯ä»¶ãå°å软件ãå¨çªå «æä¹ç±»çå¨å®¢ç½ä¸è½¯ä»¶å¼åçæ ç®æä¸æï¼å¯ä»¥åç°å¤§é¨åçwindowsç¨åºå¼åçéæ±ï¼å¨å®¢ä»¬é½æ¯ç¨MFCæDephiæ¥å¼åï¼å½ç¶ï¼è¿æä¸äºæè¯è¨ãå¦æä½ è¦å¼åä¸æ¬¾ä¸ªäººç¨çå°è½¯ä»¶ï¼MFCæ¯å¾å¥½çéæ©ã
对äºè¿ç§ä¸ªäººè½¯ä»¶ãå°å软件ï¼MFCè¿æ¯æçå¾å¤ä¼å¿çï¼
1.ä½ç§¯å°ï¼éæç¼è¯åä½ç§¯ä¹ä¸å¤§ã
2.å¨åçæ¬çwindowsä¸å ¼å®¹æ§è¾å¥½ã
3.对windowsAPIçè°ç¨è¾æ¹ä¾¿ã
å¦ä¹ MFCä¸ä» ä» è¦å¦ä¹ ç¨MFCï¼è¿è¦å¦ä¹ MFCçæ¡æ¶è®¾è®¡ææ³ã
å¦æä» ä» ä¼ç¨MFCçè¯ï¼å¯è½å¨æ¾å·¥ä½çæ¶åï¼ä¸æ¦å·¥ä½å 容离å¼äºMFCï¼å°±ä»ä¹ä¹ä¸ä¼äºãMFCä½ä¸ºä¸ä¸ªè¯çäºå¹´çæççæ¡æ¶ï¼å ¶è®¾è®¡ææ³æ¯å¾值å¾æ们å¦ä¹ çãç²ç¥è¯»ä¸è¯»MFCæºä»£ç å°±ä¼åç°MFCçå害ä¹å¤ï¼å¾å¤ææ³é½è®©äººå¾ä½©æï¼ææ¶åçå®ä¸æ®µæºä»£ç åï¼å¿éæèªæå¹ãæ æ³æ³è±¡MFCå¨é£ä¸ªè¿C++çSTLé½è¿æ²¡åºç°ç年代ï¼å°±ç¨æ³åç¼ç¨ææ¯ååºäºCListãCArrayãCMapç容å¨ãMFCä¸ä» ä» æ¯å°é¢åè¿ç¨çAPIå°è£ 为é¢å对象çå½¢å¼ï¼ä»è¿å¨èååäºå¾å¤äºæ ï¼å æ¬ç®åä¸äºè¿ç¨ï¼æé«ä¸äºç¨³å®æ§ãå¦æ让ææé¢åè¿ç¨çwindows
socket APIå°è£ æé¢å对象çç±»ï¼æè¯å®ä¼è¿æ ·æ³ï¼å¯ï¼socketAPIå ä¹æ¯ä¸ªå½æ°ç第ä¸ä¸ªåæ°é½æ¯SOCKETç»æä½ï¼è¿æ¯é¢åè¿ç¨çç¹ç¹ï¼å¦æè¦å°è£ 为é¢å对象çç±»ï¼èªç¶æ¯æè¿ä¸ªç»æä½ä½ä¸ºç±»çæååéï¼ç¶åå°è£ socketAPIå½æ°ä¸ºç±»çæ¹æ³ï¼æä¾å¤§éçé»è®¤åæ°ï¼å°±OKãå®é ä¸å¹¶éè¿æ ·ï¼è¯»ä¸è¯»MFCä¸CSocketççæºä»£ç ï¼å°±ä¼åç°å®é ä¸å¹¶éå¦æ¤ç®åãMFCçCSocketç±»éï¼åäºå¤§éçå·¥ä½ï¼ä½¿æ们ç¨èµ·æ¥å¤§å¤§çæ¹ä¾¿äºãå¦æä½ ä¹åä¸ç´ç¨socketAPIæ¥å¼åç½ç»ç¨åºï¼åæ¥ç¨MFCçCAsyncSocketåCSocketçè¯ï¼ä¼è§å¾ï¼å¤ªæ¹ä¾¿äºï¼ä»ç»è¯»ä¸è¯»MFCæºç ï¼çç对æ们å°æ¥ç¼åèªå·±çç±»ã设计èªå·±çæ¡æ¶ï¼æå¾å¤§å¸®å©ï¼
ä¸ä¸ªå¥½çç¨åºåï¼ä¸è½åªæ¯ä¼ç¨ç°æç±»åºçæ¹æ³ã
ä¸ä¸ªå¥½çç¨åºåï¼åºè¯¥ä¼å°è£ ãä¼è®¾è®¡ç±»åºãä¼è®¾è®¡æ¡æ¶ã
å¨ä¸ä¸ªå®é ç项ç®ä¸å¾å¾æ¯è¿æ ·çï¼æ们éè¦å¼åä¸ä¸ªwindowså¹³å°ä¸ç软件ï¼ç´æ¥ç¨windowsAPIå§ï¼1.太麻ç¦2.é¢åè¿ç¨ç代ç åçè¶å¤è¶å¤´ç¼3.å¯éç¨æ§ä¸å¼ºã使ç¨ç°æç±»åºå§ï¼ææ¶åæ æ³æ»¡è¶³æ们ç¹å®çéæ±ãè¿æ¶åï¼æ们就éè¦èªå·±å¨æå°è£ APIï¼èªå·±å¨ææ´¾çç°æç±»åºä¸çç±»ï¼æ¥æ·»å æ¹æ³ãå¦æä½ ä¸äºè§£ç±»åºï¼æ´¾çä¸æ¥åå°±ä¼åç°æ ¹æ¬æ ä»ä¸æãå¾å¤å ¬å¸å¨ä¸äºåäºå¾ä¹ ç项ç®ä¸ï¼å¾å¾é½æ¯æèªå·±çç±»åºãèªå·±çæ¡æ¶ï¼å¹¶ä¸æçå®ååæ©å±ãè¿ä¹æ¯æ们è¦å¦ä¹ ç±»åºãæ¡æ¶è®¾è®¡çåå ã