Cleaned up mess
This commit is contained in:
parent
bf8a6345d7
commit
1b64bbbb67
@ -1,14 +0,0 @@
|
||||
#include <iostream>
|
||||
#include "frnetlib/Packet.h"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
fr::Packet packet;
|
||||
std::vector<std::pair<int, int>> bob = {{1, 2}, {3, 4}};
|
||||
packet << bob;
|
||||
bob.clear();
|
||||
|
||||
packet >> bob;
|
||||
std::cout << bob[0].first << ", " << bob[0].second << ", " << bob[1].first << ", " << bob[1].second << std::endl;
|
||||
}
|
||||
@ -1,90 +0,0 @@
|
||||
#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;
|
||||
|
||||
string server_ip = "127.0.0.1";
|
||||
string server_port = "8081";
|
||||
|
||||
if (socket.connect(server_ip, server_port) != 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;
|
||||
}
|
||||
|
||||
@ -1,76 +0,0 @@
|
||||
#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