help please (socket + mysql + php)

help please (socket + mysql + php)

Post by danu kusman » Sun, 29 Oct 2000 02:50:18

I tried to used client server program to have a real time data on my web. On
the server side I used C and on the client side I used PHP. In the server
program I included the query action from MySQL and passed it over to the
send(). These two program works but .....

1. Why if I didn't used fork() I cannot used the server program again; the
message said the bind address is already in used? If the program still
running can I kill it?(I used FreeBSD)
2. I used fork() to handle multiple client. Is it better to use select() or
poll() rather than fork()?
3. When the server program I used, the first client works fine, but the
second client I tried was only works for one loop and it stop to receive the
query again but still connected to the socket. When I ran the 3rd etc the
client program works fine. Why this is happened?
4. I get error message from the server program that told me the get
swap_space failed and if this problem repeated the MySQL is being killed.
Can anyone explained to me what's going on?
5. Do I have to used the "ping-pong" like program to get these programs
works or the client doesn't have to send() repeatedly?
6. Why I cannot used the sleep() int the client? The page didn't show me

/* server */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <mysql.h>
#include <signal.h>

#define SERVER_PORT 5656
#define MAX_PENDING 5
#define MAX_LINE    256
#define MAX_KOLOM   16

MYSQL mysql;

void exiterr(int exitcode) {
 fprintf(stderr,"%s\n", mysql_error(&mysql));


void sig_chld(int signo){
 pid_t pid;
 int stat;

while ( (pid = waitpid(-1, &stat, WNOHANG) ) > 0)
          printf("child %d terminated\n", pid);


    uint i = 0;
    uint d = 0;
    uint e = 1;
    int kolom;
    struct sockaddr_in sin;
    char buf[MAX_LINE];
    int len;
    int serverSocket, clientSocket;
    void sig_chld(int);
    pid_t childpid;

    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_port = htons(SERVER_PORT);

 if (!(mysql_connect(&mysql,"host","password","")))

 if (mysql_select_db(&mysql,"database"))
    /* setup passive open */
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("server: socket");
 printf("SUKSES 1");

if ((bind(serverSocket, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
         perror("server: bind");


listen(serverSocket, MAX_PENDING);
signal (SIGCHLD, sig_chld);

    /* wait for connection, then receive and print text */
    while (1) {
         if ((clientSocket = accept(serverSocket, (struct sockaddr *)&sin,
&len)) < 0) {
             perror("server: accept");
         if (fork() == 0) {  /* child process */
            close (serverSocket);  /* child does not need it  */
            while (len = recv(clientSocket, buf, sizeof(buf), 0)) {
               if (mysql_query(&mysql,"SELECT * FROM TableName"))

               if (!(res = mysql_store_result(&mysql)))

               while((row = mysql_fetch_row(res))) {
                    kolom = 0;
                    for (i = 0; i < mysql_num_fields(res); i++)

                         if (kolom == MAX_KOLOM) {
                              kolom = 0;
                              buf[MAX_LINE-1] = 0;
                              len = strlen(buf) + 1;
                              send(clientSocket, buf, len, 0);
                              recv(clientSocket, buf, sizeof(buf), 0);
           buf[MAX_LINE-1] = 0;
           len = strlen(buf) + 1;
           send(clientSocket, buf, len, 0);


/* client */
                                 error_reporting (E_ALL);

                                 echo "<h2>TCP/IP Connection</h2>\n";

                                 $service_port = 5656;

                                 /* Get the IP address for the target host.
                                 $address = "";

                                 /* Create a TCP/IP socket. */
                                 $socket = socket (AF_INET, SOCK_STREAM, 0);
                                 if ($socket < 0) {
                                     echo "socket() failed: reason: " .
strerror ($socket) . "\n";
                                 } else {
                                     "socket() successful: " . strerror
($socket) . "\n";

                                 echo "Attempting to connect to '$address'
on port '$service_port'...";
                                 $result = connect ($socket, $address,
                                 if ($result < 0) {
                                     echo "connect() failed.\nReason:
($result) " . strerror($result) . "\n";
                                 } else {
                                     echo "OK.\n";

                                 $in = "HEAD / HTTP/1.0\r\n\r\n";
                                 $out = '';

                                 echo "Sending HTTP HEAD request...";
                                 write ($socket, $in, strlen ($in));
                                 echo "OK.\n";

                                 echo "Reading response:\n\n";
                                 while (read ($socket, $out, 2048)) {
                                    echo ("<br><br>");
                                    read ($socket, $out, 2048);
                                    echo $out;
                                    echo "\n";
                                    write ($socket, $in, strlen ($in));
                                    //sleep (1);

                                 echo "Closing socket...";
                                 close ($socket);
                                 echo "OK.\n\n";


1. Sigterm notice Apache / PHP / Mysql / LAN please help


I'm having the following problem:

I installed: (LINUXBOX)
REDHAT (telnet/ftp etc...)
Apache Server

I also have a WIN98 SE System (WINBOX)
I would like to have access from the WINBOX ( to the LINUXBOX
( apache server to use PHP/MySQL
I have 2 network carts installed in the systems, and connected via Crossover

I can telnet / FTP / PING in to the linuxbox from the winbox.
But i can't get access to the http server !!!
On the linuxbox i can have http access, when i fire up Netscape and use
http://linuxbox/ OR i get the apache welcome screen
test.php also works fine...

In the apache error_log it says:
[notice] caught SIGTERM, shutting down
[notice] Apache/1.3.12 (UNIX) PHP/4.0b4pl1 configured --
resuming normal operations

Any idea's ?? PLEASE help...


2. first/second/third wo

3. PHP/MySQL "mysql.sock" not found ???

4. xterminal

5. weird php error when using php-mysql-4.2.3 from ports

6. ls -l question

7. PHP + MySQL - apparently installed OK - but no MySQL support!

8. linux TCP window size broken?

9. PHP+MySQL vs. PHP+PostgreSQL

10. HELP: HELP: Redhat 7.1 + Apache + PHP + MySQL + PgSQL

11. Can't connect to local MySQL server through socket '/tmp/mysql.sock'