c++ - How to send and receive data up to SO_SNDTIMEO and SO_RCVTIMEO without corrupting connection? -
c++ - How to send and receive data up to SO_SNDTIMEO and SO_RCVTIMEO without corrupting connection? -
i planning how develop man in middle network application tcp server transfer info between server , client. behave regular client server , server remote client without modifying data. optionally used observe , measure how long server or client not able receive info ready received in situation when connection inactive.
i planning utilize blocking send , recv functions. before info transfer phone call setsockopt function set so_sndtimeo , so_rcvtimeo 10 - 20 miliseconds assuming forcefulness blocking send , recv functions homecoming in order allow active connection info routed. running thread per connection looks expensive. not utilize async sockets here because can not find guarantee finish in parts of sec when big info amount beingness sent or received. high info delays not good. utilize little buffers here calling function each received byte looks overkill.
my next assumption safe phone call send or recv later if has terminated timeout , info received less requested.
but confused contradicting info available @ msdn.
send function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740149%28v=vs.85%29.aspx
if no error occurs, send returns total number of bytes sent, can less number requested sent in len parameter.
sol_socket socket options
https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532%28v=vs.85%29.aspx
so_sndtimeo - timeout, in milliseconds, blocking send calls. default alternative zero, indicates send operation not time out. if blocking send phone call times out, connection in indeterminate state , should closed.
are assumptions right can utilize these functions this? maybe there more effective way this?
thanks answers
as previous poster has suggested, urge reconsider design of server employs asynchronous i/o strategy. may require spend important time learning each operating systems' preferred approach. time well-spent.
for other toy application, using blocking i/o in manner suggest not perform well. short timeouts, sounds me though won't able service new connections until have completed work current connection. may find (with short timeouts) you're burning more cpu time spinning waiting work doing work.
a previous poster wisely suggested taking @ windows i/o completion ports. take @ article wrote in 2007 dr. dobbs. it's not perfect, seek decent job of explaining how can design simple server uses little thread pool handle potentially big numbers of connections:
windows i/o completion ports http://www.drdobbs.com/cpp/multithreaded-asynchronous-io-io-comple/201202921
if you're on linux/freebsd/macosx, take @ libevent:
libevent http://libevent.org/
finally, good, practical book on writing tcp/ip servers , clients "practical tcp/ip sockets in c" michael donahoe , kenneth calvert. check out w. richard stevens texts (which cover topic unix.)
in summary, think should take time larn more asynchronous socket i/o , established, best-of-breed approaches developing servers.
feel free private message me if have questions downwards road.
c++ c sockets network-programming
Comments
Post a Comment