Sponsored Content
Full Discussion: Socket Keep-Alive
Top Forums Programming Socket Keep-Alive Post 302971952 by Corona688 on Wednesday 27th of April 2016 12:30:34 PM
Old 04-27-2016
All right, this loop is suspect:

Code:
    while ((size = recv(sock, recv_buf,
        RECV_BUFFER_SIZE, 0)) > 0)
    {
        recv_buf[size] = '\0';
        response.append(recv_buf);
    }

From man recv:

Code:
       When a stream socket peer has performed an orderly shutdown, the return
       value will be 0 (the traditional "end-of-file" return).

So, your program downloads the entire page, but doesn't stop there -- it calls recv() one more time when the transfer is done, which hangs until the web server becomes impatient and kicks you. Which is mecifully much less than the many minutes TCP usually defaults to. Then your program declares the download finished, writes another request to the dead socket, and "receives" another EOF in reply.

Reading until EOF might make sense without keepalives, but obviously won't do for persistent connections -- TCP/IP doesn't have an EOF signal, just an end of connection signal. This is why the web server must warn you of the content's length somehow -- as a content-length header, as chunked sections preprended with lengths in hexadecimal, etc.

Last edited by Corona688; 04-27-2016 at 01:47 PM..
This User Gave Thanks to Corona688 For This Post:
 

9 More Discussions You Might Find Interesting

1. Programming

HTTP Keep-Alive socket problem

Hello everyone, I am a newbie in UNIX/Linux socket programming. This is a class project that I had trouble with. ================================================== I was trying to make “Keep-Alive” HTTP connections to the server in a tiny web crawler project. Here is the problem: when I tried... (0 Replies)
Discussion started by: imdupeng
0 Replies

2. UNIX for Dummies Questions & Answers

Socket still alive!!

Hi, I'm investigate about a problem regarding a connection between two server (S1 and S2). A client software on S1 made a pool of connections on S2; for some reason some connections end but sockets still alive on S2 and not on S1. I always knew about sockets as a pair of processes; is it... (1 Reply)
Discussion started by: grado
1 Replies

3. Shell Programming and Scripting

Can we keep our script alive while logging out.

Hi I want to keep my script running even when i am logged off from the box. can we run the script in background which can automatically run every hour? Please advise. Thank you (1 Reply)
Discussion started by: Prateek007
1 Replies

4. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

5. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

6. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

7. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

8. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

9. What is on Your Mind?

Is this forum alive?

Odd thing. I posted in the Solaris forum, new user, just asking for a bit of advice. Nothing too complicated. As of this post there have been 140 views and zero replies. So that got me thinking, is this normal? I had a look around, and I see the same thing on many other threads, and in other... (2 Replies)
Discussion started by: _JonB_
2 Replies
recv(3XNET)					   X/Open Networking Services Library Functions 				       recv(3XNET)

NAME
recv - receive a message from a connected socket SYNOPSIS
cc [ flag ... ] file ... -lxnet [ library ... ] #include <sys/socket.h> ssize_t recv(int socket, void *buffer, size_t length, int flags); DESCRIPTION
The recv() function receives a message from a connection-mode or connectionless-mode socket. It is normally used with connected sockets because it does not permit the application to retrieve the source address of received data. The function takes the following arguments: socket Specifies the socket file descriptor. buffer Points to a buffer where the message should be stored. length Specifies the length in bytes of the buffer pointed to by the buffer argument. flags Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the fol- lowing values: MSG_PEEK Peeks at an incoming message. The data is treated as unread and the next recv() or similar func- tion will still return this data. MSG_OOB Requests out-of-band data. The significance and semantics of out-of-band data are protocol-spe- cific. MSG_WAITALL Requests that the function block until the full amount of data requested can be returned. The function may return a smaller amount of data if a signal is caught, if the connection is termi- nated, if MSG_PEEK was specified, or if an error is pending for the socket. The recv() function returns the length of the message written to the buffer pointed to by the buffer argument. For message-based sockets such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message must be read in a single operation. If a message is too long to fit in the sup- plied buffer, and MSG_PEEK is not set in the flags argument, the excess bytes are discarded. For stream-based sockets such as SOCK_STREAM, message boundaries are ignored. In this case, data is returned to the user as soon as it becomes available, and no data is discarded. If the MSG_WAITALL flag is not set, data will be returned only up to the end of the first message. If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recv() blocks until a message arrives. If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() fails and sets errno to EAGAIN or EWOULDBLOCK. USAGE
The recv() function is identical to recvfrom(3XNET) with a zero address_len argument, and to read() if no flags are used. The select(3C) and poll(2) functions can be used to determine when data is available to be received. RETURN VALUES
Upon successful completion, recv() returns the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() returns 0. Otherwise, -1 is returned and errno is set to indicate the error. ERRORS
The recv() function will fail if: EAGAIN The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no EWOULDBLOCK out-of-band data is available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to await out-of-band data. EBADF The socket argument is not a valid file descriptor. ECONNRESET A connection was forcibly closed by a peer. EFAULT The buffer parameter can not be accessed or written. EINTR The recv() function was interrupted by a signal that was caught, before any data was available. EINVAL The MSG_OOB flag is set and no out-of-band data is available. ENOTCONN A receive is attempted on a connection-mode socket that is not connected. ENOTSOCK The socket argument does not refer to a socket. EOPNOTSUPP The specified flags are not supported for this socket type or protocol. ETIMEDOUT The connection timed out during connection establishment, or due to a transmission timeout on active connection. The recv() function may fail if: EIO An I/O error occurred while reading from or writing to the file system. ENOBUFS Insufficient resources were available in the system to perform the operation. ENOMEM Insufficient memory was available to fulfill the request. ENOSR There were insufficient STREAMS resources available for the operation to complete. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
poll(2), recvmsg(3XNET), recvfrom(3XNET), select(3C), send(3XNET), sendmsg(3XNET), sendto(3XNET), shutdown(3XNET), socket(3XNET), attributes(5), standards(5) SunOS 5.10 10 Jun 2002 recv(3XNET)
All times are GMT -4. The time now is 08:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy