Skip to content

Commit

Permalink
simple chat program set up, S receives first, C sends first, S sends,…
Browse files Browse the repository at this point in the history
… C receives, etc ...
  • Loading branch information
AndreasG9 committed Sep 12, 2020
1 parent 5655b23 commit d4754b9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 42 deletions.
94 changes: 64 additions & 30 deletions Lab1/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ int main(int argc, char** argv) {
read_key.close();
}

char key_arr[9];
strcpy(key_arr, key.c_str()); // convert to c string



// ----------- Network Setup (Winsock) ----------------------------------------------

// similar to what we did with server.cpp ...
Expand All @@ -50,59 +55,88 @@ int main(int argc, char** argv) {
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(DEFAULT_PORT_NUM); // default is 8000

// Create Socket (client)
SOCKET client_socket;
// Create Socket (client to connect)
SOCKET connected_socket;

client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == INVALID_SOCKET) {
connected_socket = socket(AF_INET, SOCK_STREAM, 0);
if (connected_socket == INVALID_SOCKET) {
std::cout << "Error, socket creation failed" << std::endl;
WSACleanup();
return 1;
}

// --------------- Connect to Server -------------------
retval = connect(client_socket, (struct sockaddr*)&server, sizeof(server));
retval = connect(connected_socket, (struct sockaddr*)&server, sizeof(server));
if (retval == SOCKET_ERROR) {
std::cout << "Error, failed to connect" << std::endl;
closesocket(client_socket);
closesocket(connected_socket);
WSACleanup();
return 1;
}

std::cout << "Connected" << std::endl;
std::cout << "\nConnected" << std::endl << std::endl;


// --------------- Main Loop (Connected to Server) -------------------
char buffer[BUFFER_LENGTH];
int retval_send = 0, retval_recieve = 0;

char temp[BUFFER_LENGTH];

// SEND init buffer data ...
char message_send[BUFFER_LENGTH], message_receive[BUFFER_LENGTH];
int retval_send = 0, retval_receive = 0;

while (true) {
// recieve and send data to server, until server disconnects

std::cout << "Type message: ";

std::cin.getline(buffer, BUFFER_LENGTH);
std::cout << "TEMP. your message is: " << buffer << std::endl;

// encrpyt the message

std::cout << "This is client." << std::endl;
std::cout << ********************<< std::endl;
std::cout << "Sent plaintext is: " << std::endl;
std::cout << "Sent ciphertext is: " << std::endl;
std::cout << ********************<< std::endl;



std::cin.getline(message_send, BUFFER_LENGTH);

// encrpyt the message ...

// send ciphertext to server/S
retval_send = send(connected_socket, message_send, strlen(message_send)+1, 0);
if (retval_send == SOCKET_ERROR) {
std::cout << "Error, failed to send" << std::endl;
closesocket(connected_socket);
WSACleanup();
return 1;
}

// Client side display (send data)
std::cout << "\n\nHi, this is client." << std::endl;
std::cout << "********************" << std::endl;
std::cout << "key is: " << key_arr << std::endl;
std::cout << "sent plaintext is: " << message_send << std::endl;
//std::cout << "sent ciphertext is: " << std::endl;
std::cout << "********************\n" << std::endl;


// receive ciphertext from server/S
retval_receive = recv(connected_socket, message_receive, BUFFER_LENGTH-1, 0);
if (retval_receive > 0) {
// DECRYPT ...

// Client side display (recieve data)
std::cout << "\n********************" << std::endl;
std::cout << "received ciphertext is: " << message_receive << std::endl;
//std::cout << "received plaintext is: " << std::endl;
std::cout << "********************\n" << std::endl;
}

else if (retval_receive == 0) break; // server disconnected

else {
std::cout << "Error, receive failed" << std::endl;
closesocket(connected_socket);
WSACleanup();
exit(1);
}


//memset(message_send, 0, BUFFER_LENGTH);
}




// server disconnected, cleanup, quit client.
shutdown(connected_socket, SD_SEND);
closesocket(connected_socket);
WSACleanup();
std::cout << "Client closed" << std::endl;

return 0;
}
45 changes: 33 additions & 12 deletions Lab1/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ int main(int argc, char **argv) {
read_key.close();
}

//std::cout << "key is: " << key << std::endl;
// store key in c char array
char key_arr[9];
strcpy(key_arr, key.c_str());

//std::cout << "key is: " << key_arr << std::endl;


// ----------- Network Setup (Winsock) ----------------------------------------------
Expand Down Expand Up @@ -97,33 +100,47 @@ int main(int argc, char **argv) {
return 1;
}

std::cout << "Connected" << std::endl;
std::cout << "Connected" << std::endl << std::endl;
closesocket(server_socket);

// ----------- Main Loop (Client is connected) ----------------------------------------------
char buffer[BUFFER_LENGTH];
char message_send[BUFFER_LENGTH], message_receive[BUFFER_LENGTH];
int retval_send = 0, retval_recieve = 0;


while (true) {
// recieve and send data to client, until client disconnects


// client will send an encrypted message/ chiphertext, store in buffer
retval_recieve = recv(client_socket, buffer, BUFFER_LENGTH, 0);
// client will send an encrypted message/ chiphertext, store in c string
retval_recieve = recv(client_socket, message_receive, BUFFER_LENGTH-1, 0);
if (retval_recieve > 0) {
// good data, decrypt message

retval_send = send(client_socket, buffer, retval_recieve, 0);
// Server side display (receive data)
std::cout << "\n********************\n" << std::endl;
std::cout << "received ciphertext is: " << message_receive << std::endl;
// std::cout << "received plaintext is: " << std::endl;
std::cout << "********************\n" << std::endl;

// DECRYPT MESSAGE


std::cout << "Type message: ";
std::cin.getline(message_send, BUFFER_LENGTH);

// send decrypted ciphertext to client/C.
retval_send = send(client_socket, message_send, strlen(message_send)+1, 0);
if (retval_send == SOCKET_ERROR) {
closesocket(client_socket);
WSACleanup();
return 1;
}

else {
// successfully sent
}
// Server side display (sent data)
std::cout << "\n\nHi, this is server." << std::endl;
std::cout << "********************" << std::endl;
std::cout << "key is: " << key_arr << std::endl;
// std::cout << "sent plaintext is: " << std::endl;
std::cout << "sent ciphertext is: " << message_send << std::endl;
std::cout << "********************\n" << std::endl;
}

else if (retval_recieve == 0) break; // client disconnected, exit loop
Expand All @@ -133,10 +150,14 @@ int main(int argc, char **argv) {
closesocket(client_socket);
WSACleanup();
exit(1);

// break; // recv failed or client disconnected, exit loop
}

//memset(message_send, 0, BUFFER_LENGTH);
}

// client disconnected, cleanup, quit server.
shutdown(client_socket, SD_SEND);
closesocket(client_socket);
WSACleanup();
Expand Down

0 comments on commit d4754b9

Please sign in to comment.