diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7b32747..e426c4e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -23,7 +23,10 @@ endif() add_executable(${PROJECT_NAME} ../main.cpp) add_executable(packet packet.cpp) - +add_executable(tcpsocket_server tcpsocket_server.cpp) +add_executable(tcpsocket_client tcpsocket_client.cpp) target_link_libraries(${PROJECT_NAME} ${FRNETLIB_LIB} ${ADDITIONAL_LIB}) -target_link_libraries(packet ${FRNETLIB_LIB} ${ADDITIONAL_LIB}) \ No newline at end of file +target_link_libraries(packet ${FRNETLIB_LIB} ${ADDITIONAL_LIB}) +target_link_libraries(tcpsocket_server ${FRNETLIB_LIB} ${ADDITIONAL_LIB}) +target_link_libraries(tcpsocket_client ${FRNETLIB_LIB} ${ADDITIONAL_LIB}) \ No newline at end of file diff --git a/examples/tcpsocket_client.cpp b/examples/tcpsocket_client.cpp new file mode 100644 index 0000000..4cbb581 --- /dev/null +++ b/examples/tcpsocket_client.cpp @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + + + +int client_round(fr::TcpSocket& socket) +{ + cout << "CLIENT:Going to send something ..." << endl; + + //Receive the request + fr::Packet packet; + packet << "Hello there, I am" << (float)1.2 << "years old"; + if (socket.send(packet) != fr::Socket::Success) + { + //Failed to send packet + cout << "CLIENT:Seems got something wrong when sending" << endl; + return -1; + } + + cout << "CLIENT:Going to receive ..." << endl; + + + if (socket.receive(packet) != fr::Socket::Success) + { + cout << "CLIENT:seems got something wrong when receiving" << endl; + return -2; + } + + std::string str1, str2; + float age; + packet >> str1 >> age >> str2; + + cout << "CLIENT:we got:" << str1 << age << str2 << endl; + + cout << "CLIENT:round finished" << endl; + cout << endl << endl << endl << endl; + return 0; +} + + +int main() +{ + fr::TcpSocket socket; + if (socket.connect("127.0.0.1", "8081") != fr::Socket::Success) + { + //Failed to connect + cout << "CLIENT:it seem that the socket can be accessed or there is no such socket at all" << endl; + socket.close_socket(); + return -1; + } + + + string op_str; + int rtn = 0; + + while (true) { + cout << "CLIENT:choose what you want to do, `c` for `continue`, `q` for `quit`:" << endl; + cin >> op_str; // count for possible mutiple char input + + if (op_str.length() > 1) { + cout << "CLIENT:Seems that you input more than one char, plese check your input" << endl; + continue; + } + + char op = op_str[0]; + switch (op) { + case 'c': + cout << "continue" << endl; + rtn = client_round(socket); + break; + case 'q': + break; + } + + if (op == 'q') + break; + if (rtn != 0) + break; + } + + socket.close_socket(); + cout << "all done, bye" << endl; +} + diff --git a/examples/tcpsocket_server.cpp b/examples/tcpsocket_server.cpp new file mode 100644 index 0000000..d1d4e7e --- /dev/null +++ b/examples/tcpsocket_server.cpp @@ -0,0 +1,76 @@ +#include +#include + +using namespace std; + +int main(){ + //fr::HttpSocket client; //fr::TcpSocket for HTTP. fr::SSLSocket for HTTPS. + + fr::TcpSocket client; + + + fr::TcpListener listener; //Use an fr::SSLListener if HTTPS. + + string port = "8081"; + + //Bind to a port + if(listener.listen(port) != fr::Socket::Success) + { + cout << "LISTENER:Failed to bind to port, going to shutdown" << endl; + listener.shutdown(); + return -1; + } + + cout << "LISTENER:Listener is listening on port " << port << " ..." << endl; + + while (true) + { + cout << "LISTENER:Waiting for a new connection ..." << endl; + //Accept a new connection + if (listener.accept(client) != fr::Socket::Success) + { + cout << "LISTENER:Failed to accept client, shutdown" << endl; + break; + } + + while (true) // infinate loop for the communication + { + try + { + fr::Packet packet; + if (client.receive(packet) != fr::Socket::Success) + { + cout << "LISTENER:Failed to receive request" << endl; + } + + std::string str1, str2; + float age; + packet >> str1 >> age >> str2; + + cout << "LISTENER:We got from client:" << str1 << age << str2 << endl; + + + if (client.send(packet) != fr::Socket::Success) + { + cout << "LISTENER:Seems got something wrong when sending" << endl; + //return -2; + } + } + catch (const std::exception& e) + { + cout << "ERROR: " << e.what() << endl; + cout << "LISTENER:Seems that the client stop the connection, just destory current connection and wait for another" << endl; + //Close connection + client.close_socket(); + break; + } + } // inner while + + + }// out while + + cout << "Should not got there !!" << endl; + listener.shutdown(); + return 0; +} +