📄 httpclient.cpp
字号:
//
// This file is part of an OMNeT++/OMNEST simulation example.
//
// Copyright (C) 1992-2005 Andras Varga
//
// This file is distributed WITHOUT ANY WARRANTY. See the file
// `license' for details on this and other legal matters.
//
#include <omnetpp.h>
#include "httpmsg_m.h"
class HTTPClient : public cSimpleModule
{
private:
int addr;
int srvAddr;
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msg);
void sendHTTPRequest();
void processHTTPReply(HTTPMsg *httpMsg);
};
Define_Module(HTTPClient);
void HTTPClient::initialize()
{
addr = par("addr");
srvAddr = par("srvAddr");
cMessage *timer = new cMessage("timer");
scheduleAt(simTime()+par("sendIaTime").doubleValue(), timer);
}
void HTTPClient::handleMessage(cMessage *msg)
{
if (msg->isSelfMessage())
{
sendHTTPRequest();
scheduleAt(simTime()+par("sendIaTime").doubleValue(), msg);
}
else
{
processHTTPReply(check_and_cast<HTTPMsg *>(msg));
}
}
void HTTPClient::sendHTTPRequest()
{
const char *header = "GET / HTTP/1.0\r\n\r\n";
// assemble and send HTTP request
HTTPMsg *httpMsg = new HTTPMsg();
httpMsg->setPayload(header);
httpMsg->setDestAddress(srvAddr);
httpMsg->setSrcAddress(addr);
send(httpMsg,"out");
}
void HTTPClient::processHTTPReply(HTTPMsg *httpMsg)
{
delete httpMsg;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -