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
2
u/[deleted] Jul 06 '23
I was hoping for your answer to be the solution, but unfortunately it wasn't..
However, i figured it out. I was passing a large 512 byte buffer to the recv call, apparently in tcp the recv call tries to use all the buffer unlike udp that fills it with the datagram message and then returns. I set the buffer to the size i used for the test, so "Hello, World!" (13) and it worked..