Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- השיחה נפתחה. הודעה אחת שלא נקראה.
- דלג לתוכן
- שימוש ב-Gmail עם קוראי מסך
- aviv
- חיפוש
- קח אותי ל-Inbox
- לחץ כאן כדי לאפשר הודעות בשולחן העבודה עבור Gmail. מידע נוסף הסתר
- Gmail
- כתוב הודעה
- תוויות
- דואר נכנס (17)
- מסומן בכוכב
- חשוב
- דואר יוצא
- טיוטות
- מעגלים
- אישי
- נסיעות
- עוד
- Hangouts
- עוד
- 1 מתוך 68
- הדפס הכל בחלון חדש
- Fwd: תקשורת מחשבים מאיה
- דואר נכנס
- x
- chen hazbani
- קבצים מצורפים16:08 (לפני 0 דקות)
- אני
- ---------- Forwarded message ----------
- From: Lee <embon51@gmail.com>
- Date: 2015-12-15 12:24 GMT+02:00
- Subject: Fwd: תקשורת מחשבים מאיה
- To: "chenhazbani@gmail.com" <chenhazbani@gmail.com>
- נשלח מה-iPhone שלי
- תחילת ההודעה שהועברה:
- מאת: Maya Dvir <dvmaya@gmail.com>
- תאריך: 3 בדצמבר 2015 בשעה 13:15:38 GMT+2
- אל: Lee Embon <embon51@gmail.com>
- נושא: תקשורת מחשבים מאיה
- אני יעשה עם case
- ואת עם choice
- 2 קבצים מצורפים
- תצוגה מקדימה של הקובץ המצורף client.cpp
- C++
- client.cpp
- תצוגה מקדימה של הקובץ המצורף server.cpp
- C++
- server.cpp
- לחץ כאן כדי השב או העבר
- 0.38 GB (%2) מתוך 15 GB נמצאים בשימוש
- נהל
- תנאים - פרטיות
- פעילות אחרונה בחשבון: בעוד דקה
- פרטים
- chen hazbani
- הוסף למעגלים
- הצג פרטים
- #include <iostream>
- using namespace std;
- #include <winsock2.h>
- #include <string.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define TIME_PORT 27015
- void main()
- {
- // Initialize Winsock (Windows Sockets).
- // Create a WSADATA object called wsaData.
- // The WSADATA structure contains information about the Windows
- // Sockets implementation.
- WSAData wsaData;
- // Call WSAStartup and return its value as an integer and check for errors.
- // The WSAStartup function initiates the use of WS2_32.DLL by a process.
- // First parameter is the version number 2.2.
- // The WSACleanup function destructs the use of WS2_32.DLL by a process.
- if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
- {
- cout << "Time Server: Error at WSAStartup()\n";
- }
- // Server side:
- // Create and bind a socket to an internet address.
- // After initialization, a SOCKET object is ready to be instantiated.
- // Create a SOCKET object called m_socket.
- // For this application: use the Internet address family (AF_INET),
- // datagram sockets (SOCK_DGRAM),
- // and the UDP/IP protocol (IPPROTO_UDP).
- SOCKET m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- // Check for errors to ensure that the socket is a valid socket.
- // Error detection is a key part of successful networking code.
- // If the socket call fails, it returns INVALID_SOCKET.
- // The "if" statement in the previous code is used to catch any errors that
- // may have occurred while creating the socket. WSAGetLastError returns an
- // error number associated with the last error that occurred.
- if (INVALID_SOCKET == m_socket)
- {
- cout << "Time Server: Error at socket(): " << WSAGetLastError() << endl;
- WSACleanup();
- return;
- }
- // For a server to communicate on a network, it must first bind the socket to
- // a network address.
- // Need to assemble the required data for connection in sockaddr structure.
- // Create a sockaddr_in object called serverService.
- sockaddr_in serverService;
- // Address family (must be AF_INET - Internet address family).
- serverService.sin_family = AF_INET;
- // IP address. The sin_addr is a union (s_addr is a unsigdned long (4 bytes) data type).
- // INADDR_ANY means to listen on all interfaces.
- // inet_addr (Internet address) is used to convert a string (char *) into unsigned int.
- // inet_ntoa (Internet address) is the reverse function (converts unsigned int to char *)
- // The IP address 127.0.0.1 is the host itself, it's actually a loop-back.
- serverService.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");
- // IP Port. The htons (host to network - short) function converts an
- // unsigned short from host to TCP/IP network byte order (which is big-endian).
- serverService.sin_port = htons(TIME_PORT);
- // Bind the socket for client's requests.
- // The bind function establishes a connection to a specified socket.
- // The function uses the socket handler, the sockaddr structure (which
- // defines properties of the desired connection) and the length of the
- // sockaddr structure (in bytes).
- if (SOCKET_ERROR == bind(m_socket, (SOCKADDR *) &serverService, sizeof(serverService)))
- {
- cout << "Time Server: Error at bind() : "<<WSAGetLastError()<<endl;
- closesocket(m_socket);
- WSACleanup();
- return;
- }
- // Waits for incoming requests from clients.
- // Send and receive data.
- sockaddr client_addr;
- int client_addr_len = sizeof(client_addr);
- int bytesSent = 0;
- int bytesRecv = 0;
- char sendBuff[255];
- char recvBuff[255];
- // Get client's requests and answer them.
- // The recvfrom function receives a datagram and stores the source address.
- // The buffer for data to be received and its available size are
- // returned by recvfrom. The fourth argument is an idicator
- // specifying the way in which the call is made (0 for default).
- // The two last arguments are optional and will hold the details of the client for further communication.
- // NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr))
- cout << "Time Server : Wait for clients' requests.\n";
- while (true)
- {
- bytesRecv = recvfrom(m_socket, recvBuff, 255, 0, &client_addr, &client_addr_len);
- if (SOCKET_ERROR == bytesRecv)
- {
- cout << "Time Server: Error at recvfrom(): " <<WSAGetLastError()<<endl;
- closesocket(m_socket);
- WSACleanup();
- return;
- }
- recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
- cout << "Time Server Received: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
- // Answer client's request by the current time.
- // Get the current time
- if (strcmp(recvBuff, " GetTime") == 0)
- {
- time_t timer;
- time(&timer);
- // Parse the current time to printable string.
- strcpy(sendBuff, ctime(&timer));
- //sendBuff[strlen(sendBuff)-1] = '\0'; //to remove the new-line from the created string
- }
- else if (strcmp(recvBuff, " GetTimeWithoutDate") == 0)
- {
- time_t timer2;
- struct tm * time2;
- time(&timer2);
- time2 = localtime(&timer2);
- strftime(sendBuff, 255, "The time is : %X ", time2);//%I:%M%p//hh:mm:ss
- sendBuff[strlen(sendBuff)] = '\0';
- }
- else if (strcmp(recvBuff, " GetTimeSinceEpoch") == 0)
- {
- time_t secondsSince1970;
- secondsSince1970 = time(0);
- _itoa(secondsSince1970, sendBuff, 10);
- }
- else if (strcmp(recvBuff, " GetClientToServerDelayEstimation") == 0)
- {
- _itoa(GetTickCount(), sendBuff, 10);
- }
- else if (strcmp(recvBuff, " MeasureRTT") == 0)
- {
- strcpy_s(sendBuff, " ");
- }
- else if (strcmp(recvBuff, " GetTimeWithoutSec") == 0)
- {
- time_t timer2;
- struct tm * time2;
- time(&timer2);
- time2 = localtime(&timer2);
- strftime(sendBuff, 255, "The time is : %H:%M%p ", time2);//hh:mm
- sendBuff[strlen(sendBuff)] = '\0';
- }
- else if (strcmp(recvBuff, " GetYear") == 0)
- {
- time_t timer;
- time(&timer);
- struct tm * timeNoDate;
- timeNoDate = localtime(&timer);
- strftime(sendBuff, 255, "The Year is: %Y \n ", timeNoDate);
- }
- else if (strcmp(recvBuff, " GetMonthAndDay") == 0)
- {
- time_t timer;
- time(&timer);
- struct tm* timeNoDate;
- timeNoDate = localtime(&timer);
- strftime(sendBuff, 255, "The Month is: %B , and The Day is: %d \n", timeNoDate);
- }
- else if (strcmp(recvBuff, " GetSecondsSinceBeginingOfMonth") == 0)
- {
- time_t now = time(NULL);
- struct tm* temp = localtime(&now);
- tm thisMonthStart = { 0 };
- thisMonthStart.tm_year = temp->tm_year;
- thisMonthStart.tm_mday = 1;
- double elapsed;
- elapsed = difftime(now, mktime(&thisMonthStart));
- _itoa(elapsed, sendBuff, 10);
- }
- sendBuff[strlen(sendBuff) - 1] = '\0'; // To remove the new-line from the created string
- // Sends the answer to the client, using the client address gathered
- //by recvfrom.
- bytesSent = sendto(m_socket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&client_addr, client_addr_len);
- if (SOCKET_ERROR == bytesSent)
- {
- cout << "Time Server: Error at sendto(): " <<WSAGetLastError()<<endl;
- closesocket(m_socket);
- WSACleanup();
- return;
- }
- cout<<"Time Server Sent: "<<bytesSent<<"\\"<<strlen(sendBuff)<<" bytes of "<<sendBuff<<" message.\n";
- }
- // Closing connections and Winsock.
- cout << "Time Server: Closing Connection.\n";
- closesocket(m_socket);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement