Advertisement
Guest User

CLIENT

a guest
Mar 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. // ConsoleApplication2_Client.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. // ConsoleApplication2.cpp : Defines the entry point for the console application.
  5. //
  6. #include <exception>
  7. #include <locale>
  8. #include "stdafx.h"
  9. #include <winsock2.h>
  10. #include <conio.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <ctime>
  14. #pragma comment(lib,"WS2_32.lib")
  15. #define SERVER "127.0.0.1"
  16. #define PORT 8888
  17. #define BUFFLEN 512
  18.  
  19. class SockEx
  20. {
  21.     SockEx()
  22.     {
  23.         printf("Exception Patronus Sock Ex");
  24.  
  25.     }
  26.  
  27. };
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33.  
  34.  
  35.     int iResult = 0;
  36.     char buf[BUFFLEN], message[BUFFLEN];
  37.     SOCKET s; // zamiast socket
  38.     int slen, recv_len;
  39.     WSADATA wsa;
  40.     struct sockaddr_in si_other = { 0 };
  41.     slen = sizeof(si_other);
  42.     if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  43.     {
  44.         printf("Błąd wsadata KOD: %d", WSAGetLastError());
  45.         getch();
  46.     }
  47.  
  48.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)  // ew w miejsce zera dać IPPROTOUDP
  49.     {
  50.         printf("Błąd tworzenia gniazda KOD: %d", WSAGetLastError());
  51.         getch();
  52.     }
  53.     //  memset((char *)&si_other, 0, sizeof(si_other));
  54.     si_other.sin_family = AF_INET;
  55.     si_other.sin_addr.s_addr = inet_addr(SERVER);
  56.     si_other.sin_port = htons(PORT);
  57.     /*
  58.         if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
  59.         {
  60.             printf("Błąd Bindowania KOD: %d", WSAGetLastError());
  61.             getch();
  62.         }
  63.         */
  64.         //  puts("Bindowanie zakończone");
  65.  
  66.     while (true)
  67.     {
  68.  
  69.         fflush(stdout);
  70.         //memset ?
  71.         printf("\nWprowadź wiadomość: ");
  72.         int liczba;
  73.     //  scanf("%d", &liczba);
  74.         //sprintf(message, "%d", liczba);
  75.         gets_s(message, 511);
  76.         bool popr = false;
  77.         for (int i = 0; i < strlen(message); i++)
  78.         {
  79.             if (isdigit(message[i]) == false)
  80.             {
  81.                 printf("JESTEŚ DEBILEM");
  82.                 popr = true;
  83.                 getch();
  84.                 break;
  85.             }
  86.            
  87.         }
  88.         if (popr) continue;
  89.         liczba = atoi(message);
  90.         printf("\n<TEST> WPROWADZIŁEŚ: %s", message);
  91.  
  92.         //scanf("%s", &message);
  93.  
  94.         /*
  95.         if ((recv_len = recvfrom(s, buf, BUFFLEN, 0, (sockaddr *)&si_other, &slen)) == SOCKET_ERROR)
  96.         {
  97.             printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
  98.             getch();
  99.             exit(EXIT_FAILURE);
  100.         }
  101.  
  102.  
  103.         printf("Odebrano pakiet danych z %s : %d \n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
  104.         printf("Dane %s\n", buf);
  105.         */
  106.  
  107.         /*   if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
  108.             {
  109.                 printf("Błąd Sendto <while> KOD: %d", WSAGetLastError());
  110.                 getch();
  111.                 exit(EXIT_FAILURE);
  112.             }*/
  113.  
  114.             /*  iResult = sendto(s, message, strlen(message), 0, (SOCKADDR *)&si_other, slen);
  115.                 if (iResult == SOCKET_ERROR) {
  116.                     printf("Bład wysyłania: %d", WSAGetLastError());
  117.                     _getch();
  118.                     return 1;
  119.                 }*/
  120.  
  121.                 ////////////////////////////////////////////////////////////////////////////////////////
  122.         int licz = 563;
  123.         int ibuf = 0;
  124.     try
  125.     {
  126.         iResult = sendto(s, (char*)&liczba, sizeof(liczba), 0, (SOCKADDR *)&si_other, slen);
  127.         if (iResult == SOCKET_ERROR)
  128.         {
  129.             /*printf("Bład wysyłania: %d", WSAGetLastError());
  130.             _getch();
  131.             return 1;*/
  132.  
  133.         }
  134.     }
  135.     catch (int eror)
  136.     {
  137.         printf("Błąd SENDTO <while> TO JEST CATCH  KOD: %d", WSAGetLastError());
  138.         getch();
  139.  
  140.     }
  141.    
  142.  
  143.  
  144.     ////////////////////////////////////////////////////////////////////////////////////////////////
  145.  
  146.        
  147.         /*  if ((recv_len = recvfrom(s, buf, BUFFLEN, 0, (SOCKADDR *)&si_other, &slen)) == SOCKET_ERROR)
  148.             {
  149.                 printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
  150.                 getch();
  151.                 exit(EXIT_FAILURE);
  152.             }*/
  153.         /////////////////////////////////////////////////////////////////////////////////////////////////////////
  154.             try {
  155.                 if ((recv_len = recvfrom(s, (char*)&ibuf, sizeof(ibuf), 0, (SOCKADDR *)&si_other, &slen)) == SOCKET_ERROR)
  156.                 {
  157.                     //printf("Błąd recv_len <while> KOD: %d", WSAGetLastError());
  158.                     throw WSAGetLastError();
  159.                 //  getch();
  160.                 //  exit(EXIT_FAILURE);
  161.                 }
  162.             }
  163.             catch (int err)
  164.             {
  165.                 printf("Błąd recv_len <while> TO JEST CATCH  KOD: %d", WSAGetLastError());
  166.                 getch();
  167.             }
  168.  
  169.             ///////////////////////////////////////////////////////////////////////////////////////////////////
  170.  
  171.  
  172.         //  buf[recv_len] = '\0';
  173.  
  174.             printf("Otrzymano: %d", ibuf);
  175.  
  176.         //  printf("\nDane: [%s] IP: %s PORT: %d", buf, inet_ntoa(si_other.sin_addr), si_other.sin_port);
  177.          
  178.             /*  if (atoi(buf) <1401 || atoi(buf) < 1)
  179.                 {
  180.  
  181.  
  182.                 }
  183.                 */
  184.             _getch();
  185.         }
  186.         closesocket(s);
  187.         WSACleanup();
  188.  
  189.         return 0;
  190.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement