Networking Initial
This commit is contained in:
parent
df3767df29
commit
8e00882bb3
6 changed files with 178 additions and 86 deletions
|
|
@ -1,45 +1,81 @@
|
|||
#include <iostream>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include "asio.hpp"
|
||||
|
||||
constexpr int PORT = 8080;
|
||||
constexpr int BUFFER_SIZE = 1024;
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
|
||||
// #include <unistd.h>
|
||||
// #include <cstring>
|
||||
|
||||
constexpr int PORT = 3333;
|
||||
const char* SERVER_IP = "127.0.0.1";
|
||||
|
||||
std::mutex mutex;
|
||||
|
||||
void* readServerBroadcast(void* clientSocketPtr) {
|
||||
auto socket = ((asio::ip::tcp::socket*)clientSocketPtr);
|
||||
|
||||
while (true) {
|
||||
|
||||
short messageSize;
|
||||
asio::read(*socket, asio::buffer(&messageSize, 2));
|
||||
|
||||
mutex.lock();
|
||||
|
||||
auto message = new char[messageSize + 1];
|
||||
message[messageSize] = 0;
|
||||
|
||||
asio::read(*socket, asio::buffer(message, messageSize));
|
||||
|
||||
std::cerr << "Broadcast : " << message << std::endl;
|
||||
|
||||
delete[] message;
|
||||
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Create socket
|
||||
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (clientSocket == -1) {
|
||||
std::cerr << "Failed to create socket" << std::endl;
|
||||
asio::io_context ioContext;
|
||||
|
||||
// Create a TCP socket
|
||||
asio::ip::tcp::socket socket(ioContext);
|
||||
|
||||
// Connect to a server
|
||||
asio::ip::tcp::endpoint endpoint(asio::ip::address::from_string(SERVER_IP), PORT);
|
||||
socket.connect(endpoint);
|
||||
|
||||
// Create a new thread to handle the client
|
||||
pthread_t threadId;
|
||||
if (pthread_create(&threadId, nullptr, readServerBroadcast, (void*) &socket) != 0) {
|
||||
std::cerr << "Failed to create thread for client" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Connect to server
|
||||
sockaddr_in serverAddress{};
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_port = htons(PORT);
|
||||
if (inet_pton(AF_INET, SERVER_IP, &serverAddress.sin_addr) <= 0) {
|
||||
std::cerr << "Invalid address or address not supported" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
// Detach the thread so it can run independently
|
||||
pthread_detach(threadId);
|
||||
|
||||
if (connect(clientSocket, reinterpret_cast<sockaddr*>(&serverAddress), sizeof(serverAddress)) == -1) {
|
||||
std::cerr << "Failed to connect to server" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
while (true) {
|
||||
std::string message;
|
||||
std::cout << " >> ";
|
||||
std::cin >> message;
|
||||
|
||||
// Send message to server
|
||||
const char* message = "Hello, server!";
|
||||
if (write(clientSocket, message, strlen(message)) == -1) {
|
||||
std::cerr << "Failed to write to socket" << std::endl;
|
||||
return 1;
|
||||
mutex.lock();
|
||||
|
||||
// Send a message to the server
|
||||
auto messageSize = (short) message.size();
|
||||
asio::write(socket, asio::buffer(&messageSize, 2));
|
||||
|
||||
// Send a message to the server
|
||||
asio::write(socket, asio::buffer(message + "\n"));
|
||||
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
// Close socket
|
||||
close(clientSocket);
|
||||
// close(clientSocket);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue