UDP Confusion c++ sockets

Started by
33 comments, last by hplus0603 5 years, 8 months ago
3 hours ago, _WeirdCat_ said:

 Well im checking whenever fd is ready for writing then i do while loop until whole message(a string with x characters (may be lets say 2000 chars)) is written with write so there could be a problem

I've lost track, do you have the sockets set to blocking or non-blocking mode?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

Since i couldnt figgure how to avoid disconnection while using nonblocking mode ive changed the cise to use blocking ones with a bit of improvement :x

For nonblocking case: I dont write more bytes than i want, and write them only when socket is ready for write but it still disconnects...

4 hours ago, _WeirdCat_ said:

 For nonblocking case: I dont write more bytes than i want, and write them only when socket is ready for write but it still disconnects...

Are you stopping writing as soon as the return value of write() is either less than the size you requested, or -1 with errno set to EAGAIN / EWOULDBLOCK?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Actually when it returns -1 i repeat write until all is written so this is a bug, additionally whenever write returns 0 or more (i dont check if its less than requested) then i repeat write intil all is written so heres another bug then


	while (!sentall)
	{
		iteration++;
		if (count >= cmdlen) break;
		if (iteration > 1500) break;
		if (len_to_write <= 0) break; //we shouldnt even be here but ill add log exception
		int wb = write(sockfd, &pdata[ count ], size_t( len_to_write ));
		if (wb < 0) 
		{
			fails = fails + 1;
			if (fails >= max_write_fails)
		break;
		}
		if (wb >= 0)
		{
		count = count + wb;
		len_to_write = len_to_write - wb;
//		ALOG("WROTE: "+IntToStr(wb)+" bytes");
		}
		if (count >= cmdlen) { sentall = true; break; }
	}

So from what you said i whenever write is -1 or less than write bytes requested i do another select and check if socket isbready to write and then write rest of data?

Well thats a bit overcomplicated but if its worth it ill try.

I'm late to this thread, but the heading says "UDP."

UDP is not like TCP. The sendto() call either takes the full packet, or nothing at all. A fractional packet doesn't make sense.

Also, write() in general doesn't make sense for UDP. So maybe you switched to TCP somewhere in this thread?

Finally, just spin-looping when a non-blocking socket returns "not ready" is not helpful; the CPU is much faster than the network. The thing you need to do is either put your data in a queue for later delivery, or immediately decide to drop it. (If you jammed the send buffer full already, you're sending faster than the network can deliver, anyway.)

 

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement