28 #include <QtNetwork/QNetworkInterface> 29 #include <QtNetwork/QTcpSocket> 30 #include <QtCore/QCoreApplication> 31 #include <QtCore/QFile> 32 #include <QtCore/QFileInfo> 33 #include <QtCore/QTime> 34 #include <opencv2/opencv.hpp> 39 printf(
"\ntcpService [options] port\n" 41 " --add \"image.png\" # Add object (file name + id). Set id=0\n" 42 " will make the server generating an id.\n" 43 " --remove # Remove object by ID.\n" 44 " --host #.#.#.# Set host address.\n" 45 " --help Show this help.\n" 47 " Add: $ tcpService --add image.png 1 --host 127.0.0.1 4000\n" 48 " Remove: $ tcpService --remove 1 --host 127.0.0.1 4000\n");
52 int main(
int argc,
char * argv[])
60 for(
int i=1; i<argc-1; ++i)
62 if(strcmp(argv[i],
"--host") == 0 || strcmp(argv[i],
"-host") == 0)
71 printf(
"error parsing --host\n");
76 if(strcmp(argv[i],
"--add") == 0 || strcmp(argv[i],
"-add") == 0)
85 addId = atoi(argv[i]);
89 printf(
"error parsing --add\n");
94 printf(
"error parsing --add\n");
99 if(strcmp(argv[i],
"--remove") == 0 || strcmp(argv[i],
"-remove") == 0)
104 removeId = std::atoi(argv[i]);
108 printf(
"error parsing --remove\n");
113 if(strcmp(argv[i],
"-help") == 0 ||
114 strcmp(argv[i],
"--help") == 0)
119 printf(
"Unrecognized option: %s\n", argv[i]);
123 port = atoi(argv[argc-1]);
125 if(fileName.isEmpty() && removeId == -1)
127 printf(
"Arguments --add or --remove should be set.\n");
132 printf(
"Port should be set!\n");
136 if(ipAddress.isEmpty())
138 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
142 if(!fileName.isEmpty())
144 image = cv::imread(fileName.toStdString());
147 printf(
"Cannot read image from \"%s\".\n", fileName.toStdString().c_str());
150 fileName = QFileInfo(fileName).fileName();
153 QCoreApplication
app(argc, argv);
156 QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
157 QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
159 printf(
"Connecting to \"%s:%d\"...\n", ipAddress.toStdString().c_str(), port);
160 request.connectToHost(ipAddress, port);
162 if(!request.waitForConnected())
164 printf(
"Connecting to \"%s:%d\"... connection failed!\n", ipAddress.toStdString().c_str(), port);
169 printf(
"Connecting to \"%s:%d\"... connected!\n", ipAddress.toStdString().c_str(), port);
173 QDataStream out(&block, QIODevice::WriteOnly);
174 out.setVersion(QDataStream::Qt_4_0);
180 std::vector<unsigned char> buf;
181 cv::imencode(
".png", image, buf);
186 quint64 imageSize = buf.size();
188 out.writeRawData((
char*)buf.data(), (int)buf.size());
189 printf(
"Add object %d \"%s\"\n", addId, fileName.toStdString().c_str());
195 printf(
"Remove object %d\n", removeId);
198 out.device()->seek(0);
199 out << (quint64)(block.size() -
sizeof(quint64));
201 qint64 bytes = request.write(block);
202 printf(
"Service published (%d bytes)!\n", (
int)bytes);
203 request.waitForBytesWritten();
204 request.waitForReadyRead();
int main(int argc, char *argv[])