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
anything.
/* 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;
MYSQL_RES *res;
MYSQL_ROW row;
void exiterr(int exitcode) {
fprintf(stderr,"%s\n", mysql_error(&mysql));
exit(exitcode);
pid_t pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG) ) > 0)
printf("child %d terminated\n", pid);
return;
main()
{
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","")))
exiterr(1);
if (mysql_select_db(&mysql,"database"))
exiterr(2);
/* setup passive open */
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("server: socket");
exit(1);
}
printf("SUKSES 1");
if ((bind(serverSocket, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
perror("server: bind");
exit(1);
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");
exit(1);
}
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"))
exiterr(3);
if (!(res = mysql_store_result(&mysql)))
exiterr(4);
while((row = mysql_fetch_row(res))) {
strcpy(buf,"");
kolom = 0;
for (i = 0; i < mysql_num_fields(res); i++)
sprintf(buf,"%s|%s",buf,row[i]);
kolom++;
if (kolom == MAX_KOLOM) {
sprintf(buf,"%s\n",buf);
kolom = 0;
buf[MAX_LINE-1] = 0;
len = strlen(buf) + 1;
send(clientSocket, buf, len, 0);
recv(clientSocket, buf, sizeof(buf), 0);
strcpy(buf,"");
}
}
}
strcpy(buf,"\n");
buf[MAX_LINE-1] = 0;
len = strlen(buf) + 1;
send(clientSocket, buf, len, 0);
}
close(clientSocket);
}
}
<?php
error_reporting (E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
$service_port = 5656;
/* Get the IP address for the target host.
*/
$address = "192.168.0.209";
/* 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,
$service_port);
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";
?>