|
I would like to understand how to use TcpSocket socket;
socket.connect(Address("10.0.0.2"), 2000);
Packet *packet = new Packet("data", makeShared<ByteCountChunk>(B(16*1024)));
socket.send(packet);
socket.close();in I can get it to work by doing something similar to what needs to be done on the receiving side; see the full code of my minimal example below for what I do there. But the sentence right after the example code in the linked documentation is
This makes me believe that sending should not be this "cumbersome". Am I missing something or does sending require the same callback setup as receiving, after all? Full code of a minimal test example: TestApp.cc: #include "TestApp.h"
#include "InitialMessage_m.h"
#include "inet/networklayer/common/L3AddressResolver.h"
#include "inet/common/packet/chunk/ByteCountChunk.h"
Define_Module(TestApp);
const int EXAMPLE_PORT = 42;
const std::string SENDER = "client0";
const std::string RECEIVER = "client1";
void TestApp::initialize(int stage)
{
if (stage == 0 && this->getParentModule()->getName() == SENDER) {
this->scheduleAt(omnetpp::SimTime(1, omnetpp::SimTimeUnit::SIMTIME_S), new InitialMessage);
}
if (stage == inet::INITSTAGE_APPLICATION_LAYER) {
this->serverSocket.setOutputGate(this->gate("socketOut"));
this->serverSocket.setCallback(this);
this->serverSocket.bind(EXAMPLE_PORT);
this->serverSocket.listen();
}
}
void TestApp::handleMessage(cMessage *msg)
{
if (auto initMsg = dynamic_cast<InitialMessage *>(msg)) {
EV_WARN_C("TestApp") << "Trying to send!" << std::endl;
inet::L3Address destination;
inet::L3AddressResolver().tryResolve(RECEIVER.c_str(), destination);
inet::TcpSocket socket;
socket.setOutputGate(this->gate("socketOut"));
socket.setCallback(this);
socket.connect(destination, EXAMPLE_PORT);
auto packet = new inet::Packet("data", inet::makeShared<inet::ByteCountChunk>(inet::B(16*1024)));
socket.send(packet);
socket.close();
delete initMsg;
} else if (this->serverSocket.belongsToSocket(msg)) {
this->serverSocket.processMessage(msg);
} else if (auto socket = this->inSocketMap.findSocketFor(msg)) {
socket->processMessage(msg);
} else {
EV_WARN_C("TestApp") << "Ignored message: " << msg << std::endl;
delete msg;
}
}
/* TcpSocket::ICallback methods */
void TestApp::socketDataArrived(inet::TcpSocket *socket, inet::Packet *packet, bool urgent) {
EV_WARN_C("TestApp") << "Test Packet arrived: " << packet << std::endl;
delete packet;
}
void TestApp::socketAvailable(inet::TcpSocket *socket, inet::TcpAvailableInfo *availableInfo) {
auto inSocket = new inet::TcpSocket(availableInfo);
inSocket->setOutputGate(this->gate("socketOut"));
inSocket->setCallback(this);
this->inSocketMap.addSocket(inSocket);
this->serverSocket.accept(availableInfo->getNewSocketId());
}
void TestApp::socketEstablished(inet::TcpSocket *socket) {}
void TestApp::socketPeerClosed(inet::TcpSocket *socket) {}
void TestApp::socketClosed(inet::TcpSocket *socket) {}
void TestApp::socketFailure(inet::TcpSocket *socket, int code) {}
void TestApp::socketStatusArrived(inet::TcpSocket *socket, inet::TcpStatusInfo *status) {}
void TestApp::socketDeleted(inet::TcpSocket *socket) {}TestApp.h: #ifndef __TESTAPP_H_
#define __TESTAPP_H_
#include <omnetpp.h>
#include "inet/transportlayer/contract/tcp/TcpSocket.h"
#include "inet/common/socket/SocketMap.h"
using namespace omnetpp;
class TestApp : public cSimpleModule, public inet::TcpSocket::ICallback
{
protected:
virtual int numInitStages() const override { return inet::NUM_INIT_STAGES; }
virtual void initialize(int stage) override;
virtual void handleMessage(cMessage *msg) override;
/* TcpSocket::ICallback methods */
virtual void socketDataArrived(inet::TcpSocket *socket, inet::Packet *packet, bool urgent) override;
virtual void socketAvailable(inet::TcpSocket *socket, inet::TcpAvailableInfo *availableInfo) override;
virtual void socketEstablished(inet::TcpSocket *socket) override;
virtual void socketPeerClosed(inet::TcpSocket *socket) override;
virtual void socketClosed(inet::TcpSocket *socket) override;
virtual void socketFailure(inet::TcpSocket *socket, int code) override;
virtual void socketStatusArrived(inet::TcpSocket *socket, inet::TcpStatusInfo *status) override;
virtual void socketDeleted(inet::TcpSocket *socket) override;
private:
inet::TcpSocket serverSocket;
inet::SocketMap inSocketMap;
};
#endifTestApp.ned: InitialMessage.msg: SmallNetwork.ned: smallNetwork.ini: |
Replies: 1 comment 2 replies
|
Take a look at: https://deepwiki.com/search/i-would-like-to-understand-how_f38cd8fb-990a-4ec5-871d-a0e5bbd84c3d?mode=fast TL;DR: Your socket.send() call executes before the TCP connection is established. In INET, TcpSocket.connect() is asynchronous; you must wait for the socketEstablished callback (or check socket state) before sending data. Receiving requires callbacks, but sending only requires respecting the connection state. But it's not a problem to call send() before the connection is established. The data would be queued and sent along the ACK packet. The problem is that you https://github.com/inet-framework/inet/blob/4a912bf7/src/inet/transportlayer/tcp/Tcp.ned#L67-L70 I highly suggest to explore your question with the interactive documentation at: https://ai-inet.omnetpp.org . I just pasted your question there, and it gave the above answer. |
Take a look at: https://deepwiki.com/search/i-would-like-to-understand-how_f38cd8fb-990a-4ec5-871d-a0e5bbd84c3d?mode=fast
TL;DR:
Your socket.send() call executes before the TCP connection is established. In INET, TcpSocket.connect() is asynchronous; you must wait for the socketEstablished callback (or check socket state) before sending data. Receiving requires callbacks, but sending only requires respecting the connection state.
But it's not a problem to call send() before the connection is established. The data would be queued and sent along the ACK packet. The problem is that you
close()your connection before the connection is established and in that case, queued data is dropped accord…