C/C++ Server Request Deadlock

Posted on 2007-07-23
Last Modified: 2013-11-19
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.
Question by:Taplar
    LVL 3

    Accepted Solution

    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(...)
    LVL 2

    Author Comment

    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?
    LVL 2

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    Accessing elements inside an iframe 5 50
    Problem to Popup 37 61
    Amazon Product image url. 12 25
    Video will not go into background 20 16
    You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    The viewer will learn how to dynamically set the form action using jQuery.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now