Advertisement
Guest User

SERVER

a guest
Mar 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  2. //
  3. #include <time.h>
  4. #include <locale.h>
  5. #include "stdafx.h"
  6. #include <winsock2.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <cstdlib>
  10. #include <cstdio>
  11. #include <ctime>
  12. #pragma comment(lib,"WS2_32.lib")
  13. #define PORT 8888
  14. #define BUFFLEN 512
  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     char buf[BUFFLEN];
  19.     char buf2[BUFFLEN]= "TEST!";
  20.     SOCKET s;
  21.     int slen, recv_len;
  22.     WSADATA wsa;
  23.     struct sockaddr_in server, si_other;
  24.     slen = sizeof(si_other);
  25.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  26.     {
  27.         printf("Błąd wsadata KOD: %d", WSAGetLastError());
  28.         getch();
  29.     }
  30.  
  31.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)  // ew w miejsce zera dać IPPROTOUDP
  32.     {
  33.         printf("Błąd tworzenia gniazda KOD: %d", WSAGetLastError());
  34.         getch();
  35.     }
  36.  
  37.     server.sin_family = AF_INET;
  38.     server.sin_addr.s_addr = INADDR_ANY;
  39.     server.sin_port = htons(PORT);
  40.  
  41.     if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
  42.     {
  43.         printf("Błąd Bindowania KOD: %d", WSAGetLastError());
  44.         getch();
  45.     }
  46.     puts("Bindowanie zakończone");
  47.     int iResult = 0;
  48.     int ibuf=0;
  49.     int ran = 0;
  50.     while (true)
  51.     {
  52.         printf("oczekiwanie na dane: ");
  53.         fflush(stdout);
  54.         //memset ?
  55.         /*double liczba = rand() % 1401;
  56.         sprintf(buf2, "%f",liczba);*/
  57.         /*
  58.         int liczba = rand() % 3871;
  59.         sprintf(buf2, "%d", liczba);*/
  60.  
  61.  
  62.  
  63.     /*  if ((recv_len = recvfrom(s, buf, BUFFLEN, 0, (sockaddr *)&si_other, &slen)) == SOCKET_ERROR)
  64.             {
  65.             printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
  66.             getch();
  67.             exit(EXIT_FAILURE);
  68.             }
  69.         */
  70.        
  71.  
  72.         if ((recv_len = recvfrom(s, (char*)&ibuf, sizeof(ibuf), 0, (sockaddr *)&si_other, &slen)) == SOCKET_ERROR)
  73.         {
  74.             printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
  75.             getch();
  76.         }
  77.  
  78.  
  79.     /*  buf[recv_len] = '\0';
  80.         printf("\nOdebrano pakiet danych z %s : %d \n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));   
  81.         printf("\nDane %s\n", buf);*/
  82.        
  83.         printf("\nDane:   %d\n", ibuf);
  84.  
  85.     /*  iResult = sendto(s, buf2, strlen(buf2), 0, (SOCKADDR *)&si_other, slen);
  86.         if (iResult == SOCKET_ERROR) {
  87.             printf("Bład wysyłania: %d", WSAGetLastError());
  88.             _getch();
  89.             return 1;
  90.         }*/
  91.         ran = rand() % 65443 + 4;
  92.             iResult = sendto(s, (char*)&ran, sizeof(ran), 0, (SOCKADDR *)&si_other, slen);
  93.             if (iResult == SOCKET_ERROR) {
  94.                 printf("Bład wysyłania: %d", WSAGetLastError());
  95.                 _getch();
  96.                 return 1;
  97.             }
  98.  
  99.     } // koniec while
  100.    
  101.     closesocket(s);
  102.     WSACleanup();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement