From 513229ed3a01c8d3a48b6462e94b769d35c0655f Mon Sep 17 00:00:00 2001 From: BrynjarSt <31983827+BrynjarSt@users.noreply.github.com> Date: Thu, 19 Oct 2017 10:32:27 +0200 Subject: [PATCH 1/5] Create main --- main | 1 + 1 file changed, 1 insertion(+) create mode 100644 main diff --git a/main b/main new file mode 100644 index 00000000..a2c18d70 --- /dev/null +++ b/main @@ -0,0 +1 @@ +//Hello Fuckers From 8f21f61c91bbc4059e3168768fd0b8cf9a6060ea Mon Sep 17 00:00:00 2001 From: BrynjarSt <31983827+BrynjarSt@users.noreply.github.com> Date: Thu, 19 Oct 2017 10:36:47 +0200 Subject: [PATCH 2/5] Update main --- main | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main b/main index a2c18d70..75379bb9 100644 --- a/main +++ b/main @@ -1 +1 @@ -//Hello Fuckers +//Hello From 8f0c1d7385829bd3ab36563079a7695605d282d6 Mon Sep 17 00:00:00 2001 From: BrynjarSt <31983827+BrynjarSt@users.noreply.github.com> Date: Thu, 19 Oct 2017 10:39:15 +0200 Subject: [PATCH 3/5] Delete main --- main | 1 - 1 file changed, 1 deletion(-) delete mode 100644 main diff --git a/main b/main deleted file mode 100644 index 75379bb9..00000000 --- a/main +++ /dev/null @@ -1 +0,0 @@ -//Hello From a2d3cc32cb0487b33da2a603e2b80e5b7af0ed86 Mon Sep 17 00:00:00 2001 From: Andreas Junker Date: Thu, 19 Oct 2017 10:57:48 +0200 Subject: [PATCH 4/5] I changed thisi --- main.ccp | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 main.ccp diff --git a/main.ccp b/main.ccp new file mode 100644 index 00000000..e69de29b From b602c5d0ce8fe77c13fc9a507ed191be6bfb3dd2 Mon Sep 17 00:00:00 2001 From: Andreas Junker Date: Thu, 19 Oct 2017 11:08:18 +0200 Subject: [PATCH 5/5] I change this --- CLIENT.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ CLIENT.h | 15 +++++++++ SERVER.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ SERVER.h | 21 +++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 CLIENT.cpp create mode 100644 CLIENT.h create mode 100644 SERVER.cpp create mode 100644 SERVER.h diff --git a/CLIENT.cpp b/CLIENT.cpp new file mode 100644 index 00000000..09a18847 --- /dev/null +++ b/CLIENT.cpp @@ -0,0 +1,68 @@ +//TCP CLIENT source file + +#include "CLIENT.h" + +using namespace std; + +void main() +{ + //Locals + long SUCCESSFUL; + WSAData WinSockData; + + WORD DLLVersion; + DLLVersion = MAKEWORD(2, 1); + SUCCESSFUL = WSAStartup(DLLVersion, &WinSockData); + int count = 0; + + + string RESPONSE; // These a instances of the type string + string CONVERTER; // in C++ you have to do a lot of convertions with strings because some functions can only handle certain types of strings + char MESSAGE[200]; // this is a simpler string (a collection of characters). //Must be large enough to accomedate the largest amount of data sent from the server + + SOCKADDR_IN ADDRESS; + + SOCKET sock; + sock = socket(AF_INET, SOCK_STREAM, NULL); // TCP based connection = AF_INET + + ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1"); //Local IP, the IP adress is converted into a format that can be used in Networking + ADDRESS.sin_family = AF_INET; + ADDRESS.sin_port = htons(444); // Have to use htons method to convert it to network style + + cout << "\n\tCLIENT: Do you want to connect to this SERVER? (Y/N)"; + cin >> RESPONSE; + + RESPONSE[0] = tolower(RESPONSE[0]); // Convert first letter to lower case and only this so if you type YES it takes Y = y + + if (RESPONSE == "n") + { + cout << "\n\tOK. Quitting instead."; + } + else if (RESPONSE == "y") + { + connect(sock, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS)); + + SUCCESSFUL = recv(sock, MESSAGE, sizeof(MESSAGE), NULL); //WE assign the received data into the variable Successfull + + CONVERTER = MESSAGE; //The overloaded assignment operator allows us to convert Message (the simple string) into CONVERTER a more complex string. + + cout << "\n\tMessage from SERVER:\n\n\t" << CONVERTER << endl; + + SUCCESSFUL = recv(sock, MESSAGE, sizeof(MESSAGE), NULL); + + for (int i = 0; i < strlen(MESSAGE); i++) + if (MESSAGE[i] == 'a') count++; + + + cout << "\tThere are this many a's in the sentence from the Server: " << count; + + } + else + { + cout << "\n\tThat was an inappropriate RESPONSE!"; + } + + cout << "\n\n\t"; + system("PAUSE"); + exit(1); +} \ No newline at end of file diff --git a/CLIENT.h b/CLIENT.h new file mode 100644 index 00000000..d0117f36 --- /dev/null +++ b/CLIENT.h @@ -0,0 +1,15 @@ +//TCP CLIENT header file + +//Must include "Ws2_32.lib". +#pragma once +#pragma comment(lib,"Ws2_32.lib") +#define _WINSOCK_DEPRECATED_NO_WARNINGS + +//Standard HEADER files +#include +#include +#include +#include +#include + +#define SCK_VERSION2 0x0202 diff --git a/SERVER.cpp b/SERVER.cpp new file mode 100644 index 00000000..8a7146ed --- /dev/null +++ b/SERVER.cpp @@ -0,0 +1,91 @@ +//TCP SERVER source file + +#include "SERVER.h" + + +using namespace std; + +void main() +{ + //main Locals + long SUCCESSFUL; + WSAData WinSockData; + WORD DLLVERSION; //Word are objects of a data size that our processor naturally handles (16 or 32-bit) + + + int counter=0; + int a; + + string CONVERTER; + string IntToString; + char MESSAGE[200]; + char MESSAGEANSWER[1]; + + + + //WORDS = objects of a data size that a processor naturally handles (such as 16 or 32-bit) + DLLVERSION = MAKEWORD(2, 1); //macro to create WORD value by concatenating(Means link serveral object together) its arguments + + //Start Winsock DLL + SUCCESSFUL = WSAStartup(DLLVERSION, &WinSockData); //WSAStartup starts the winsock application interface + + //Create Socket Structure + SOCKADDR_IN ADDRESS; //Instantiate a SOCKADDR_IN object and name ADRESS + int AddressSize = sizeof(ADDRESS); //Store Adress size in an int + + //Create Sockets + SOCKET sock_LISTEN; //Listen for incomming connection + SOCKET sock_CONNECTION;//activate if connection found + + //socket Arguments: AF_INET = internet domain (not Unix doman), + //SOCK_STREAM = connection oriented TCP (not SOCK_DGRAM(UDP)) + sock_CONNECTION = socket(AF_INET, SOCK_STREAM, NULL); + ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1"); //Set IP, this number must be converted + ADDRESS.sin_family = AF_INET; + ADDRESS.sin_port = htons(444); //port, htons method to convert the type to a network type + + sock_LISTEN = socket(AF_INET, SOCK_STREAM, NULL); + bind(sock_LISTEN, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS)); //Bind command bind a socket to an adress, we bind the sock_LISTEN to our IP Adress + listen(sock_LISTEN, SOMAXCONN); //LISTEN without limits. First argument is the socket we set to listen and SOMAXCONN is the maximum number of connections a system will allow. + + //If connection found: + + for (;;) //Infinite foor loop will loop forever... + { + cout << "\n\tSERVER: Waiting for incoming connection..."; + + if(sock_CONNECTION= accept(sock_LISTEN, (SOCKADDR*)&ADDRESS, &AddressSize)) + { + cout << "\n\tA connection was found!" << endl; + + SUCCESSFUL = send(sock_CONNECTION, "Welcome! You have connected to the Server!", 46, NULL); + + ////////////////////////////////////////////////////////////////////////////////////////// + + SUCCESSFUL = recv(sock_CONNECTION, MESSAGE, 46, NULL); //RECEIVED MESSAGE FROM CLIENT + for (int i = 0; i < strlen(MESSAGE); i++) + if (MESSAGE[i] == 'a') { + counter++; + MESSAGEANSWER[0] = counter; + } + + /* + if (counter == 1) { + MESSAGEANSWER[0] = '1'; + } + if (counter == 2) { + MESSAGEANSWER[0] = '2'; + } + if (counter == 3) { + MESSAGEANSWER[0] = '3'; + } + */ + SUCCESSFUL = send(sock_CONNECTION, MESSAGEANSWER, 46, NULL); //RESULT of how many A's + + + + } + + } + +} \ No newline at end of file diff --git a/SERVER.h b/SERVER.h new file mode 100644 index 00000000..b35e1e84 --- /dev/null +++ b/SERVER.h @@ -0,0 +1,21 @@ +//TCP Server header file +//Note: you need to add "Ws2_32.lib" to the LINKER setting. Like this: +#pragma comment(lib,"Ws2_32.lib") +#define _WINSOCK_DEPRECATED_NO_WARNINGS + +//STD console header files +#include +#include +#include +#include + +//SOCKET header files +#include +#include +#include +#include +#include +#include +#include + +#define SCK_VERSION2 0x0202