Advertisement
Guest User

...

a guest
Dec 19th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.39 KB | None | 0 0
  1.  
  2. השיחה נפתחה. הודעה אחת שלא נקראה.
  3.  
  4. דלג לתוכן
  5. שימוש ב-Gmail עם קוראי מסך
  6. aviv
  7. חיפוש
  8.  
  9.  
  10.  
  11. קח אותי ל-Inbox
  12. לחץ כאן כדי לאפשר הודעות בשולחן העבודה עבור Gmail.   מידע נוסף  הסתר
  13. Gmail
  14. כתוב הודעה
  15. תוויות
  16. דואר נכנס ‏(17)
  17. מסומן בכוכב
  18. חשוב
  19. דואר יוצא
  20. טיוטות
  21. מעגלים
  22. אישי
  23. נסיעות
  24. עוד
  25. Hangouts
  26.  
  27.  
  28.  
  29.  
  30.   עוד
  31. 1 מתוך 68  
  32.  
  33. הדפס הכל בחלון חדש
  34. Fwd: תקשורת מחשבים מאיה
  35. דואר נכנס
  36. x
  37.  
  38. chen hazbani‏
  39. קבצים מצורפים16:08 (לפני 0 דקות)
  40.  
  41. אני
  42. ---------- Forwarded message ----------
  43. From: Lee <embon51@gmail.com>
  44. Date: 2015-12-15 12:24 GMT+02:00
  45. Subject: Fwd: תקשורת מחשבים מאיה
  46. To: "chenhazbani@gmail.com" <chenhazbani@gmail.com>
  47.  
  48.  
  49.  
  50.  
  51. נשלח מה-iPhone שלי
  52.  
  53. תחילת ההודעה שהועברה‏:
  54.  
  55. מאת: Maya Dvir <dvmaya@gmail.com>
  56. תאריך: 3 בדצמבר 2015 בשעה 13:15:38 GMT+2
  57. אל: Lee Embon <embon51@gmail.com>
  58. נושא: תקשורת מחשבים מאיה
  59.  
  60. אני יעשה עם case
  61. ואת עם choice
  62. 2 קבצים מצורפים
  63.  
  64. תצוגה מקדימה של הקובץ המצורף client.cpp
  65.  
  66. C++
  67. client.cpp
  68. תצוגה מקדימה של הקובץ המצורף server.cpp
  69.  
  70. C++
  71. server.cpp
  72.    
  73. לחץ כאן כדי השב או העבר
  74. 0.38 GB‏ (%2) מתוך 15 GB נמצאים בשימוש
  75. נהל
  76. תנאים - פרטיות
  77. פעילות אחרונה בחשבון: בעוד דקה
  78. פרטים
  79. chen hazbani
  80. הוסף למעגלים
  81.  
  82. הצג פרטים
  83.  
  84.  
  85. #include <iostream>
  86.  
  87. using namespace std;
  88.  
  89. #include <winsock2.h>
  90.  
  91. #include <string.h>
  92.  
  93. #include <time.h>
  94.  
  95. #include <stdio.h>
  96.  
  97. #include <stdlib.h>
  98.  
  99.  
  100.  
  101. #define TIME_PORT   27015
  102.  
  103.  
  104.  
  105. void main()
  106.  
  107. {
  108.  
  109.     // Initialize Winsock (Windows Sockets).
  110.  
  111.     // Create a WSADATA object called wsaData.
  112.  
  113.     // The WSADATA structure contains information about the Windows
  114.  
  115.     // Sockets implementation.
  116.  
  117.     WSAData wsaData;
  118.  
  119.  
  120.  
  121.     // Call WSAStartup and return its value as an integer and check for errors.
  122.  
  123.     // The WSAStartup function initiates the use of WS2_32.DLL by a process.
  124.  
  125.     // First parameter is the version number 2.2.
  126.  
  127.     // The WSACleanup function destructs the use of WS2_32.DLL by a process.
  128.  
  129.     if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
  130.  
  131.     {
  132.  
  133.         cout << "Time Server: Error at WSAStartup()\n";
  134.  
  135.     }
  136.  
  137.  
  138.  
  139.     // Server side:
  140.  
  141.     // Create and bind a socket to an internet address.
  142.  
  143.     // After initialization, a SOCKET object is ready to be instantiated.
  144.  
  145.     // Create a SOCKET object called m_socket.
  146.  
  147.     // For this application:    use the Internet address family (AF_INET),
  148.  
  149.     //                          datagram sockets (SOCK_DGRAM),
  150.  
  151.     //                          and the UDP/IP protocol (IPPROTO_UDP).
  152.  
  153.     SOCKET m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  154.  
  155.  
  156.  
  157.     // Check for errors to ensure that the socket is a valid socket.
  158.  
  159.     // Error detection is a key part of successful networking code.
  160.  
  161.     // If the socket call fails, it returns INVALID_SOCKET.
  162.  
  163.     // The "if" statement in the previous code is used to catch any errors that
  164.  
  165.     // may have occurred while creating the socket. WSAGetLastError returns an
  166.  
  167.     // error number associated with the last error that occurred.
  168.  
  169.     if (INVALID_SOCKET == m_socket)
  170.  
  171.     {
  172.  
  173.         cout << "Time Server: Error at socket(): " << WSAGetLastError() << endl;
  174.  
  175.         WSACleanup();
  176.  
  177.         return;
  178.  
  179.     }
  180.  
  181. // For a server to communicate on a network, it must first bind the socket to
  182.  
  183.     // a network address.
  184.  
  185.     // Need to assemble the required data for connection in sockaddr structure.
  186.  
  187.     // Create a sockaddr_in object called serverService.
  188.  
  189.     sockaddr_in serverService;
  190.  
  191.     // Address family (must be AF_INET - Internet address family).
  192.  
  193.     serverService.sin_family = AF_INET;
  194.  
  195.     // IP address. The sin_addr is a union (s_addr is a unsigdned long (4 bytes) data type).
  196.  
  197.     // INADDR_ANY means to listen on all interfaces.
  198.  
  199.     // inet_addr (Internet address) is used to convert a string (char *) into unsigned int.
  200.  
  201.     // inet_ntoa (Internet address) is the reverse function (converts unsigned int to char *)
  202.  
  203.     // The IP address 127.0.0.1 is the host itself, it's actually a loop-back.
  204.  
  205.     serverService.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");
  206.  
  207.     // IP Port. The htons (host to network - short) function converts an
  208.  
  209.     // unsigned short from host to TCP/IP network byte order (which is big-endian).
  210.  
  211.     serverService.sin_port = htons(TIME_PORT);
  212.  
  213.  
  214.  
  215.     // Bind the socket for client's requests.
  216.  
  217.     // The bind function establishes a connection to a specified socket.
  218.  
  219.     // The function uses the socket handler, the sockaddr structure (which
  220.  
  221.     // defines properties of the desired connection) and the length of the
  222.  
  223.     // sockaddr structure (in bytes).
  224.  
  225.     if (SOCKET_ERROR == bind(m_socket, (SOCKADDR *) &serverService, sizeof(serverService)))
  226.  
  227.     {
  228.  
  229.         cout << "Time Server: Error at bind() : "<<WSAGetLastError()<<endl;
  230.  
  231.         closesocket(m_socket);
  232.  
  233.         WSACleanup();
  234.  
  235.         return;
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.     // Waits for incoming requests from clients.
  242.  
  243.  
  244.  
  245.     // Send and receive data.
  246.  
  247.     sockaddr client_addr;
  248.  
  249.     int client_addr_len = sizeof(client_addr);
  250.  
  251.     int bytesSent = 0;
  252.  
  253.     int bytesRecv = 0;
  254.  
  255.     char sendBuff[255];
  256.  
  257.     char recvBuff[255];
  258.  
  259.  
  260.  
  261.     // Get client's requests and answer them.
  262.  
  263.     // The recvfrom function receives a datagram and stores the source address.
  264.  
  265.     // The buffer for data to be received and its available size are
  266.  
  267.     // returned by recvfrom. The fourth argument is an idicator
  268.  
  269.     // specifying the way in which the call is made (0 for default).
  270.  
  271.     // The two last arguments are optional and will hold the details of the client for further communication.
  272.  
  273.     // NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr))
  274.  
  275.     cout << "Time Server : Wait for clients' requests.\n";
  276.  
  277.  
  278.  
  279.     while (true)
  280.  
  281.     {
  282.  
  283.         bytesRecv = recvfrom(m_socket, recvBuff, 255, 0, &client_addr, &client_addr_len);
  284.  
  285.         if (SOCKET_ERROR == bytesRecv)
  286.  
  287.         {
  288.  
  289.             cout << "Time Server: Error at recvfrom(): " <<WSAGetLastError()<<endl;
  290.  
  291.             closesocket(m_socket);
  292.  
  293.             WSACleanup();
  294.  
  295.             return;
  296.  
  297.         }
  298.  
  299.  
  300.  
  301.         recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  302.  
  303.         cout << "Time Server Received: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  304.  
  305.  
  306.  
  307.  
  308.  
  309.         // Answer client's request by the current time.
  310.  
  311.        
  312.  
  313.         // Get the current time
  314.  
  315.         if (strcmp(recvBuff, " GetTime") == 0)
  316.  
  317.         {
  318.  
  319.             time_t timer;
  320.  
  321.             time(&timer);
  322.  
  323.             // Parse the current time to printable string.
  324.  
  325.         strcpy(sendBuff, ctime(&timer));
  326.  
  327.         //sendBuff[strlen(sendBuff)-1] = '\0'; //to remove the new-line from the created string
  328.  
  329.         }
  330.  
  331.         else if (strcmp(recvBuff, " GetTimeWithoutDate") == 0)
  332.  
  333.         {
  334.  
  335.             time_t timer2;
  336.  
  337.             struct tm * time2;
  338.  
  339.  
  340.  
  341.             time(&timer2);
  342.  
  343.             time2 = localtime(&timer2);
  344.  
  345.  
  346.  
  347.             strftime(sendBuff, 255, "The time is : %X ", time2);//%I:%M%p//hh:mm:ss
  348.  
  349.             sendBuff[strlen(sendBuff)] = '\0';
  350.  
  351.         }
  352.  
  353.         else if (strcmp(recvBuff, " GetTimeSinceEpoch") == 0)
  354.  
  355.         {
  356.  
  357.             time_t secondsSince1970;
  358.  
  359.             secondsSince1970 = time(0);
  360.  
  361.             _itoa(secondsSince1970, sendBuff, 10);
  362.  
  363.         }
  364.  
  365.         else if (strcmp(recvBuff, " GetClientToServerDelayEstimation") == 0)
  366.  
  367.         {
  368.  
  369.             _itoa(GetTickCount(), sendBuff, 10);
  370.  
  371.         }
  372.  
  373.         else if (strcmp(recvBuff, " MeasureRTT") == 0)
  374.  
  375.         {
  376.  
  377.             strcpy_s(sendBuff, " ");
  378.  
  379.         }
  380.  
  381.         else if (strcmp(recvBuff, " GetTimeWithoutSec") == 0)
  382.  
  383.         {
  384.  
  385.             time_t timer2;
  386.  
  387.             struct tm * time2;
  388.  
  389.  
  390.  
  391.             time(&timer2);
  392.  
  393.             time2 = localtime(&timer2);
  394.  
  395.  
  396.  
  397.             strftime(sendBuff, 255, "The time is : %H:%M%p ", time2);//hh:mm
  398.  
  399.             sendBuff[strlen(sendBuff)] = '\0';
  400.  
  401.         }
  402.  
  403.         else if (strcmp(recvBuff, " GetYear") == 0)
  404.  
  405.         {
  406.  
  407.             time_t timer;
  408.  
  409.             time(&timer);
  410.  
  411.             struct tm * timeNoDate;
  412.  
  413.             timeNoDate = localtime(&timer);
  414.  
  415.             strftime(sendBuff, 255, "The Year is: %Y \n ", timeNoDate);
  416.  
  417.         }
  418.  
  419.         else if (strcmp(recvBuff, " GetMonthAndDay") == 0)
  420.  
  421.         {
  422.  
  423.             time_t timer;
  424.  
  425.             time(&timer);
  426.  
  427.             struct tm* timeNoDate;
  428.  
  429.             timeNoDate = localtime(&timer);
  430.  
  431.             strftime(sendBuff, 255, "The Month is: %B , and The Day is: %d \n", timeNoDate);
  432.  
  433.         }
  434.  
  435.         else if (strcmp(recvBuff, " GetSecondsSinceBeginingOfMonth") == 0)
  436.  
  437.         {
  438.  
  439.             time_t now = time(NULL);
  440.  
  441.             struct tm* temp = localtime(&now);
  442.  
  443.             tm thisMonthStart = { 0 };
  444.  
  445.  
  446.  
  447.             thisMonthStart.tm_year = temp->tm_year;
  448.  
  449.             thisMonthStart.tm_mday = 1;
  450.  
  451.  
  452.  
  453.             double elapsed;
  454.  
  455.             elapsed = difftime(now, mktime(&thisMonthStart));
  456.  
  457.             _itoa(elapsed, sendBuff, 10);
  458.  
  459.         }
  460.  
  461.  
  462.  
  463.         sendBuff[strlen(sendBuff) - 1] = '\0'; // To remove the new-line from the created string
  464.  
  465.  
  466.  
  467.         // Sends the answer to the client, using the client address gathered
  468.  
  469.         //by recvfrom.
  470.  
  471.         bytesSent = sendto(m_socket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&client_addr, client_addr_len);
  472.  
  473.  
  474.  
  475.         if (SOCKET_ERROR == bytesSent)
  476.  
  477.         {
  478.  
  479.             cout << "Time Server: Error at sendto(): " <<WSAGetLastError()<<endl;
  480.  
  481.             closesocket(m_socket);
  482.  
  483.             WSACleanup();
  484.  
  485.             return;
  486.  
  487.         }
  488.  
  489.         cout<<"Time Server Sent: "<<bytesSent<<"\\"<<strlen(sendBuff)<<" bytes of "<<sendBuff<<" message.\n";
  490.  
  491.     }
  492.  
  493.  
  494.  
  495.     // Closing connections and Winsock.
  496.  
  497.     cout << "Time Server: Closing Connection.\n";
  498.  
  499.     closesocket(m_socket);
  500.  
  501.     WSACleanup();
  502.  
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement