lgdnpt Posted June 25, 2023 Posted June 25, 2023 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.
cash-metall Posted June 26, 2023 Posted June 26, 2023 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"); }
lgdnpt Posted June 26, 2023 Author Posted June 26, 2023 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.
lgdnpt Posted June 26, 2023 Author Posted June 26, 2023 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".
cash-metall Posted June 27, 2023 Posted June 27, 2023 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?
lgdnpt Posted June 28, 2023 Author Posted June 28, 2023 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: this is my UDP server tool:NetAssist.exe
lgdnpt Posted June 28, 2023 Author Posted June 28, 2023 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.
lgdnpt Posted June 28, 2023 Author Posted June 28, 2023 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
cash-metall Posted June 28, 2023 Posted June 28, 2023 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; } 1
lgdnpt Posted June 29, 2023 Author Posted June 29, 2023 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! 1
bmyagkov Posted June 29, 2023 Posted June 29, 2023 31 minutes ago, lgdnpt said: it works! Thank you very very much! You're welcome!
Recommended Posts