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(�display_errors�, 0);
$fp = pfsockopen("192.168.1.30",7778, $errno, $errstr, 30);
stream_set_blocking ( $fp , 0 ) ;
if ($fp) {
$out = "Device-1;";
fwrite($fp, $out);
$out_put = fread($fp, 2048);
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 ??