Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions CLIENT.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 15 additions & 0 deletions CLIENT.h
Original file line number Diff line number Diff line change
@@ -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 <sdkddkver.h>
#include <WinSock2.h>
#include<Windows.h>
#include<iostream>
#include <string>

#define SCK_VERSION2 0x0202
91 changes: 91 additions & 0 deletions SERVER.cpp
Original file line number Diff line number Diff line change
@@ -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



}

}

}
21 changes: 21 additions & 0 deletions SERVER.h
Original file line number Diff line number Diff line change
@@ -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 <sdkddkver.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

//SOCKET header files
#include<WinSock2.h>
#include<Windows.h>
#include<iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>

#define SCK_VERSION2 0x0202
Empty file added main.ccp
Empty file.