Skip to content

Commit

Permalink
sample
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Jul 2, 2024
1 parent 76c04c7 commit b6b147e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ make_sample(DIR "ecs/events")
make_sample(DIR "ecs/relations")
make_sample(DIR "gl/compute")
make_sample(DIR "gl/quad")
make_sample(DIR "net/tcp")
45 changes: 45 additions & 0 deletions core/samples/net/tcp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <cstring>
#include <iostream>
#include <thread>

#include <cubos/core/log.hpp>
/// [Import header]
#include <cubos/core/net/tcp_socket.hpp>
/// [Import header]
#include <cubos/core/reflection/external/cstring.hpp>
#include <cubos/core/reflection/external/primitives.hpp>

using cubos::core::net::Address;
using cubos::core::net::TcpSocket;

/// [Define constants]
#define SERVER_ADDRESS Address::LocalHost
#define SERVER_PORT 8080
/// [Define constants]

/// [Client function]
void runClient()
{
TcpSocket socket;
CUBOS_ASSERT(socket.connect(SERVER_ADDRESS, SERVER_PORT, 0), "Failed to connect to TCP server");

CUBOS_INFO("TCP client connected at port {}", SERVER_PORT);

// send message
std::string msg = "ping";
CUBOS_ASSERT(socket.send(msg), "Failed to send data via TCP");

// receive message
char buffer[1024];
std::size_t received;
Address senderAddress;
CUBOS_ASSERT(socket.receive(buffer, sizeof(buffer), received), "Failed to receive data via TCP");

CUBOS_INFO("Received via TCP: {}", buffer);
}
/// [Client function]

int main()
{
CUBOS_TODO("listener part");
}

0 comments on commit b6b147e

Please sign in to comment.