Advertisement
Guest User

ts

a guest
Nov 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. SERWER
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  3. #include <cstdio>
  4. #include <cstdlib>
  5.  
  6. #include <winsock2.h>
  7. #include <iostream>
  8. #include <string>
  9.  
  10. #pragma comment(lib, "Ws2_32.lib")
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16. WSADATA wsaData;
  17.  
  18. int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  19. if (result != NO_ERROR)
  20. printf("Initialization error.\n");
  21.  
  22. SOCKET mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  23. if (mainSocket == INVALID_SOCKET)
  24. {
  25. printf("Error creating socket: %ld\n", WSAGetLastError());
  26. WSACleanup();
  27. return 1;
  28. }
  29.  
  30. sockaddr_in service;
  31. memset(&service, 0, sizeof(service));
  32. service.sin_family = AF_INET;
  33. service.sin_addr.s_addr = inet_addr("127.0.0.1");
  34. service.sin_port = htons(27015);
  35.  
  36. if (bind(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
  37. {
  38. printf("bind() failed.\n");
  39. closesocket(mainSocket);
  40. return 1;
  41. }
  42.  
  43. if (listen(mainSocket, 1) == SOCKET_ERROR)
  44. printf("Error listening on socket.\n");
  45.  
  46. SOCKET acceptSocket = SOCKET_ERROR;
  47. printf("Waiting for a client to connect...\n");
  48.  
  49. while (acceptSocket == SOCKET_ERROR)
  50. {
  51. acceptSocket = accept(mainSocket, NULL, NULL);
  52. }
  53.  
  54. printf("Client connected.\n");
  55. mainSocket = acceptSocket;
  56.  
  57. int bytesSent;
  58. int bytesRecv = SOCKET_ERROR;
  59. const int size = 32;
  60. char sendbuf[1];
  61. char recvbuf[size];
  62.  
  63. bytesRecv = recv(mainSocket, recvbuf, size, 0);
  64. printf("Bytes received: %ld\n", bytesRecv);
  65. printf("Received text: %s\n", recvbuf);
  66. string t = recvbuf;
  67. cout << t.size() << endl;
  68. //printf("Bytes sent: %ld\n", bytesSent);
  69. int c=0;
  70. for (int i = 0; i < min(size, t.size()); i++) {
  71. sendbuf[0] = t[i];
  72. bytesSent = send(mainSocket, sendbuf, 1, 0);
  73. c++;
  74. }
  75. //bytesSent = send(mainSocket, sendbuf, strlen(sendbuf), 0);
  76. printf("Bytes sent: %ld\n", bytesSent);
  77.  
  78. cout << "C: " << c << endl;
  79. bytesSent = shutdown(mainSocket, SD_SEND);
  80. if (bytesSent == SOCKET_ERROR) {
  81. printf("shutdown failed with error: %d\n", WSAGetLastError());
  82. closesocket(mainSocket);
  83. WSACleanup();
  84. }
  85.  
  86. system("pause");
  87. }
  88.  
  89.  
  90. KLIENT
  91.  
  92.  
  93. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  94. #include <cstdio>
  95. #include <cstdlib>
  96.  
  97. #include <winsock2.h>
  98. #include <iostream>
  99. #include <string>
  100. #include <iostream>
  101.  
  102. #pragma comment(lib, "Ws2_32.lib")
  103.  
  104. using namespace std;
  105.  
  106. int main()
  107. {
  108. cout << "podaj tekst do przeslania" << endl;
  109. string t;
  110. getline(cin, t);
  111.  
  112.  
  113. WSADATA wsaData;
  114.  
  115. int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  116. if (result != NO_ERROR)
  117. printf("Initialization error.\n");
  118. SOCKET mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  119. if (mainSocket == INVALID_SOCKET)
  120. {
  121. printf("Error creating socket: %ld\n", WSAGetLastError());
  122. WSACleanup();
  123. return 1;
  124. }
  125. sockaddr_in service;
  126. memset(&service, 0, sizeof(service));
  127. service.sin_family = AF_INET;
  128. service.sin_addr.s_addr = inet_addr("127.0.0.1");
  129. service.sin_port = htons(27015);
  130.  
  131. if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR)
  132. {
  133. printf("Failed to connect.\n");
  134. WSACleanup();
  135. return 1;
  136. }
  137. int bytesSent;
  138. int bytesRecv = SOCKET_ERROR;
  139. const int size = 32;
  140. char sendbuf[size];
  141. for (int i = 0; i < min(size,t.size()); i++) {
  142. sendbuf[i] = t[i];
  143. }
  144.  
  145. char recvbuf[size] = "";
  146.  
  147. bytesSent = send(mainSocket, sendbuf, strlen(sendbuf), 0);
  148. printf("Bytes sent: %ld\n", bytesSent);
  149.  
  150. while (1)
  151. {
  152. bytesRecv = recv(mainSocket, recvbuf, 1, 0);
  153.  
  154. if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
  155. {
  156. printf("Connection closed.\n");
  157. break;
  158. }
  159.  
  160. if (bytesRecv < 0)
  161. return 1;
  162.  
  163. printf("Bytes received: %ld\n", bytesRecv);
  164. printf("Received text: %s\n", recvbuf);
  165. }
  166.  
  167. while (1) {
  168. if (connect(mainSocket, (SOCKADDR *)& service, sizeof(service)) == SOCKET_ERROR) {
  169. cout << "Zakonczono prace!"<<endl;
  170. system("pause");
  171. return 0;
  172. }
  173. }
  174.  
  175. system("pause");
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement