Client.cpp
Upload User: whzytd4
Upload Date: 2022-08-01
Package Size: 7346k
Code Size: 3k
Category:

WinSock-NDIS

Development Platform:

Visual C++

  1. // Client.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream.h>
  5. #include "winsock2.h"
  6. #pragma comment(lib, "WS2_32.lib")
  7. #define MAX_NUM_BUF 64
  8. char bufRecv[MAX_NUM_BUF];
  9. char bufSend[MAX_NUM_BUF];
  10. SOCKET sHost;
  11. bool bConning;
  12. WORD wVersionRequested;
  13. WSADATA wsd;
  14. int retVal;
  15. void InitMember(void);
  16. void ShowErrorMsg(void);
  17. bool RecvLine(SOCKET s, char* buf);
  18. void InitMember()
  19. {
  20. memset(bufRecv, 0, MAX_NUM_BUF);
  21. memset(bufSend, 0, MAX_NUM_BUF);
  22. sHost = INVALID_SOCKET;
  23. bConning = false;
  24. }
  25. void ShowErrorMsg()
  26. {
  27. int Error = GetLastError();
  28. HLOCAL hlocal = NULL;
  29. bool fok = FormatMessage(
  30. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  31. NULL, Error, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  32. (PTSTR)&hlocal, 0, NULL);
  33. if (hlocal != NULL)
  34. {
  35. MessageBox(NULL, (char*)LocalLock(hlocal), "CLIENT ERROR", MB_OK);
  36. LocalFree(hlocal);
  37. }
  38. }
  39. bool RecvLine(SOCKET s, char* buf)
  40. {
  41. bool retVal = TRUE; //返回值
  42. bool bLineEnd = FALSE; //行结束
  43. int nReadLen = 0; //读入字节数
  44. int nDataLen = 0; //数据长度
  45. while (!bLineEnd && bConning) //与客户端连接 没有换行
  46. {
  47. nReadLen = recv(s, buf + nDataLen, 1, 0);//每次接收一个字节
  48. //错误处理
  49. if (SOCKET_ERROR == nReadLen)
  50. {
  51. retVal= FALSE; //读数据失败
  52. break; //跳出循环
  53. }
  54. if (0 == nReadLen)//客户端关闭
  55. {
  56. retVal = FALSE; //读数据失败
  57. break ; //跳出循环
  58. }
  59. //读入数据
  60. if ('n' == *(buf + nDataLen)) //换行符
  61. {
  62. bLineEnd = TRUE; //接收数据结束
  63. }else{
  64. nDataLen += nReadLen; //增加数据长度
  65. }
  66. }
  67. return retVal;
  68. }
  69. int main(int argc, char* argv[])
  70. {
  71. InitMember();
  72. wVersionRequested = MAKEWORD(2, 2);
  73. int err = WSAStartup(wVersionRequested, &wsd);
  74. if(err != 0)
  75. {
  76. MessageBox(NULL, "can not find a usable Windows Sockets dll!", "ERROR", MB_OK);
  77. return -1;
  78. }
  79. sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  80. if(sHost == INVALID_SOCKET)
  81. {
  82. ShowErrorMsg();
  83. WSACleanup();
  84. return -1;
  85. }
  86. cout<<"Client Succeeded"<<endl;
  87. cout<<"Be ready to connect to server..."<<endl;
  88. LPHOSTENT hostEntry;
  89. char   hostname[MAX_NUM_BUF];
  90. gethostname(hostname, MAX_NUM_BUF);
  91. hostEntry = gethostbyname(hostname);
  92. if(!hostEntry)
  93. {
  94. ShowErrorMsg();
  95. return -1;
  96. }
  97. SOCKADDR_IN addrServ;
  98. addrServ.sin_family = AF_INET;
  99. addrServ.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
  100. addrServ.sin_port = htons(5500);
  101. retVal = connect(sHost, (SOCKADDR *)&addrServ, sizeof(SOCKADDR_IN));
  102. if(retVal == SOCKET_ERROR)
  103. {
  104. ShowErrorMsg();
  105. return -1;
  106. }else
  107. {
  108. bConning = true;
  109. }
  110. cout<<"Connect successfully!"<<endl;
  111. strcpy(bufSend, "Hello, Server!n");
  112. retVal = send(sHost, bufSend, strlen(bufSend), 0);
  113. if(retVal == SOCKET_ERROR)
  114. {
  115. ShowErrorMsg();
  116. return -1;
  117. }
  118. if (!RecvLine(sHost, bufRecv))
  119. {
  120. ShowErrorMsg();
  121. return -1;
  122. }
  123. cout<<bufRecv<<endl;
  124. closesocket(sHost);
  125. WSACleanup();
  126. cout << "Client exiting..." << endl;
  127. Sleep(20000);
  128. }