Advertisement
Alchemik96

PWSS UDP SERVER CIENI

Mar 28th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. #include <winsock.h>
  3. #pragma comment(lib, "Ws2_32.lib")
  4. #include <conio.h>
  5. #include <locale.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <conio.h>
  10. #include <string.h>
  11. #include <string>
  12. #include <time.h>
  13. using namespace std;
  14. int main() {
  15. //formalności
  16. setlocale(LC_ALL, "Polish");
  17. WSADATA wsadata;
  18. WSAStartup(MAKEWORD(2, 2), &wsadata);
  19. srand(time(NULL));
  20.  
  21. //zmienne, bufor itd.
  22. SOCKET recvsocket = socket(AF_INET, SOCK_DGRAM, 0);
  23. sockaddr_in recvaddr = { 0 };
  24. recvaddr.sin_family = AF_INET;
  25. recvaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  26. recvaddr.sin_port = htons(6666);
  27. int size = sizeof(recvaddr);
  28. int iResult;
  29. char buff[128] ;
  30. float rnd=0;
  31. int sendint = 0;
  32.  
  33. //program
  34. iResult = bind(recvsocket, (SOCKADDR*)&recvaddr, sizeof(recvaddr));
  35. if (iResult == SOCKET_ERROR) {
  36. printf("coś się spierdoliło z bindowaniem");
  37.  
  38. }
  39. for (;;) {
  40.  
  41.  
  42. iResult = recvfrom(recvsocket, buff, sizeof(buff), 0, (SOCKADDR*)&recvaddr, &size);
  43.  
  44. if (iResult == SOCKET_ERROR) continue;
  45. else {
  46. buff[iResult] = 0;
  47. puts(buff);
  48. puts("odebrano wiadomość"); //odbiera
  49. //strcat(buff, "-odbrano");
  50.  
  51. sendint = strlen(buff);
  52. iResult = sendto(recvsocket, (char*)&sendint, sizeof(sendint), 0, (SOCKADDR*)&recvaddr, sizeof(recvaddr));
  53. if (iResult == SOCKET_ERROR) {
  54. printf("błąd wysyłki: %d", WSAGetLastError());
  55. _getch();
  56. return 1;
  57. }
  58. else puts("no coś wysłano");
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. //formalności
  68. shutdown(recvsocket, 2);
  69. closesocket(recvsocket);
  70. //shutdown(sendsocket, 2);
  71. //closesocket(sendsocket);
  72. WSACleanup();
  73. return 1;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement