æ°æå¿
å¦:windowsç½ç»ç¼ç¨ç»å
¸å
¥é¨
ä½è
ï¼huyoo
ãã对äºä¸ä¸ªwindowsç½ç»ç¼ç¨åå¦è
ï¼ä¸é¢æ¹æ³æ¯ç»å
¸å
¥é¨ã
ããåå¦è
建议ä¸è¦ç¨MFCæä¾çç±»ï¼èç¨windows APIåä¸ä¸ªç®åæå¡å¨å客æ·ç«¯ï¼è¿æ ·æå©äºå¯¹socketç¼ç¨æºå¶çç解ã
ãã
ãã为äºç®åèµ·è§ï¼åºç¨ç¨åºæ¯åºäºMFCçæ å对è¯æ¡ã
ãã
ããWinsockç¨WINDOWS APIå®ç°ï¼
ãã
ãã(1)æå¡å¨ç«¯æ两个线ç¨ï¼
ããä¸»çº¿ç¨ â ä½ éè¦ç¼å以ä¸å½æ°æ¥å®ç°
ãã
ãã#define NETWORK_EVENT USER_MESSAGE+100 file://å®ä¹ç½ç»äºä»¶
ããsockaddr_in clientaddr; file://ææ¶åæ¾å®¢æ·ç«¯IPå°å
ãã
ããfile://èªå·±å®ä¹æ¶æ¯æ å°å½æ°ï¼å°ä¸é¢å®ä¹çç½ç»äºä»¶æ å°å°å¤çå½æ°
ããfile://OnNetEvent为ç½ç»äºä»¶å¤çå½æ°ï¼å®å¨ä¸é¢å®ä¹
ããON_MESSAGE(NETWORK_EVENT, OnNetEvent);
ãã
ããå¨ä½ 对è¯æ¡ä¸çåå§åå½æ°ä¸è°ç¨ä¸é¢çåå§åç½ç»çåå½æ°
ããBOOL InitNetwork() file://åå§åç½ç»
ãã{
ããfile://åå§åTCPåè®®
ããBOOL ret = WSAStartup(MAKEWORD(2,2), &wsaData);
ããif(ret != 0)
ãã{
ããMessageBox("åå§åå¥æ¥å失败!");
ããreturn FALSE;
ãã}
ãã
ããfile://å建æå¡å¨ç«¯å¥æ¥å
ããSOCKET serverSocket
ãã= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ããif(serverSocket == INVALID_SOCKET)
ãã{
ããMessageBox("å建å¥æ¥å失败!");
ããclosesocket(m_Socket);
ããWSACleanup();
ããreturn FALSE;
ãã}
ãã
ããfile://ç»å®å°æ¬å°ä¸ä¸ªç«¯å£ä¸
ããsockaddr_in localaddr;
ããlocaladdr.sin_family = AF_INET;
ããlocaladdr.sin_port = htons(1688);
ããlocaladdr.sin_addr.s_addr = 0;
ããif(bind(serverSocket ,(const struct sockaddr*)&localaddr,
ããsizeof(sockaddr)) == SOCKET_ERROR)
ãã{
ããMessageBox("ç»å®å°å失败!");
ããclosesocket(m_Socket);
ããWSACleanup();
ããreturn FALSE;
ãã}
ãã
ããfile://注åç½ç»å¼æ¥äºä»¶,m_hWnd为åºç¨ç¨åºç主对è¯æ¡æ主çªå£çå¥æ
ããWSAAsyncSelect(serverSocket, m_hWnd, NETWORK_EVENT,
ããFD_ACCEPT | FD_CLOSE | FD_READ | FD_WRITE);
ãã
ããlisten(serverSocket, 5); file://设置侦å¬æ¨¡å¼
ãã
ããreturn TRUE;
ãã}
ããfile://å®ä¹ç½ç»äºä»¶çååºå½æ°
ããvoid OnNetEvent(WPARAM wParam, LPARAM lParam)
ãã{
ããfile://è°ç¨APIå½æ°,å¾å°ç½ç»äºä»¶ç±»å
ããint iEvent = WSAGETSELECTEVENT(lParam);
ãã
ããfile://å¾å°ååºæ¤äºä»¶ç客æ·ç«¯å¥æ¥å
ããSOCKET pSock = (SOCKET)wParam;
ããswitch(iEvent)
ãã{
ããcase FD_ACCEPT: file://客æ·ç«¯è¿æ¥è¯·æ±
ãã{
ããOnAccept();
ãã
ããbreak;
ãã}
ããcase FD_CLOSE: file://客æ·ç«¯æå¼äºä»¶:
ãã{
ããOnClose(pSock);
ããbreak;
ãã}
ããcase FD_READ: file://ç½ç»æ°æ®å
å°è¾¾äºä»¶
ãã{
ããOnReceive(pSock);
ããbreak;
ãã}
ããcase FD_WRITE: file://åéç½ç»æ°æ®äºä»¶
ãã{
ããOnSend(pSock);
ããbreak;
ãã}
ããdefault: break;
ãã}
ãã}
ãã
ããvoid OnAccept(SOCET pSock) file://ååºå®¢æ·ç«¯è¿æ¥è¯·æ±å½æ°
ãã{
ããint len = sizeof(sockaddr);
ãã
ããfile://è°ç¨APIå½æ°,æ¥åè¿æ¥,并è¿åä¸ä¸ªæ°å¥æ¥å
ããfile://è¿å¯ä»¥è·å¾å®¢æ·ç«¯çIPå°å
ããSOCKET clientSocket = accept(serverSocket,
ãã(struct sockaddr*)&clientaddr, &len);
ãã
ããfile://为æ°çsocket注åå¼æ¥äºä»¶,注æ没æAcceptäºä»¶
ããif(WSAAsyncSelect(clientSocket ,m_hWnd, IP_EVENT,
ããFD_CLOSE | FD_READ | FD_WRITE) == SOCKET_ERROR)
ãã{
ããMessageBox("注åå¼æ¥äºä»¶å¤±è´¥!");
ããreturn;
ãããã}
ãã
ããfile://èªç¼å½æ°,å°æ¤å®¢æ·ç«¯çç¸å
³ä¿¡æ¯ä¿åä¸æ¥:å¥æ¥åã
ãã// IPå°åãç»éæ¶é´
ããsaveClientSocket(clientSocket,clientAddrï¼currentTimer);
ãã}
ãã
ããvoid OnClose(SOCET pSock)
ãã{
ããfile://èªç¼å½æ°,ç»æä¸ç¸åºç客æ·ç«¯çéä¿¡,éæ¾ç¸åºèµæºå¹¶åç¸åºå¤ç
ããendClientSocket(pSock);
ãã}
ãã
ããvoid OnSend(SOCET pSock)
ãã{
ããfile://èªç¼å½æ°,å¨ç»å®¢æ·ç«¯åæ°æ®æ¶åä¸äºé¢å¤ç
ããhandleOnSend(pSock);
ãã}
ãã
ããvoid OnReceive(SOCET pSock)
ãã{
ããrecv(...); file://è°ç¨APIå½æ°,读åºç½ç»ç¼å²åºä¸çæ°æ®å
ãã
ããfile://èªç¼å½æ°,å°æ¤æ°æ®å
åååºæ¤æ°æ®ç客æ·ç«¯
ããfile://clientSocketå°è£
æä¸æ¡ç½ç»æ¶æ¯
ããbuildNetMsg(...);
ãã
ããfile://èªç¼å½æ°,å°æ¤ç½ç»æ¶æ¯æ¾å
¥ä¸ä¸ªæ¶æ¯éåä¸,ç±å·¥ä½çº¿ç¨å»å¤ç
ããsaveNetMsg(...);
ããSetEvent(...); file://ç¨äºä»¶å¯¹è±¡è§¦åå·¥ä½çº¿ç¨
ãã}
ãã
ãã客æ·ç«¯ç»éåï¼éå³æèªå·±ç计ç®æºååç»æå¡å¨ï¼æå¡å¨æ¥å°åï¼æå®ä¿åä¸æ¥ãè¿æ ·æå¡å¨å°±å¯ä»¥æ¾ç¤ºææå¨çº¿å®¢æ·ç«¯çä¿¡æ¯äºï¼å
æ¬:客æ·ç«¯è®¡ç®æºåãIPå°åãç»éæ¶é´çã
ãã
ãã注æ: 客æ·ç«¯æ²¡æOnAccept()å½æ°ï¼ä½æOnConnect()å½æ°ã
ãã
ããå·¥ä½çº¿ç¨ â
ããå¨ä½ çåºç¨ç¨åºåå§åæ¶,å建并å¯å¨ä¸ä¸ªå·¥ä½çº¿ç¨
ãã
ããAfxBeginThread(WorkThread,this,THREAD_PRIORITY_NORMAL);
ããfile://thiså¯è½ä¸ºåºç¨ç¨åºç主对è¯æ¡æ主çªå£çå¥æ
ãã
ããUINT WorkThread(LPVOID pParam)
ãã{
ããwhile(1)
ãã{
ããfile://çå¾
å¤éäºä»¶å°æ¥
ããint ret = WaitForMultipleObject(...);
ãã
ããswitch(ret)
ãã{
ããcase OBJECT_0:
ãã{
ããif(bNewNetMsg) file://æ¥çç½ç»æ¶æ¯éåæ¯å¦ææ°çç½ç»æ¶æ¯
ãã{
ããreadNetMsg(...); file://å¦ææ°çç½ç»æ¶æ¯,å读åº
ããhandleNetMsg(...); file://å¤çæ¤ç½ç»æ¶æ¯
ãã}
ããbreak;
ãã}
ããcase OBJECT_0 + 1:
ãã{
ããfile://åéåºå¤ç
ããbreak;
ãã}
ããdefault: break;
ãã}
ãã
ããreturn 0;
ãã}
ãã
ãã客æ·ç«¯ä¸ºå线ç¨,ç»éæå¡å¨æ¶,ç¨connect()å½æ°ç»æå¡å¨åè¿æ¥è¯·æ±;
ãã客æ·ç«¯æ²¡æOnAccept()å½æ°,ä½æOnConnect()å½æ°ã
ããå¨OnConnect()å½æ°éååè¿æ¥è¯·æ±æ¶çé¢å¤ç;
ããå¨OnReceive()å½æ°éååºå¹¶å¤çç½ç»æ°æ®;
ããå¨OnClose()å½æ°éååºæå¡å¨çå
³éäºä»¶;
ããå¨OnSend()å½æ°éååæ°æ®æ¶çé¢å¤ç;
ãã
ããå¦æä½ è¿æ³å®ç°å客æ·ç«¯ä¹é´çå¨çº¿äº¤æµ(å³æè°çè天室),ä½ å¨å®¢æ·ç«¯è¿å¯ä»¥åºäºUDPåè®®
ããååä¸å¥å¤ç¹å¯¹å¤ç¹çå±åç½ç»æ模å模åï¼ä»¥åå¨åä½ èï¼ä½ å
æä¸é¢çç¨åºå®ç°ã
ãã
ãã以ä¸çI/Oå¼æ¥æ¨¡ååºäºWindowsçæ¶æ¯æºå¶,å¦å¤è¿å¯ä»¥ç¨äºä»¶æ¨¡åãéå 模åæå®æ端å£æ¨¡åï¼
ããå»ºè®®ä½ åèWindowsç½ç»ç¼ç¨æåä¹ç±»ç书ã
ãã
ããå¦æä½ è½å¯¹ä¸é¢çæºå¶å¾çç»,ä½ è¯å®å·²ç»å¯¹Winsockç¼ç½ç»ç¨åºçæºå¶æä¸å®ç解,æ¥ä¸æ¥ä½ å¯ä»¥è¿è¡æ´ç²¾å½©çç¼ç¨, ä¸ä»
å¯ä»¥å¨ç½ä¸ä¼ è¾æ®éæ°æ®,èä¸è¿
ããä»¥ä¼ è¾è¯é³ãè§é¢æ°æ®ï¼ä½ è¿å¯ä»¥èªå·±åä¸ä¸ªè天室ï¼åä½ çåå¦å¨å®éªå®¤çå±åç½éå¯ä»¥å
±ååäº«ä½ çææã
温馨提示:内容为网友见解,仅供参考