fork download
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <winsock.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <thread>
  10.  
  11. using namespace std;
  12. #pragma comment(lib, "ws2_32.lib")
  13.  
  14.  
  15. TCHAR error[100];
  16. // buffer to hold our recived data
  17. char buf[64];
  18. // buffer to hold our sent data
  19. char sendData[64];
  20.  
  21. extern int a = 0, b = 0, c = 0, d = 0;
  22.  
  23.  
  24. class ThrArg {
  25. public:
  26. int arg1 = 0, arg2 = 0, port;
  27. char *ip;
  28. SOCKET client_class;
  29. DWORD thread_class;
  30. sockaddr_in from;
  31. int fromlen = sizeof(from);
  32. };
  33.  
  34.  
  35. SOCKET sock;
  36.  
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. void cleanBuf() //每傳送/接收一次,都要清除
  39. {
  40. for (int i = 0; i<64; i++)
  41. {
  42. buf[i] = NULL;
  43. }
  44. }
  45. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. void cleanSendData() //每傳送/接收一次,都要清除
  47. {
  48. for (int i = 0; i<64; i++)
  49. {
  50. sendData[i] = NULL;
  51. }
  52. }
  53. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. void keyin()
  55. {
  56. while (1)
  57. {
  58. cout << "please key in value of a:";
  59. cin >> a;
  60. cout << "value of a is " << a << "\n";
  61. Sleep(5000);
  62. }
  63. }
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66.  
  67.  
  68. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70. DWORD WINAPI receive_cmds(LPVOID lpParam)
  71. {
  72.  
  73. ThrArg *lpclass1 = (ThrArg *)lpParam;
  74. char ip_threadINreceive_cmds[17];
  75. int port_thread = lpclass1->port;
  76.  
  77.  
  78.  
  79.  
  80. // set our socket to the socket passed in as a parameter
  81. SOCKET current_client = lpclass1->client_class;
  82.  
  83.  
  84. // for error checking
  85. int res;
  86. int *ptr = &a;
  87. BOOL didRunFlag = 0;
  88.  
  89. strcpy(ip_threadINreceive_cmds, lpclass1->ip);
  90.  
  91.  
  92. ///////
  93.  
  94.  
  95.  
  96.  
  97.  
  98. if (didRunFlag == 0)
  99. {
  100. strcpy(sendData, "歡迎跟server連線");
  101. Sleep(1000);
  102. send(current_client, sendData, sizeof(sendData), 0);
  103. cout << "\n已對使用者" << ip_threadINreceive_cmds << " : " << port_thread << "發送歡迎訊息\n";
  104.  
  105. cleanBuf();
  106. cleanSendData();
  107.  
  108. didRunFlag = 1;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. // our recv loop
  115.  
  116.  
  117.  
  118.  
  119. while (1)
  120. {
  121. res = recv(current_client, buf, sizeof(buf), 0);// recv cmds
  122.  
  123.  
  124.  
  125. if (*ptr >= 1)
  126. {
  127. strcpy(sendData, "a的值是 ");
  128. char buffer[1];
  129. sprintf(buffer, "%d", a);
  130. strcat_s(sendData, buffer);
  131. strcat_s(sendData, " 的唷\n");
  132.  
  133. send(current_client, sendData, sizeof(sendData), 0);
  134. cout << sendData << "\n";
  135. printf("\n我做完了發送a的值的流程\n");
  136. cleanBuf();
  137. cleanSendData();
  138. }
  139.  
  140.  
  141.  
  142. if (res <= 0)
  143. {
  144. cout << "\n使用者"<< ip_threadINreceive_cmds<<":"<<port_thread<<"已斷線\n";
  145. closesocket(current_client);
  146. cleanBuf();
  147. cleanSendData();
  148. ExitThread(0);
  149. }
  150.  
  151. else if (strstr(buf, "bye"))
  152. { // dissconnected this user
  153. cout << "\n使用者" << ip_threadINreceive_cmds << ":" << port_thread << "已離線\n";
  154.  
  155. strcpy(sendData, "謝謝您,等候您下次使用");
  156. Sleep(10);
  157. send(current_client, sendData, sizeof(sendData), 0);
  158.  
  159. // close the socket associted with this client and end this thread
  160. closesocket(current_client);
  161. ExitThread(0);
  162.  
  163. cleanBuf();
  164. cleanSendData();
  165. }
  166. else if (strstr(buf, "1"))
  167. { // dissconnected this user
  168. printf("\n使用者丟1\n");
  169.  
  170. strcpy(sendData, "星期一猴子穿新衣");
  171. Sleep(10);
  172. send(current_client, sendData, sizeof(sendData), 0);
  173. printf("\n我有做完發送流程\n");
  174. cleanBuf();
  175. cleanSendData();
  176.  
  177.  
  178. }
  179.  
  180. else
  181. {
  182. cout << "有收到client端傳的消息 但沒有用處\n";
  183. cout << "res = " << res << "\n";
  184. }
  185.  
  186. res = 0;
  187. didRunFlag = didRunFlag + 1;
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. //else
  200. //{
  201. // strcpy(sendData, buf);
  202. // Sleep(10);
  203. // send(current_client, sendData, sizeof(sendData), 0);
  204. // cout << buf << "\n";
  205. // cout << "我有做完發送流程\n";
  206. // cleanBuf();
  207. // cleanSendData();
  208.  
  209. //}
  210.  
  211.  
  212.  
  213. }
  214.  
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  216.  
  217. int server()
  218. {
  219. WSADATA wsaData;
  220. sockaddr_in server;
  221.  
  222. // start winsock
  223. int ret = WSAStartup(0x101, &wsaData); // use highest version of winsock avalible
  224.  
  225. if (ret != 0)
  226. {
  227. return 0;
  228. }
  229.  
  230. // fill in winsock struct ...
  231. server.sin_family = AF_INET;
  232. server.sin_addr.s_addr = INADDR_ANY;
  233. server.sin_port = htons(4999); // listen on telnet port 4999
  234.  
  235. // create our socket
  236. sock = socket(AF_INET, SOCK_STREAM, 0);
  237.  
  238. if (sock == INVALID_SOCKET)
  239. {
  240. closesocket;
  241. return 0;
  242.  
  243. }
  244.  
  245. // bind our socket to a port(port 4999)
  246. if (::bind(sock, (struct sockaddr*)(sockaddr*)&server, sizeof(server)))
  247. {
  248. return 0;
  249. }
  250.  
  251.  
  252.  
  253. // listen for a connection
  254. if (listen(sock, 50) != 0)
  255. {
  256. closesocket;
  257. return 0;
  258.  
  259. }
  260.  
  261.  
  262.  
  263. }
  264.  
  265. void client() {
  266.  
  267.  
  268. ThrArg class1;
  269.  
  270. // socket that we snedzrecv data on
  271.  
  272.  
  273.  
  274.  
  275. // loop forever
  276. while (true)
  277. {
  278. // accept connections
  279. class1.client_class= accept(sock, (struct sockaddr*)&class1.from, &class1.fromlen);
  280.  
  281.  
  282.  
  283. class1.ip = inet_ntoa(class1.from.sin_addr);
  284. class1.port = htons(class1.from.sin_port);
  285.  
  286. cout << "\nserver接收到連線請求,連線ip:" << class1.ip << ",port:" << class1.port << endl;
  287.  
  288.  
  289.  
  290. // create our recv_cmds t hread and parse client socket as a parameter
  291. CreateThread(NULL, 0, receive_cmds, (LPVOID)&class1, 0, &class1.thread_class);
  292. }
  293.  
  294. }
  295.  
  296. int main()
  297. {
  298.  
  299.  
  300. printf("程式開始啦\r\n");
  301.  
  302. // our masterSocket(socket that listens for connections)
  303.  
  304. cout << "3秒後另外叫一個thread來控制全域變數\n";
  305. Sleep(3000);
  306. thread thread1(keyin);
  307.  
  308. // for our thread
  309.  
  310. server();
  311. client();
  312.  
  313. // shutdown winsock
  314. closesocket(sock);
  315. WSACleanup();
  316.  
  317. // exit
  318. return 0;
  319. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:20: fatal error: stdafx.h: No such file or directory
compilation terminated.
stdout
Standard output is empty