Mihailo21

TCPKlientPostavka1

Nov 20th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2.  
  3. #define WIN32_LEAN_AND_MEAN
  4.  
  5. #include <windows.h>
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "conio.h"
  11.  
  12. #pragma comment (lib, "Ws2_32.lib")
  13. #pragma comment (lib, "Mswsock.lib")
  14. #pragma comment (lib, "AdvApi32.lib")
  15.  
  16. #define SERVER_IP_ADDRESS "127.0.0.1"
  17. #define SERVER_PORT 27016
  18. #define BUFFER_SIZE 256
  19.  
  20. // TCP client that use blocking sockets
  21. int main()
  22. {
  23.     // Socket used to communicate with server
  24.     SOCKET connectSocket = INVALID_SOCKET;
  25.  
  26.     // Variable used to store function return value
  27.     int iResult;
  28.  
  29.     // Buffer we will use to store message
  30.     char dataBuffer[BUFFER_SIZE];
  31.  
  32.     // WSADATA data structure that is to receive details of the Windows Sockets implementation
  33.     WSADATA wsaData;
  34.  
  35.     // Initialize windows sockets library for this process
  36.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  37.     {
  38.         printf("WSAStartup failed with error: %d\n", WSAGetLastError());
  39.         return 1;
  40.     }
  41.  
  42.     // create a socket
  43.     connectSocket = socket(AF_INET,
  44.         SOCK_STREAM,
  45.         IPPROTO_TCP);
  46.  
  47.     if (connectSocket == INVALID_SOCKET)
  48.     {
  49.         printf("socket failed with error: %ld\n", WSAGetLastError());
  50.         WSACleanup();
  51.         return 1;
  52.     }
  53.  
  54.     // Create and initialize address structure
  55.     sockaddr_in serverAddress;
  56.     serverAddress.sin_family = AF_INET;                             // IPv4 protocol
  57.     serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);   // ip address of server
  58.     serverAddress.sin_port = htons(SERVER_PORT);                    // server port
  59.  
  60.     // Connect to server specified in serverAddress and socket connectSocket
  61.     if (connect(connectSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
  62.     {
  63.         printf("Unable to connect to server.\n");
  64.         closesocket(connectSocket);
  65.         WSACleanup();
  66.         return 1;
  67.     }
  68.  
  69.     // Read string from user into outgoing buffer
  70.     printf("Enter message to send: ");
  71.     gets_s(dataBuffer, BUFFER_SIZE);
  72.  
  73.     // Send message to server using connected socket
  74.     iResult = send(connectSocket, dataBuffer, (int)strlen(dataBuffer), 0);
  75.  
  76.     // Check result of send function
  77.     if (iResult == SOCKET_ERROR)
  78.     {
  79.         printf("send failed with error: %d\n", WSAGetLastError());
  80.         closesocket(connectSocket);
  81.         WSACleanup();
  82.         return 1;
  83.     }
  84.  
  85.     printf("Message successfully sent. Total bytes: %ld\n", iResult);
  86.  
  87.     // Shutdown the connection since we're done
  88.     iResult = shutdown(connectSocket, SD_BOTH);
  89.  
  90.     // Check if connection is succesfully shut down.
  91.     if (iResult == SOCKET_ERROR)
  92.     {
  93.         printf("Shutdown failed with error: %d\n", WSAGetLastError());
  94.         closesocket(connectSocket);
  95.         WSACleanup();
  96.         return 1;
  97.     }
  98.  
  99.     // For demonstration purpose
  100.     printf("\nPress any key to exit: ");
  101.     _getch();
  102.  
  103.  
  104.     // Close connected socket
  105.     closesocket(connectSocket);
  106.  
  107.     // Deinitialize WSA library
  108.     WSACleanup();
  109.  
  110.     return 0;
  111. }
Add Comment
Please, Sign In to add comment