tcpsocket client and server example.
This commit is contained in:
parent
1de53ef6e8
commit
bb9eaa3981
@ -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})
|
||||
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})
|
||||
86
examples/tcpsocket_client.cpp
Normal file
86
examples/tcpsocket_client.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include <frnetlib/TcpSocket.h>
|
||||
#include <frnetlib/TcpListener.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
76
examples/tcpsocket_server.cpp
Normal file
76
examples/tcpsocket_server.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include <frnetlib/TcpSocket.h>
|
||||
#include <frnetlib/TcpListener.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
//fr::HttpSocket<fr::TcpSocket> 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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user