LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 04-03-2012, 06:21 AM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
How prevent lighttpd sharing persistent sockets for different requests arm embedded


I want to have persistent socket to send and receive data to a program written in C language on embedded linux on arm9 processor (this program has no problem in making and serving socket I tested it with other applications)
this is my PHP script:
PHP Code:

<meta http-equiv="refresh" content="2;url=?">
<?php
error_reporting
(0);
@
ini_set(&#65533;display_errors�, 0);
$fp pfsockopen("192.168.1.30",7778$errno$errstr30);
stream_set_blocking $fp ) ;
if (
$fp) {
    
$out "Device-1;";
    
fwrite($fp$out);
    
$out_put fread($fp2048);
    echo 
$out_put ;    
}else{
    echo 
'0000';
}
?>
as you see it reloads every 2 seconds. I used pfsockopen to prevent closing socket due to the PHP script exit and each user on different PCs has its own persistent opened socket because of some reasons in my project,The above code works fine with apache and every user hasits own persistent socket with no problem, the problem rises when I run this script in lighttpd with fastCGI enabled.
lighttpd opens ONE persistent socket for all users (web pages on different PCs) , lighttpd share this persistent socket for all of them and does not open new socket for different web pages on different or same PCs and all of them read each others data and sends their own data to one opened socket .
and my C sample file is:
Code:
  memset(&hints, 0, sizeof (hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    while (1)
    {
        if ((rv = getaddrinfo(NULL, LISTENPORT, &hints, &servinfo)) != 0)
        {
            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
            sleep(2);
        }
        else
        {
            break;
        }
    }
    // loop through all the results and bind to the first we can
    while (1)
    {
        for(tmpaddrinfo = servinfo; tmpaddrinfo != NULL; tmpaddrinfo = tmpaddrinfo->ai_next)
        {
            if ((sockfd = socket(tmpaddrinfo->ai_family, tmpaddrinfo->ai_socktype,tmpaddrinfo->ai_protocol)) == -1)
            {
                perror("server: socket");
                continue;
            }
            if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
            {
                    perror("setsockopt");
                    exit(1);
            }
            if (bind(sockfd, tmpaddrinfo->ai_addr, tmpaddrinfo->ai_addrlen) == -1) {
                close(sockfd);
                perror("server: bind");
                continue;
            }
            break;
        }

        if (tmpaddrinfo == NULL)
        {
            fprintf(stderr, "server: failed to bind\n");
            sleep(1);
        }
        else
        {
            break;
        }
    }

    freeaddrinfo(servinfo); // all done with this structure

    if (listen(sockfd, MAXLISTENQ) == -1)
    {
        perror("listen");
        exit(1);
    }

    printf("server: waiting for connections...\n");

    while(1)
    {  // main accept() loop
        sin_size = sizeof (their_addr);
        printf("server: going to  accept connections...\n");
        newfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);

            printf("server: connections accepted:%d on port %d\n",newfd,MainHandler->listenport);
            Delete_unusednewfds_from_last_requests(MainHandler);
            printf("server: Delete_Request passed accepted\n");
            if (newfd == -1)
            {
                perror("accept");
                continue;
            }
            else
            {
                inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s, sizeof s);
                printf("server: got connection from %s on socket %d\n", s,newfd);
                ADD_newfd_to_link_list_requests(MainHandler,newfd);
            }

    }
my C program is multithreaded program and ADD_newfd_to_link_list_requests and Delete_unusednewfds_from_last_requests add and delete threades from list.
and this is my log file from C program when I open ONE web page with script above:
Quote:
server: going to accept connections...
server: connections accepted:7 on port 7778
Delete_Request
server: got connection from 192.168.1.30 on socket 7
D-1; 7 port:7778
D-1; 7 port:7778
D-1; 7 port:7778
D-1; 7 port:7778
D-1; 7 port:7778
but when I open SECOND Script on different PC ,my C program does bot receive any request to open new socket but it receives what the second web pages send on same socket that opened before "7 on port 7778":
Quote:
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
D-1;D-1; 7 port:7778
as you can see "D-1;" comes twice in socket 7 after I opened second web page.
I have to say again that I have no problem with this C program and php script on apache web server but this problem rises on lighttpd .
How can I prevent this problem? which configuration should I change for what to enable one socket for each session ??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent lighttpd and fastCGI to share persistent sockets for different session golden_boy615 Programming 2 03-02-2012 01:11 AM
how to limit php-cgi processes in lighttpd web server on embeddedb arm linux golden_boy615 Linux - Server 4 01-04-2012 02:39 AM
how to limit php-cgi processes in lighttpd web server on embeddedb arm linux golden_boy615 Linux - Server 0 12-14-2011 02:18 AM
Prevent stealing of sockets raghu2383 Linux - Networking 2 09-20-2008 02:34 PM
C(sockets):how to implement persistent and pipeline supermyself Programming 0 06-09-2005 12:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration