Coder Social home page Coder Social logo

Comments (4)

wlgq2 avatar wlgq2 commented on July 22, 2024

TCP协议设计如此,你可以尝试使用uv-cpp提供的读写pack机制。

from uv-cpp.

LoveChina-3000 avatar LoveChina-3000 commented on July 22, 2024
LiteTcpClient client(loop);
client.connectToServer(addr2);
uv::http::HttpServer::SetBufferMode(uv::GlobalConfig::BufferMode::CycleBuffer);

#if 1

//跨线程发送数据
std::thread thread([&client]()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(2000));
	char* data = new char[4]{ 't','e','s','t' };
	char msg[255] = "Hello,I am Client";
	while (1) { **//线程中循环**
		uv::Packet packet;
		packet.pack(msg, sizeof(msg));
		client.write(packet.Buffer().c_str(), packet.PacketSize());; **//运行几秒后崩溃** 
		////线程安全;
		//client.writeInLoop(packet.Buffer().c_str(), packet.PacketSize(), **//用writeInLoop 会直接崩溃**
		//	[](uv::WriteInfo& info)
		//{
		//	//数据需要在发生完成回调中释放
		//	//write message error.
		//	if (0 != info.status)
		//	{
		//		//打印错误信息
		//		std::cout << "Write error :" << EventLoop::GetErrorMessage(info.status) << std::endl;
		//	}
		//	delete[] info.buf;
		//});


	}
});

#endif

from uv-cpp.

LoveChina-3000 avatar LoveChina-3000 commented on July 22, 2024

麻烦大佬帮忙看下,client跨线程循环发送时候,使用write发送,发送大概几秒后崩溃,使用writeInLoop直接崩溃

from uv-cpp.

wlgq2 avatar wlgq2 commented on July 22, 2024

packet变量在回调中已经释放,当然会崩。要么在把packet的数据memcopy到char*里面在回调里释放,要么把packet弄成指针,在回调里释放数据。或者把 packet移到while外面,回调里面别释放了

from uv-cpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.