Mihailo21

PeteTCPClient

Nov 21st, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <windows.h>
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "conio.h"
  9.  
  10. #pragma comment (lib, "Ws2_32.lib")
  11. #pragma comment (lib, "Mswsock.lib")
  12. #pragma comment (lib, "AdvApi32.lib")
  13.  
  14. #pragma pack(1)
  15.  
  16. #define SERVER_IP_ADDRESS "127.0.0.1"
  17. #define SERVER_PORT 27016
  18. #define BUFFER_SIZE 256
  19.  
  20.  
  21. struct studentInfo{
  22.     char ime [15];
  23.     char prezime [20];
  24.     short poeni;
  25. };
  26.  
  27. // TCP client that use non-blocking sockets
  28. int main()
  29. {
  30.     // Socket used to communicate with server
  31.     SOCKET connectSocket = INVALID_SOCKET;
  32.  
  33.     // Variable used to store function return value
  34.     int iResult;
  35.  
  36.     // Buffer we will use to store message
  37.     char dataBuffer[BUFFER_SIZE];
  38.  
  39.     // WSADATA data structure that is to receive details of the Windows Sockets implementation
  40.      WSADATA wsaData;
  41.  
  42.     // Initialize windows sockets library for this process
  43.     if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  44.     {
  45.         printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  46.         return 1;
  47.     }
  48.  
  49.     // create a socket
  50.     connectSocket = socket(AF_INET,
  51.                            SOCK_STREAM,
  52.                            IPPROTO_TCP);
  53.  
  54.     if (connectSocket == INVALID_SOCKET)
  55.     {
  56.         printf("socket failed with error: %ld\n", WSAGetLastError());
  57.         WSACleanup();
  58.         return 1;
  59.     }
  60.  
  61.     // Create and initialize address structure
  62.     sockaddr_in serverAddress;
  63.     serverAddress.sin_family = AF_INET;                             // IPv4 protocol
  64.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);   // ip address of server
  65.     serverAddress.sin_port = htons(SERVER_PORT);                    // server port
  66.  
  67.     // Connect to server specified in serverAddress and socket connectSocket
  68.     iResult = connect(connectSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress));
  69.     if (iResult == SOCKET_ERROR)
  70.     {
  71.         printf("Unable to connect to server.\n");
  72.         closesocket(connectSocket);
  73.         WSACleanup();
  74.         return 1;
  75.     }
  76.        
  77.         //promenljiva tipa studentInfo cija ce se polja popunuti i cela struktira poslati u okviru jedne poruke
  78.         studentInfo student;  
  79.     short poeni;
  80.  
  81.     while(true)
  82.     {
  83.         // Unos potrebnih podataka koji ce se poslati serveru
  84.         printf("Unesite ime studenta: ");
  85.         gets_s(student.ime, 15);
  86.  
  87.         printf("Unesite prezime studenta: ");
  88.         gets_s(student.prezime, 20);
  89.  
  90.         printf("Unesite osvojene poene na testu: ");
  91.         scanf("%d", &poeni);
  92.         student.poeni=htons(poeni);  //obavezna funkcija htons() jer cemo slati podatak tipa short
  93.         getchar();    //pokupiti enter karakter iz bafera tastature
  94.  
  95.  
  96.         // Slanje pripremljene poruke zapisane unutar strukture studentInfo
  97.         //prosledjujemo adresu promenljive student u memoriji, jer se na toj adresi nalaze podaci koje saljemo
  98.         //kao i velicinu te strukture (jer je to duzina poruke u bajtima)
  99.         iResult = send( connectSocket, (char*) &student, (int)sizeof(studentInfo), 0 );
  100.  
  101.         // Check result of send function
  102.         if (iResult == SOCKET_ERROR)
  103.         {
  104.             printf("send failed with error: %d\n", WSAGetLastError());
  105.             closesocket(connectSocket);
  106.             WSACleanup();
  107.             return 1;
  108.         }
  109.  
  110.         printf("Message successfully sent. Total bytes: %ld\n", iResult);
  111.  
  112.         printf("\nPress 'x' to exit or any other key to continue: ");
  113.         if(getch() == 'x')
  114.             break;
  115.     }
  116.  
  117.     // Shutdown the connection since we're done
  118.     iResult = shutdown(connectSocket, SD_BOTH);
  119.  
  120.     // Check if connection is succesfully shut down.
  121.     if (iResult == SOCKET_ERROR)
  122.     {
  123.         printf("Shutdown failed with error: %d\n", WSAGetLastError());
  124.         closesocket(connectSocket);
  125.         WSACleanup();
  126.         return 1;
  127.     }
  128.  
  129.     Sleep(1000);
  130.  
  131.     // Close connected socket
  132.     closesocket(connectSocket);
  133.  
  134.     // Deinitialize WSA library
  135.     WSACleanup();
  136.  
  137.     return 0;
  138. }
  139.  
Add Comment
Please, Sign In to add comment