我遇到了插座和数据包的麻烦......让我解释一下
当客户端发送“Se | Bla | Blu”时,输出为“Re | Bla | Blu”,就像我想要的那样,但问题是,在consol服务器中,输出是“DataAsString = Se | Bla” | Bluouh我们现在在这里Noooo“。 所以我们可以看到它没有在writeAsync()中打印“我在这里”,所以我们可以假设有一个错误,这就是我要解决的问题,弹出窗口意味着:
Debug Assertion失败!
程序:C:\ Windows \ system32 \ MSVCP120D.dll文件:c:\ program files (x86)\ microsoft visual studio 12.0 \ vc \ include \ xstring Line:79
Expression:string iterator not deferencable
有关程序如何导致断言失败的信息, 请参阅有关断言的可视化C ++文档。
(按重试调试应用程序)[放弃] [重试] [忽略]
我试图调试它,但没办法,我不知道问题来自哪里。任何的想法 ?
也许缓冲区不允许字符串?也许我必须将dataAsString和m_dataToRead重新初始化为“”之后? 提前谢谢,见到你。
编辑:完整:
#include <boost/asio.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <memory>
#include <utility>
#include "../configuration/constantes.h"
class client : public std::enable_shared_from_this<client>
{
public:
client(boost::asio::ip::tcp::socket socket) : m_socket(std::move(socket)){}
void start()
{
readAsync();
}
private:
void readAsync()
{
auto self(shared_from_this());
m_socket.async_read_some(boost::asio::buffer(m_dataToRead, 512), [this, self](boost::system::error_code error, std::size_t length)
{
if (!error)
{
packetsTreating(m_dataToRead, length);
}
start();
});
}
void writeAsync(std::string m_dataToSend, size_t length)
{
auto self(shared_from_this());
boost::asio::async_write(m_socket, boost::asio::buffer(m_dataToSend, length), [this, self](boost::system::error_code error, std::size_t)
{
if (!error)
{
std::cout << "I am here";
start();
}
});
}
void speak(std::string channel, std::string object)
{
std::cout << "Bouh";
packetLength = 2 + sizeof(canal) + sizeof(objet);
std::cout << "We are here now";
writeAsync(("Re|" + channel + "|" + object), packetLength);
std::cout << "Noooo";
}
void logIn(std::string id, std::string wp)
{
}
void logOut(std::string whatDC)
{
}
void packetsTreating(char* data, size_t length)
{
std::string dataAsString;
dataAsString.assign(data, length);
std::cout << "DataAsString = " << dataAsString;
std::vector<std::string> fields;
boost::split(fields, dataAsString, boost::is_any_of("|"));
if (fields[0] == "Co" && fields.size() == 3)
logIn(fields[1], fields[2]);
else if (fields[0] == "Dc" && fields.size() == 2)
logOut(fields[1]);
else if (fields[0] == "Se" && fields.size() == 3)
speak(fields[1], fields[2]);
else
std::cout << "Unknown command." << std::endl;
}
size_t packetLength = 0;
boost::asio::ip::tcp::socket m_socket;
char m_dataToRead[512];
};
class server
{
public:
server(boost::asio::io_service& ios, short port) : m_acceptor(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)), m_socket(ios)
{
acceptConnection();
}
private:
void acceptConnection()
{
m_acceptor.async_accept(m_socket, [this](boost::system::error_code error)
{
if (!error && connectedPerso <= maxConnec) // maxConnec is in constantes.h, = 250
{
connectedPerso++; // Btw, do you know how to -- it when a client disconnect ?
std::cout << "Connection, there is " << connectedPerso << " clients." << std::endl;
std::make_shared<client>(std::move(m_socket))->start();
}
acceptConnection();
});
}
unsigned short connectedPerso = 0;
boost::asio::ip::tcp::acceptor m_acceptor;
boost::asio::ip::tcp::socket m_socket;
};
void main()
{
try
{
std::cout << "TCP open on port " << port << ". maxConnec is " << maxConnec << "." << std::endl;
boost::asio::io_service iosConnector;
serveur serveur(iosConnector, port); // port = 2013
iosConnector.run();
}
catch (std::exception& e)
{
std::cerr << "Exception : " << e.what() << "\n";
}
}
答案 0 :(得分:1)
存在逻辑错误。您正在从同步写入处理程序和同步读取处理程序中调用start()。由于start()在套接字上调用async_read,因此在第一次写入后,最终会在套接字上有2个活动的异步读取。这是非法的。
答案 1 :(得分:0)
提供的答案是为了证明您发布的代码似乎适用于我的系统:
$ telnet localhost 2013
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
hello
ccccc
Re|1|hello|
Se|channel|text
Re|channel|text
Se|channel|text
Re|channel|text
Se|channel|text
Re|channel|text
^]
telnet> close
Connection closed.
标准输出:
Connection, there is 1 clients.
DataAsString = hello
Unknown command.
DataAsString = hello
Unknown command.
DataAsString = ccccc
Unknown command.
DataAsString = Re|1|hello|
Unknown command.
DataAsString = Se|channel|text
BouhWe are here nowNooooI am hereDataAsString = Se|channel|text
BouhWe are here nowNooooI am hereDataAsString = Se|channel|text