r/linux_programming • • Jul 06 '23
TCP socket
Hi all. I have a question for you.
I've just implemented a very simple test for TCP sockets. I wrote an application that spawns two threads.
One is the server and creates a TCP socket, binds it to the loopback interface (and port), marks it as listening socket with a listen call, blocks on accept, and then calls recv to get data.
The other thread is the client, which creates a socket, performs a connect to the other, sends a string buffer "Hello, World!" and terminates.
In UDP this works, but in TCP it doesn't (the server blocks on recv indefinitely), UNLESS in the client (transmitter in this case) i call close() right after the send(). It acst like "uh, so you want close already? Ok..so i process all data.." , the recv returns and i see the string on the server...
How can i set the sockets to process the data always even if there's just one byte to process?
Many thanks
View all comments
Show parent comment
1
u/[deleted] Jul 07 '23
Thank you for the detailed answer. I tried using the nonblock flag and it returned with the data even using a large buffer. I'm understanding that TCP and UDP are completely different protocols. The first is stream based and the second is message based. Trying to consider them as the same is a mistake.