💡 Finish client comments
This commit is contained in:
parent
347af49a5d
commit
48ae343609
1 changed files with 32 additions and 0 deletions
|
@ -79,14 +79,30 @@ int main() {
|
|||
cout << "Failed to create SSL object" << endl;
|
||||
}
|
||||
|
||||
// Initializes a new TCP socket in the IPv4 family
|
||||
// 0 tells the OS to automatically pick the correct protocol (TCP)
|
||||
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (clientSocket == -1) {
|
||||
cout << "Client socket failed" << endl;
|
||||
return clientSocket;
|
||||
}
|
||||
|
||||
// Attach our SSL object to the socket
|
||||
SSL_set_fd(ssl, clientSocket);
|
||||
|
||||
sockaddr_in serverAdress;
|
||||
|
||||
// Specify that we use the IPv4 family
|
||||
serverAdress.sin_family = AF_INET;
|
||||
|
||||
// Define the port the client should connect to
|
||||
serverAdress.sin_port = htons(8080);
|
||||
|
||||
// Define the IP the client should connect to
|
||||
serverAdress.sin_addr.s_addr = inet_addr("127.0.0.50");
|
||||
|
||||
// Connect the client to the server
|
||||
// serverAdress is cast to a sockaddr
|
||||
int connectStatus = connect(clientSocket, (struct sockaddr *)&serverAdress,
|
||||
sizeof(serverAdress));
|
||||
if (connectStatus != 0) {
|
||||
|
@ -102,10 +118,17 @@ int main() {
|
|||
}
|
||||
|
||||
while (true) {
|
||||
|
||||
// Create a new message object
|
||||
Message message;
|
||||
|
||||
// FIXME Seems to send multiple messages if content contains spaces
|
||||
// Get the content of the message from the user
|
||||
cout << "Type message to server: ";
|
||||
cin >> message.content;
|
||||
|
||||
// Exit the program if the user typed "exit" as the content of the
|
||||
// message
|
||||
if (message.content == "exit") {
|
||||
cout << "BYE!" << endl;
|
||||
break;
|
||||
|
@ -114,14 +137,23 @@ int main() {
|
|||
cout << endl;
|
||||
message.username = "Client 1";
|
||||
|
||||
// Send the content of the message to the server with encryption
|
||||
// .data() gets a pointer to the content of the message
|
||||
SSL_write(ssl, message.toString().data(),
|
||||
strlen(message.toString().data()));
|
||||
}
|
||||
|
||||
// Clean up
|
||||
// Deallocate the SSL objects
|
||||
SSL_free(ssl);
|
||||
|
||||
// Deallocate the SSL context
|
||||
SSL_CTX_free(ctx);
|
||||
|
||||
// Deallocate the SSL error strings
|
||||
ERR_free_strings();
|
||||
|
||||
// Close the client socket
|
||||
close(clientSocket);
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue