Jump to content

[SOLVED] How to receive message from UDP server?


photo

Recommended Posts

I'm learning the socket from the document, I made an UDP client, the send function is working well, but the receive function does't work. My UDP server is a third tool, so it can't be the server's problem. Please point out my mistakes, thanks! And this is my code.

QQ截图20230625192951.png

QQ截图20230625193127.png

Link to comment

Hello! 

16 hours ago, lgdnpt said:

but the receive function does't work.

can you tell in more detail what is happening - the application freezes? or the socket cannot read anything? or reads, but there is not the information that you expected?

 

is `reciveupdate` method called every frame?

you can try to add some checking and extralogs to understand what is going on

if (socket->isOpened())
{
	if (socket->isReadyToRead(1000 /*1 ms*/))
	{
		Log::message("ready to read!\n");

		int size = socket->readStream(blob, RECV_SIZE);
		if (size>0)
		{
			Log::message("read %d bytes!\n", size);
		// dosmth
		}
		else
		{
			Log::warning("read nothing :(\n");
		}
	}
	else
	{
		Log::message("z");
	}
}
else
{
	Log::error("socket closed!\n");
}

 

Link to comment
5 hours ago, cash-metall said:

Hello! 

can you tell in more detail what is happening - the application freezes? or the socket cannot read anything? or reads, but there is not the information that you expected?

 

is `reciveupdate` method called every frame?

you can try to add some checking and extralogs to understand what is going on

if (socket->isOpened())
{
	if (socket->isReadyToRead(1000 /*1 ms*/))
	{
		Log::message("ready to read!\n");

		int size = socket->readStream(blob, RECV_SIZE);
		if (size>0)
		{
			Log::message("read %d bytes!\n", size);
		// dosmth
		}
		else
		{
			Log::warning("read nothing :(\n");
		}
	}
	else
	{
		Log::message("z");
	}
}
else
{
	Log::error("socket closed!\n");
}

 

Thanks for your reply!

The detail is the application can't read any message, and "receiveupdate" method called every frame, I also make the application run in background, so the application doesn't freeze, but it can't work.

Link to comment
1 minute ago, lgdnpt said:

Thanks for your reply!

The detail is the application can't read any message, and "receiveupdate" method called every frame, I also make the application run in background, so the application doesn't freeze, but it can't work.

Sorry for my poor English expression,it's not "make", it's "I set the application can run in background".

Link to comment

have you tried adding the logs I mentioned above? can you show the output of the application?

Your code looks correct. How can i reproduce this behavior? what kind of 3rd party server: is this something private or can i download it?

Link to comment
20 hours ago, cash-metall said:

have you tried adding the logs I mentioned above? can you show the output of the application?

Your code looks correct. How can i reproduce this behavior? what kind of 3rd party server: is this something private or can i download it?

I tried add that, and here is the log:1591699089_QQ20230628150757.thumb.png.9344d1379358bd764cb4d417f3eb974c.png

this is my UDP server tool:1853616724_QQ20230628150500.png.b13067ffde17587d955145c44c85c142.pngNetAssist.exe

 

Link to comment
20 hours ago, cash-metall said:

have you tried adding the logs I mentioned above? can you show the output of the application?

Your code looks correct. How can i reproduce this behavior? what kind of 3rd party server: is this something private or can i download it?

I tried to use "socket->block();", when I click send button in the 3re party server, the unigine application appended a "z" character, so I think my application can receive the message, but it can't read the message.

Link to comment
2 minutes ago, lgdnpt said:

I tried to use "socket->block();", when I click send button in the 3re party server, the unigine application appended a "z" character, so I think my application can receive the message, but it can't read the message.

here is the video

2023-06-28_15-33-44.mkv

Link to comment

thanks for the help with replay!
i found the problem: you don't need to use  socket->bind when creating a socket.

this is my version 

SocketPtr socket;

#define SOCKET_BUFFER_SIZE 256
#define MAX_READ_UDP_SIZE 65535 // maximum possible UDP packet (IPv4, not Jumbo)
#define MAX_SEND_UDP_SIZE 1432	// default MTU, excluded IP and UDP headers


void encodePacket(const BlobPtr &blob)
{
	String s = "Helloworld!!!";

	blob->write(s.get(), s.size());

	Log::message("\n------- send %s -------\n", s.get());
}

void decodePacket(const BlobPtr &blob)
{
	Log::message("\n==== new message ====\n");

	const char *s = (const char *)blob->getData();
	Log::message("%s", s);

	Log::message("\n==== end message ====\n");
}

int AppSystemLogic::init()
{
	Engine::get()->setBackgroundUpdate(true);

	socket = Socket::create(Socket::SOCKET_DGRAM);

	if (!socket->open("127.0.0.1", 7777) ||
		!socket->recv(SOCKET_BUFFER_SIZE) ||
		!socket->send(SOCKET_BUFFER_SIZE) ||
		!socket->nonblock())
	{
		Log::error("can't create send UDP socket\n");
	}

	return 1;
}


int AppSystemLogic::update()
{
	if (Input::isKeyDown(Input::KEY_S))
	{
		BlobPtr packet = Blob::create();

		encodePacket(packet);

		if (packet->getSize() > MAX_SEND_UDP_SIZE)
			Log::warning("size more then MTU. blob can be segmented!\n");
		if (packet->getSize() > MAX_READ_UDP_SIZE)
			Log::error("size more then MAX UPD for IPv4\n");

		socket->write(packet->getData(), packet->getSize());
	}


	if (socket->isReadyToRead(100000 /*100 ms*/))
	{
		BlobPtr packet = Blob::create();
		int src_size = (int)socket->readStream(packet, MAX_READ_UDP_SIZE);
		if (src_size)
		{
			packet->seekSet(0);
			decodePacket(packet);
		}
	}

	// Write here code to be called before updating each render frame.
	return 1;
}

 

  • Like 1
Link to comment
22 hours ago, cash-metall said:

thanks for the help with replay!
i found the problem: you don't need to use  socket->bind when creating a socket.

this is my version 

SocketPtr socket;

#define SOCKET_BUFFER_SIZE 256
#define MAX_READ_UDP_SIZE 65535 // maximum possible UDP packet (IPv4, not Jumbo)
#define MAX_SEND_UDP_SIZE 1432	// default MTU, excluded IP and UDP headers


void encodePacket(const BlobPtr &blob)
{
	String s = "Helloworld!!!";

	blob->write(s.get(), s.size());

	Log::message("\n------- send %s -------\n", s.get());
}

void decodePacket(const BlobPtr &blob)
{
	Log::message("\n==== new message ====\n");

	const char *s = (const char *)blob->getData();
	Log::message("%s", s);

	Log::message("\n==== end message ====\n");
}

int AppSystemLogic::init()
{
	Engine::get()->setBackgroundUpdate(true);

	socket = Socket::create(Socket::SOCKET_DGRAM);

	if (!socket->open("127.0.0.1", 7777) ||
		!socket->recv(SOCKET_BUFFER_SIZE) ||
		!socket->send(SOCKET_BUFFER_SIZE) ||
		!socket->nonblock())
	{
		Log::error("can't create send UDP socket\n");
	}

	return 1;
}


int AppSystemLogic::update()
{
	if (Input::isKeyDown(Input::KEY_S))
	{
		BlobPtr packet = Blob::create();

		encodePacket(packet);

		if (packet->getSize() > MAX_SEND_UDP_SIZE)
			Log::warning("size more then MTU. blob can be segmented!\n");
		if (packet->getSize() > MAX_READ_UDP_SIZE)
			Log::error("size more then MAX UPD for IPv4\n");

		socket->write(packet->getData(), packet->getSize());
	}


	if (socket->isReadyToRead(100000 /*100 ms*/))
	{
		BlobPtr packet = Blob::create();
		int src_size = (int)socket->readStream(packet, MAX_READ_UDP_SIZE);
		if (src_size)
		{
			packet->seekSet(0);
			decodePacket(packet);
		}
	}

	// Write here code to be called before updating each render frame.
	return 1;
}

 

it works! Thank you very very much!

  • Like 1
Link to comment
  • bmyagkov changed the title to [SOLVED] How to receive message from UDP server?
×
×
  • Create New...