• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

C/C++ Server Request Deadlock

Lang: C/C++
OS: Ubuntu
Compiler: g++
Interface to server: Firefox web browser
Problem: Loop Deadlocks

while ( ( n = recv(slave_socket, buffer_pointer, 255, 0) ) > 0) {
...prints to server terminal for time being...

buffer is 256, request is accepted, loop and browser deadlock
Browser continuely waits for a response, server continuely waits for packets.

Temporary Fix: buffer is 2048, replaced loop with if statement

if ( ( n = recv(slave_socket, buffer_pointer, 2047, 0) ) > 0 ) { ... }

Question: I would like to keep using the loop and fix the deadlocking, or atleast find another way so that the buffer can be dynamic so to accomidate any request and also to not waste space.
  • 2
1 Solution
Adrien de CroyCommented:
what type is n?

it needs to be signed, else a socket error (-1) will show as 4 billion, which is greater than zero.

It shouldn't matter what size the buffer is for a TCP socket.  If UDP, then you may be getting a socket error about the size of the message being bigger than the buffer you passed in.  There's a socket ioctl that can get the amount of data waiting on a socket buffer if you wanted to always read the entire amount.  Check the API docs for ioctlsocket(...)
TaplarAuthor Commented:
n is a signed int, there isn't an issue there.  ioctlsocket is a windows function, not a linux function.  I've looked for a reference to ioctl() to find the parameters to get how much data is in the socket waiting to be read, but I haven't found it yet.  Could you point me to a reference?
TaplarAuthor Commented:
I found the solution thanks to you.  I can use  ioctl(file_descriptor, FIONREAD, &byte_count_variable) to see how much data is waiting to be read and loop off of that.  Works great.  Thanks.

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now