Skip to content

Commit

Permalink
just for the sake of it: ask ChatGPT
Browse files Browse the repository at this point in the history
  • Loading branch information
sorgom committed Apr 4, 2024
1 parent fc5f57e commit 79c8e5a
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
146 changes: 146 additions & 0 deletions ChatGPT/ChatGPT_dialog_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# ChatGPT dialog
## 1st question
_c++ multi thread tcp server header and source_

## 1st reply
Below is a simple example of a multi-threaded TCP server implemented in C++. This example demonstrates a basic TCP server that can handle multiple client connections simultaneously using threading.

**Server Header (server.h):**
````cpp
#ifndef SERVER_H
#define SERVER_H

#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>

class TCPServer {
public:
TCPServer(int port);
~TCPServer();

void Start();
void Stop();

private:
void Listen();
void HandleClient(int clientSocket);

int port;
int serverSocket;
std::vector<std::thread> clientThreads;
std::atomic<bool> running;
};

#endif // SERVER_H
````
**Server Source (server.cpp):**
````cpp
#include "server.h"

TCPServer::TCPServer(int port) : port(port), running(false) {}

TCPServer::~TCPServer() {
Stop();
}

void TCPServer::Start() {
running = true;

serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Error creating socket\n";
return;
}

sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);

if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
std::cerr << "Bind failed\n";
return;
}

if (listen(serverSocket, 5) < 0) {
std::cerr << "Listen failed\n";
return;
}

std::thread listenerThread(&TCPServer::Listen, this);
listenerThread.detach();
}

void TCPServer::Stop() {
running = false;
close(serverSocket);

for (auto& thread : clientThreads) {
thread.join();
}
}

void TCPServer::Listen() {
while (running) {
sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
int clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (clientSocket < 0) {
std::cerr << "Accept failed\n";
continue;
}

std::thread clientThread(&TCPServer::HandleClient, this, clientSocket);
clientThreads.push_back(std::move(clientThread));
}
}

void TCPServer::HandleClient(int clientSocket) {
// Handle client communication here
// For demonstration, just echo received messages back to the client
char buffer[1024];
int bytesRead;
while ((bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0)) > 0) {
send(clientSocket, buffer, bytesRead, 0);
}

close(clientSocket);
}
````
## 2nd question
_great, how about a main.cpp for this?_
## 2nd reply (1st of 2)
_Certainly! Here's a simple main.cpp file that demonstrates how to create an instance of the TCPServer class and start the server:_
````cpp
#include "server.h"
int main() {
// Port number to listen on
int port = 8080;
// Create an instance of the server
TCPServer server(port);
// Start the server
server.Start();
// Wait for user input to stop the server
std::cout << "Server is running. Press any key to stop.\n";
std::cin.get();
// Stop the server
server.Stop();
std::cout << "Server stopped.\n";
return 0;
}
````

Empty file added ChatGPT/main.cpp
Empty file.
71 changes: 71 additions & 0 deletions ChatGPT/server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "server.h"

TCPServer::TCPServer(int port) : port(port), running(false) {}

TCPServer::~TCPServer() {
Stop();
}

void TCPServer::Start() {
running = true;

serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cerr << "Error creating socket\n";
return;
}

sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);

if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
std::cerr << "Bind failed\n";
return;
}

if (listen(serverSocket, 5) < 0) {
std::cerr << "Listen failed\n";
return;
}

std::thread listenerThread(&TCPServer::Listen, this);
listenerThread.detach();
}

void TCPServer::Stop() {
running = false;
close(serverSocket);

for (auto& thread : clientThreads) {
thread.join();
}
}

void TCPServer::Listen() {
while (running) {
sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
int clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (clientSocket < 0) {
std::cerr << "Accept failed\n";
continue;
}

std::thread clientThread(&TCPServer::HandleClient, this, clientSocket);
clientThreads.push_back(std::move(clientThread));
}
}

void TCPServer::HandleClient(int clientSocket) {
// Handle client communication here
// For demonstration, just echo received messages back to the client
char buffer[1024];
int bytesRead;
while ((bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0)) > 0) {
send(clientSocket, buffer, bytesRead, 0);
}

close(clientSocket);
}
33 changes: 33 additions & 0 deletions ChatGPT/server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef SERVER_H
#define SERVER_H

#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>

class TCPServer {
public:
TCPServer(int port);
~TCPServer();

void Start();
void Stop();

private:
void Listen();
void HandleClient(int clientSocket);

int port;
int serverSocket;
std::vector<std::thread> clientThreads;
std::atomic<bool> running;
};

#endif // SERVER_H

0 comments on commit 79c8e5a

Please sign in to comment.