fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "winsock2.h"
  5. #pragma comment(lib, "ws2_32.lib")
  6.  
  7. #define BUF_SIZE 64
  8.  
  9. int main(int argc, char* argv[]) {
  10. WSADATA wsd; //WSADATA變數
  11. SOCKET sServer; //伺服器Socket
  12. SOCKET sClient; //客戶端Socket
  13. SOCKADDR_IN addrServ;; //伺服器地址
  14. char buf[BUF_SIZE]; //接收資料緩衝區
  15. int retVal; //返回值
  16. int bufNum = 0;
  17. //Select模式的相關變數
  18. FD_SET socketSet; //伺服器Sockets集合
  19. FD_SET readSet; //測試可讀性的Sockets集合
  20. FD_SET writeSet; //測試可寫性的Sockets集合
  21. bool bSent; //是否回傳訊息給Client的記號
  22. //初始化Socket動態庫
  23. if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
  24. cout << "WSAStartup failed!" << endl;
  25. system("pause");
  26. return 1;
  27. }
  28.  
  29. //初始化Sockets集合
  30. FD_ZERO(&socketSet);
  31.  
  32. //建立Socket
  33. sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  34. if(INVALID_SOCKET == sServer) {
  35. cout << "socket failed!" << endl;
  36. WSACleanup();//釋放Socket資源;
  37. system("pause");
  38. return -1;
  39. }
  40. //將伺服器Socket加入伺服器Sockets集合
  41. FD_SET(sServer, &socketSet);
  42.  
  43. //伺服器Socket地址
  44. addrServ.sin_family = AF_INET;
  45. addrServ.sin_port = htons(4999);
  46. addrServ.sin_addr.s_addr = INADDR_ANY;
  47.  
  48. //綁定Socket
  49. retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
  50. if(SOCKET_ERROR == retVal) {
  51. cout << "bind failed!" << endl;
  52. closesocket(sServer); //關閉Socket
  53. WSACleanup(); //釋放Socket資源;
  54. system("pause");
  55. return -1;
  56. }
  57.  
  58. //開始監聽
  59. retVal = listen(sServer, 1);
  60. if(SOCKET_ERROR == retVal) {
  61. cout << "listen failed!" << endl;
  62. closesocket(sServer); //關閉Socket
  63. WSACleanup(); //釋放Socket資源;
  64. system("pause");
  65. return -1;
  66. }
  67. cout << "Server is listening..." << endl;
  68. //待續_Start
  69. //伺服器只管接收客戶端的連線以及問候語,所以只要判斷可讀性便可
  70. while (1) {
  71. FD_ZERO(&readSet); //清空測試可讀性
  72. FD_ZERO(&writeSet); //清空測試可寫性
  73. bSent = false;
  74. readSet = socketSet; //將要測試可讀性的Socket加入readSet
  75. writeSet = socketSet; //將要測試可讀性的Socket加入writeSet
  76. if (SOCKET_ERROR == select(0, &readSet, &writeSet, NULL, NULL)) {
  77. cout << "select() returned with error " << WSAGetLastError() << endl;
  78. closesocket(sServer); //關閉Socket
  79. WSACleanup(); //釋放Socket資源;
  80. system("pause");
  81. return -1;
  82. }
  83. //接下來看看測試的結果
  84. if (FD_ISSET(sServer, &readSet)) {//伺服器Socket還在集合中嗎?
  85. //在,那表示有客戶端的連線進來了
  86. cout << "Client is connecting...";
  87. sClient = accept(sServer, NULL, NULL);
  88. if(INVALID_SOCKET == sClient) {
  89. cout << "accept failed!" << endl;
  90. closesocket(sServer); //關閉Socket
  91. WSACleanup(); //釋放Socket資源;
  92. system("pause");
  93. return -1;
  94. }
  95. cout << " connected." << endl;
  96. //將客戶端Socket加入伺服器Sockets集合
  97. FD_SET(sClient, &socketSet);
  98. system("pause");
  99. }
  100. if (FD_ISSET(sClient, &readSet)) {//客戶端Socket還在集合中嗎?
  101. //接收客戶端資料
  102. ZeroMemory(buf, BUF_SIZE);
  103. retVal = recv(sClient, buf, BUF_SIZE, 0);
  104. if (SOCKET_ERROR == retVal) {
  105. cout << "recv failed!" << endl;
  106. closesocket(sServer); //關閉Socket
  107. closesocket(sClient); //關閉Socket
  108. WSACleanup(); //釋放Socket資源;
  109. system("pause");
  110. return -1;
  111. }
  112. if (0==retVal) break;
  113. cout<<"MSG Received from Client: "<<buf<<endl;
  114. bSent = true;
  115. }
  116. if (FD_ISSET(sClient, &writeSet) && bSent) {//客戶端Socket還在集合中嗎?
  117. if (SOCKET_ERROR == retVal) {
  118. cout << "recv failed!" << endl;
  119. closesocket(sServer); //關閉Socket
  120. closesocket(sClient); //關閉Socket
  121. WSACleanup(); //釋放Socket資源;
  122. system("pause");
  123. return -1;
  124. }
  125. retVal = send(sClient, buf, strlen(buf), 0);
  126. if (!strcmp(buf,"bye")) break;
  127. }
  128. Sleep(100); //休息一段時間以降低在While迴圈中CPU的使用率
  129. }
  130. //待續_End
  131.  
  132. //退出
  133. closesocket(sServer); //關閉Socket
  134. closesocket(sClient); //關閉Socket
  135. WSACleanup(); //釋放Socket資源;
  136.  
  137. system("pause");
  138. return 0;
  139. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:22: fatal error: winsock2.h: No such file or directory
compilation terminated.
stdout
Standard output is empty