Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication20.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "pch.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <windows.h>
- using namespace std;
- #pragma comment(lib, "Ws2_32.lib")
- #pragma warning(disable:4996)
- int main()
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //zelena farba
- WSADATA wsaData; //struktura wsadata na pracu s winsock
- int iResult;
- int o;
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); //zakladna inicilializacia winsocku
- if (iResult != 0)
- {
- printf("WSAStartup failed : %d\n", iResult);
- return 1;
- }
- struct addrinfo *result = NULL, *ptr = NULL; //praca s adresami
- struct addrinfo hints;
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP; //protokol TCP
- iResult = getaddrinfo("147.175.115.34", "777", &hints, &result);
- if (iResult != 0) //kontrola, ci nenastala chyba
- {
- printf("getaddrinfo failed : %d\n", iResult);
- WSACleanup();
- return 1;
- }
- else
- printf("getaddrinfo didn’t fail…\n");
- //vytvorenie socketu
- SOCKET ConnectSocket = INVALID_SOCKET;
- ptr = result;
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); // pokus o vytvorenie socketu
- if (ConnectSocket == INVALID_SOCKET) //kontrola
- {
- printf("Error at socket() : %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 1;
- }
- else
- printf("Error at socket DID NOT occur…\n");
- //pokus o pripojenie sa na server
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) //kontrola, ci nenastala chyba
- printf("Not connected to server…\n");
- else
- printf("Connected to server!\n");
- if (iResult == SOCKET_ERROR) //osetrenie chyboveho stavu
- {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- WSACleanup();
- return 1;
- }
- Sleep(250);
- SetConsoleOutputCP(CP_UTF8);
- const char* sprava = "Adam Novotn" "\xc3\xbd \n";
- wprintf(L"%S", sprava);
- while (1)
- {
- //posielanie
- char sendbuf[4096];
- int abc = 4096;
- int o;
- printf("Zadaj retazec: ");
- fgets(sendbuf, abc, stdin);
- iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR)
- {
- printf("send failed : %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- printf("Bytes Sent : %ld\n", iResult); //vypisanie poctu odoslanych dat
- //prijimanie
- #define DEFAULT_BUFLEN 4096 //velkost prijimacieho buffera
- int recvbuflen = DEFAULT_BUFLEN;
- int i;
- char recvbuf[DEFAULT_BUFLEN];
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); //funkcia na príjimanie
- if (iResult > 0)
- printf("Bytes received : %d\n", iResult); //prisli validne data, vypis poctu
- else if (iResult == 0)
- printf("Connection closed\n"); //v tomto pripade server ukoncil komunikaciu
- else
- printf("recv failed with error : %d\n", WSAGetLastError()); //ina chyba
- for (i = 0; i <= strlen(recvbuf); i++)
- {
- if (recvbuf[i] == '\n')
- {
- recvbuf[i] = '\0';
- }
- }
- }
- //zavretie socketu
- closesocket(ConnectSocket);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement