Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. #undef UNICODE
  2. #define WIN32_LEAN_AND_MEAN
  3.  
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <ws2tcpip.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <thread>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14. // Need to link with Ws2_32.lib
  15. #pragma comment (lib, "Ws2_32.lib")
  16. // #pragma comment (lib, "Mswsock.lib")
  17.  
  18.  
  19.  
  20. WSADATA wsaData;
  21. int iResult;
  22.  
  23. HANDLE CompletionPort;
  24. SOCKET ListenSocket = INVALID_SOCKET;
  25. SOCKET ClientSocket = INVALID_SOCKET;
  26.  
  27. OVERLAPPED Overlapped;
  28. WSABUF DataBuf;
  29. CHAR Buffer[200];
  30. DWORD BytesSEND;
  31. DWORD BytesRECV;
  32. DWORD RecvBytes;
  33.  
  34. sockaddr_in local;
  35.  
  36. DWORD Flags;
  37. int iSendResult;
  38.  
  39.  
  40. void serverWorkerThread() {
  41.  
  42. OVERLAPPED lpol2;
  43. DWORD Flags = 0;
  44. DWORD BytesTransferred;
  45. DWORD is = 0;
  46. DWORD sentBytes;
  47. bool kage = 0;
  48. WSABUF wsabuf;
  49. CHAR Buffer2[200];
  50.  
  51.  
  52. while (true) {
  53.  
  54. if (GetQueuedCompletionStatus(CompletionPort, &BytesTransferred, &is, (LPOVERLAPPED*)& lpol2, INFINITE) == 0)
  55.  
  56. {
  57.  
  58. printf("GetQueuedCompletionStatus() failed with error %dn", GetLastError());
  59.  
  60.  
  61.  
  62. }
  63.  
  64. else
  65.  
  66. printf("GetQueuedCompletionStatus() is OK!n");
  67.  
  68. cout << DataBuf.buf << endl;
  69.  
  70. cout << is;
  71. DataBuf.len = 14;
  72. ZeroMemory(&(Overlapped), sizeof(OVERLAPPED));
  73.  
  74.  
  75.  
  76.  
  77.  
  78. if (WSASend(ClientSocket, &DataBuf, 1, &sentBytes, 0,
  79.  
  80. NULL, NULL) == SOCKET_ERROR)
  81.  
  82. {
  83.  
  84. if (WSAGetLastError() != ERROR_IO_PENDING)
  85.  
  86. {
  87.  
  88. printf("WSASend() failed with error %dn", WSAGetLastError());
  89.  
  90.  
  91.  
  92. }
  93.  
  94. }
  95.  
  96. else
  97.  
  98. printf("WSASend() is OK!n");
  99.  
  100.  
  101. ZeroMemory(&(lpol2), sizeof(OVERLAPPED));
  102.  
  103. wsabuf.buf = Buffer2;
  104. wsabuf.len = 200;
  105. BytesTransferred = 0;
  106. is = 0;
  107.  
  108.  
  109. ZeroMemory(&(Overlapped), sizeof(OVERLAPPED));
  110. Flags = 0;
  111. if (WSARecv(ClientSocket, &wsabuf, 1, &RecvBytes, &Flags, &Overlapped, NULL) == SOCKET_ERROR)
  112.  
  113. {
  114.  
  115. if (WSAGetLastError() != ERROR_IO_PENDING)
  116.  
  117. {
  118.  
  119. printf("WSARecv() failed with error %dn", WSAGetLastError());
  120.  
  121.  
  122. }
  123.  
  124. }
  125.  
  126. else printf("WSARecv() is OK!n");
  127.  
  128.  
  129. }
  130.  
  131. }
  132.  
  133. int __cdecl main(void)
  134. {
  135.  
  136.  
  137. // Initialize Winsock
  138. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  139. if (iResult != 0) {
  140. printf("WSAStartup failed with error: %dn", iResult);
  141. return 1;
  142. }
  143.  
  144. CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
  145.  
  146.  
  147.  
  148. //Now we populate the sockaddr_in structure
  149. local.sin_family = AF_INET; //Address family
  150. local.sin_addr.s_addr = INADDR_ANY; //Wild card IP address
  151. local.sin_port = htons((u_short)20248); //port to use
  152.  
  153.  
  154. // Create a SOCKET for connecting to server
  155. ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
  156. if (ListenSocket == INVALID_SOCKET) {
  157. printf("socket failed with error: %ldn", WSAGetLastError());
  158. WSACleanup();
  159. return 1;
  160. }
  161.  
  162. // Setup the TCP listening socket
  163.  
  164.  
  165.  
  166.  
  167. iResult = bind(ListenSocket, (sockaddr*)& local, sizeof(local));
  168. if (iResult == SOCKET_ERROR) {
  169. printf("bind failed with error: %dn", WSAGetLastError());
  170.  
  171. closesocket(ListenSocket);
  172. WSACleanup();
  173. return 1;
  174. }
  175.  
  176.  
  177.  
  178. iResult = listen(ListenSocket, SOMAXCONN);
  179. if (iResult == SOCKET_ERROR) {
  180. printf("listen failed with error: %dn", WSAGetLastError());
  181. closesocket(ListenSocket);
  182. WSACleanup();
  183. return 1;
  184. }
  185. DataBuf.len = 2000;
  186. DataBuf.buf = Buffer;
  187. thread t1(serverWorkerThread);
  188. t1.detach();
  189.  
  190. DWORD iss = 34543;
  191.  
  192.  
  193.  
  194. while (TRUE)
  195.  
  196. {
  197.  
  198. ClientSocket = WSAAccept(ListenSocket, NULL, NULL, NULL, 0);
  199. if (ClientSocket == INVALID_SOCKET) {
  200. printf("accept failed with error: %dn", WSAGetLastError());
  201. closesocket(ListenSocket);
  202. WSACleanup();
  203. return 1;
  204. }
  205.  
  206.  
  207.  
  208.  
  209. if (CreateIoCompletionPort((HANDLE)ClientSocket, CompletionPort, 43343, 0) == NULL)
  210.  
  211. {
  212.  
  213. printf("CreateIoCompletionPort() failed with error %dn", GetLastError());
  214.  
  215. return 1;
  216.  
  217. }
  218.  
  219. else
  220.  
  221. printf("CreateIoCompletionPort() is OK!n");
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228. Flags = 0;
  229. if (WSARecv(ClientSocket, &DataBuf, 1, &RecvBytes, &Flags, &Overlapped, NULL) == SOCKET_ERROR)
  230.  
  231. {
  232.  
  233. if (WSAGetLastError() != ERROR_IO_PENDING)
  234.  
  235. {
  236.  
  237. printf("WSARecv() failed with error %dn", WSAGetLastError());
  238.  
  239. return 1;
  240.  
  241. }
  242.  
  243. }
  244.  
  245. else printf("WSARecv() is OK!n");
  246.  
  247.  
  248.  
  249. }
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. // shutdown the connection since we're done
  259. iResult = shutdown(ClientSocket, SD_SEND);
  260. if (iResult == SOCKET_ERROR) {
  261. printf("shutdown failed with error: %dn", WSAGetLastError());
  262. closesocket(ClientSocket);
  263. WSACleanup();
  264. return 1;
  265. }
  266.  
  267. // cleanup
  268. closesocket(ClientSocket);
  269. WSACleanup();
  270.  
  271. return 0;
  272. }
  273.  
  274. #define WIN32_LEAN_AND_MEAN
  275. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  276.  
  277. #include <windows.h>
  278. #include <winsock2.h>
  279. #include <ws2tcpip.h>
  280. #include <stdlib.h>
  281. #include <stdio.h>
  282. #include <iostream>
  283.  
  284.  
  285.  
  286. // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  287. #pragma comment (lib, "Ws2_32.lib")
  288. #pragma comment (lib, "Mswsock.lib")
  289. #pragma comment (lib, "AdvApi32.lib")
  290.  
  291. using namespace std;
  292.  
  293. #define DEFAULT_BUFLEN 512
  294. #define DEFAULT_PORT "27015"
  295.  
  296. int __cdecl main(int argc, char** argv)
  297. {
  298. WSADATA wsaData;
  299. SOCKET ConnectSocket = INVALID_SOCKET;
  300. SOCKADDR_IN ServerAddr;
  301.  
  302. // Server/receiver port to connect to
  303.  
  304. unsigned int Port = 20248;
  305. //struct addrinfo* result = NULL,
  306. // * ptr = NULL,
  307. // hints;
  308. const char* sendbuf = "this is a test";
  309. char recvbuf[DEFAULT_BUFLEN];
  310. int iResult;
  311. int recvbuflen = DEFAULT_BUFLEN;
  312.  
  313. cout << sendbuf << endl;
  314.  
  315. // Initialize Winsock
  316. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  317. if (iResult != 0) {
  318. printf("WSAStartup failed with error: %dn", iResult);
  319. return 1;
  320. }
  321.  
  322.  
  323.  
  324.  
  325.  
  326. // Create a SOCKET for connecting to server
  327. ConnectSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
  328. if (ConnectSocket == INVALID_SOCKET) {
  329. printf("socket failed with error: %ldn", WSAGetLastError());
  330. WSACleanup();
  331. return 2;
  332. }
  333.  
  334.  
  335.  
  336. // IPv4
  337.  
  338. ServerAddr.sin_family = AF_INET;
  339. // Port no.
  340. ServerAddr.sin_port = htons(Port);
  341. // The IP address
  342. ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  343.  
  344.  
  345. // Connect to server.
  346. iResult = connect(ConnectSocket, (SOCKADDR*)& ServerAddr, sizeof(ServerAddr));
  347. if (iResult == SOCKET_ERROR) {
  348. closesocket(ConnectSocket);
  349. ConnectSocket = INVALID_SOCKET;
  350. printf("socket failed with error: %ldn", WSAGetLastError());
  351. WSACleanup();
  352. return 3;
  353. }
  354.  
  355.  
  356.  
  357.  
  358.  
  359. if (ConnectSocket == INVALID_SOCKET) {
  360. printf("Unable to connect to server!n");
  361. WSACleanup();
  362. return 4;
  363. }
  364.  
  365. // Send an initial buffer
  366. iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
  367. if (iResult == SOCKET_ERROR) {
  368. printf("send failed with error: %dn", WSAGetLastError());
  369. closesocket(ConnectSocket);
  370. WSACleanup();
  371. return 5;
  372. }
  373.  
  374. printf("Bytes Sent: %ldn", iResult);
  375.  
  376. // shutdown the connection since no more data will be sent
  377. iResult = shutdown(ConnectSocket, SD_SEND);
  378. if (iResult == SOCKET_ERROR) {
  379. printf("shutdown failed with error: %dn", WSAGetLastError());
  380. closesocket(ConnectSocket);
  381. WSACleanup();
  382. return 6;
  383. }
  384.  
  385. // Receive until the peer closes the connection
  386. do {
  387.  
  388. iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  389. if (iResult > 0)
  390. printf("Bytes received: %dn", iResult);
  391. else if (iResult == 0)
  392. printf("Connection closedn");
  393. else
  394. printf("recv failed with error: %dn", WSAGetLastError());
  395.  
  396. } while (iResult > 0);
  397.  
  398. // cleanup
  399. closesocket(ConnectSocket);
  400. WSACleanup();
  401.  
  402. return 0;
  403.  
  404.  
  405.  
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement