Socket.cpp
上传用户:garry_shen
上传日期:2015-04-15
资源大小:45647k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include "wsa_xtra.h"
  4. #include "Socket.h"
  5. #include "..XMudClient.h"
  6. HWND hWinMain;
  7. u_long GetIPAddr(LPTSTR szHost)
  8. {
  9. LPHOSTENT lpstHost;
  10. u_long lAddr=INADDR_ANY;
  11. if(*szHost){
  12. lAddr=inet_addr(szHost);
  13. if((lAddr==INADDR_NONE)&&(strcmp(szHost,"255.255.255.255"))){
  14. lpstHost=gethostbyname(szHost);
  15. if(lpstHost)
  16. lAddr=*((u_long*)(lpstHost->h_addr));
  17. else
  18. lAddr=INADDR_ANY;
  19. }
  20. }
  21. return(lAddr);
  22. }
  23. SOCKET InitClient(PSOCKADDR_IN pstName,HWND hWnd,u_int nAsyncMsg)
  24. {
  25. int nRet;
  26. SOCKET hSock;
  27. hWinMain=hWnd;
  28. hSock=socket(AF_INET,SOCK_STREAM,0);
  29. if(hSock==INVALID_SOCKET) {
  30. }else{
  31. nRet=WSAAsyncSelect(hSock,hWnd,nAsyncMsg,(FD_CONNECT | FD_READ | FD_WRITE | FD_CLOSE));
  32. if(nRet==SOCKET_ERROR){
  33. closesocket(hSock);
  34. hSock=INVALID_SOCKET;
  35. }else{
  36. nRet=connect(hSock,(LPSOCKADDR)pstName,SOCKADDR_LEN);
  37. if(nRet==SOCKET_ERROR){
  38. int WSAErr=WSAGetLastError();
  39. if(WSAErr!=WSAEWOULDBLOCK){
  40. closesocket(hSock);
  41. hSock=INVALID_SOCKET;
  42. }
  43. }
  44. }
  45. }
  46. return(hSock);
  47. }
  48. int CloseConn(SOCKET &hSock,LPTSTR lpstrBuf,int len,HWND hWnd)
  49. {
  50. int nRet=0;
  51. TCHAR achDiscard[BUF_SIZE];
  52. int cbBytesToDo=len,cbBytesDone=0;
  53. if(hSock!=INVALID_SOCKET){
  54. if(hWnd){
  55. }
  56. nRet=shutdown(hSock,1);
  57. for(nRet=1;(nRet&&(nRet!=SOCKET_ERROR));){
  58. if(lpstrBuf){
  59. nRet=recv(hSock,&lpstrBuf[cbBytesDone],cbBytesToDo,0);
  60. if(nRet&&(nRet!=SOCKET_ERROR)){
  61. cbBytesToDo-=nRet;
  62. cbBytesDone+=nRet;
  63. }
  64. }else
  65. nRet=recv(hSock,(LPTSTR)achDiscard,BUF_SIZE,0);
  66. closesocket(hSock);
  67. }
  68. hSock=INVALID_SOCKET;
  69. }
  70. return(nRet);
  71. }
  72. extern CRITICAL_SECTION csSend;
  73. int SendData(SOCKET hSock,LPTSTR lpstrSendData,int nLen)
  74. {
  75. static int cbSentToServer=0;
  76. int nRet,nBytesSent=0;
  77. if(hSock!=INVALID_SOCKET){
  78. while(cbSentToServer<nLen){
  79. nRet=send(hSock,&lpstrSendData[cbSentToServer],nLen-cbSentToServer,0);
  80. if(nRet==SOCKET_ERROR){
  81. int WSAErr=WSAGetLastError();
  82. if(WSAErr!=WSAEWOULDBLOCK){
  83. return WS_ERROR;
  84. }
  85. return WS_WAIT;
  86. }
  87. cbSentToServer+=nRet;
  88. }
  89. }
  90. cbSentToServer=0;
  91. return WS_OK;
  92. }
  93. int RecvData(SOCKET hSock,LPTSTR lpstrBuf,int len)
  94. {
  95. int cbBytesRcvd=0;
  96. int nRet=0,WSAErr;
  97. while(cbBytesRcvd<len){
  98. nRet=recv(hSock,&(lpstrBuf[cbBytesRcvd]),len-cbBytesRcvd,0);
  99. if(nRet==SOCKET_ERROR){
  100. WSAErr=WSAGetLastError();
  101. if(WSAErr!=WSAEWOULDBLOCK){
  102. return WS_ERROR;
  103. }
  104. goto recv_end;
  105. }
  106. else if(nRet==0)
  107. goto recv_end;
  108. else
  109. cbBytesRcvd+=nRet;
  110. }
  111. recv_end:
  112. return(cbBytesRcvd);
  113. }