利用spdz开源库的网络通信模块实现两端通信
程序一共有两端,一个是客户端client.cpp,一个是服务器端server.cpp
客户端代码:client.cpp
#include <iostream>
#include "mytools.hpp"
#include "Networking/Player.h"
#include "Tools/octetStream.h"
#include "Tools/FlexBuffer.h"
#include "Networking/sockets.h"
#include "Tools/Hash.h"
#include "Tools/int.h"
#include "Networking/Receiver.h"
#include "Networking/Sender.h"
#include "Tools/ezOptionParser.h"
using namespace std;
int main()
{
int player = 1;
int nplayers = 2;
const char* servername = "127.0.0.1";
int pnb = 8000;
int my_port = 8005;
const Names n = Names(player,nplayers,servername,pnb,my_port);
PlainPlayer p = PlainPlayer(n,10);
//MultiPlayer<int> p = MultiPlayer<int>(n);
string str = "";
auto m2 = octetStream(str);
p.receive_player_no_stats(0,m2);
print(m2.get_data());
str = "Hello222 !!";
auto m1 = octetStream(str);
p.send_to_no_stats(0,m1);
cout<< "Success !!" <<endl;
return 0;
}
服务器端代码:server.cpp
#include <iostream>
#include "mytools.hpp"
#include "Networking/Player.h"
#include "Tools/octetStream.h"
#include "Tools/FlexBuffer.h"
#include "Networking/sockets.h"
#include "Tools/Hash.h"
#include "Tools/int.h"
#include "Networking/Receiver.h"
#include "Networking/Sender.h"
#include "Tools/ezOptionParser.h"
using namespace std;
int main()
{
int player = 0;
int nplayers = 2;
const char* servername = "127.0.0.1";
int pnb = 8000;
int my_port = 8001;
Names n = Names(player,nplayers,servername,pnb,my_port);
//Names n = Names(player,nplayers,servername,pnb,Names::DEFAULT_PORT);
PlainPlayer p = PlainPlayer(n,10);
string str = "Hello111 !!";
auto m1 = octetStream(str);
p.send_to_no_stats(1,m1);
str = "";
auto m2 = octetStream(str);
p.receive_player_no_stats(1,m2);
print(m2.get_data());
cout<< "Success !!" <<endl;
return 0;
}
服务器端向客户端发送了字符串“Hello111”,客户端向服务器端发送了字符串"Hello222"。
makefile内容:
all:client server
client: client.o
g++ -g -o client client.o ../libSPDZ.so
server: server.o
g++ -g -o server server.o ../libSPDZ.so
client.o: client.cpp mytools.hpp ../Networking/Player.h
g++ -g -c client.cpp -I ~/spdz/mpspdz027/
server.o: server.cpp mytools.hpp
g++ -g -c server.cpp -I ~/spdz/mpspdz027/
main: networktest.o
g++ -o main networktest.o ../libSPDZ.so
networktest.o: networktest.cpp mytools.hpp ../Networking/Player.h
g++ -c networktest.cpp -I ~/spdz/mpspdz027/
clean:
rm -f *.o main *.out
编译的时候使用命令
make all
mytools.hpp内容:
#include <iostream>
using namespace std;
template<class T>
void print(T s)
{
cout << s << endl;
}
运行截图:
客户端:
服务器端:
解释:
首先创建一个Names类,用来开启并监听一个端口。
创建一个PlainPlayer类,模拟一个player,用来进行消息通信。
octetStream类用来对发送的消息进行处理。
通过player的发送和接收函数来发送或者接收数据。
本博文可以到此结束。下面内容,与上面内容关联不大。
--------------------分割线---------------------------------
networktest.cpp代码:
#include <iostream>
#include <fstream>
#include <dbg.h>
#include "mytools.hpp"
#include "Networking/Player.h"
#include "Tools/ezOptionParser.h"
using namespace std;
int main(int argc, const char** argv)
{
ez::ezOptionParser opt;
opt.add(
"", // Default.
1, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"This player's number.", // Help description.
"-p" // Flag token.
);
opt.add(
"localhost", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Host where party 0 is running (default: localhost)", // Help description.
"-h" // Flag token. hostname
);
opt.add(
"8000", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Base port number (default: 8000).", // Help description.
"-pn" // Flag token. base port number
);
opt.add(
"", // Default.
1, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"my port number.", // Help description.
"-mpn" // Flag token.
);
opt.add(
"", // Default.
1, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of player.", // Help description.
"-N" // Flag token.
);
opt.parse(argc, argv);
opt.syntax = "./main [OPTIONS] <progname>";
int player;
int nplayers;
string hostname;
int pnb;
int my_port;
opt.get("-p")->getInt(player);
opt.get("-pn")->getInt(pnb);
opt.get("-mpn")->getInt(my_port);
opt.get("-h")->getString(hostname);
opt.get("-N")->getInt(nplayers);
Names n = Names(player,nplayers,hostname,pnb,my_port);
PlainPlayer p = PlainPlayer(n,10);
dbg(p.my_num());
dbg("Success !!");
return 0;
}
这个代码用到了dbg库,没有的可以删了,或者安装一个,dbg安装步骤:https://blog.csdn.net/shengsikandan/article/details/120766714
编译方式:
make main
运行办法,两个终端:
第一个终端:
./main -p 0 -h localhost -pn 8000 -mpn 8005 -N 2
第二个终端:
./main -p 1 -h localhost -pn 8000 -mpn 8006 -N 2
这个代码只用了一个可执行文件,就能实现两个端口进行通信。