我正在为路由器编写代码。我已经设法从客户那里发送和接收信息。我现在要做的是创建一个向量来处理来自我拥有的接收包的线程的所有传入包。
我正在使用 C++ 和 BOOST。
int rawpacket_recv(int s, unsigned char *packet, int length)
{
int to_recv = 0;
to_recv = read(s, packet, length);
//and some error detection
}
void RecvThread(int *socket)
{
int eth_receiver, eth_socket;
eth_socket = *socket;
for(;;)
{
unsigned char *eth_buffer = (unsigned char *)malloc(1518);
eth_receiver = rawpacket_recv(eth_socket, eth_buffer, 1518);
//put eth_buffer into a vector defined in main
free(eth_buffer);
usleep(10000);
}
}
int main(int argc, char **argv)
{
vector<unsigned char *> packages;
int eth0socket;
char eth0 = "eth0";
eth0socket = CreateAndBindSocket(ETH_P_ALL, ð0socket, eth0); //Method from the program that does what it name describes
boost::thread eth0recvthread(RecvThread, ð0socket);
eth0recvthread.interrupt();
eth0recvthread.join();
return 0;
}
我在代码片段中留下了一些不重要的代码,但我想要的是 RecvThread 能够将接收到的包放入这个向量中,以便我可以在 main 方法中处理它们(现在)——大概作为互斥体/信号。
谢谢!