The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
[ HOME | TOPICS | INDEX | WEB RESOURCES | BOOKS | CONTRIBUTE | SEARCH | FEEDBACK | FAQ | FORUMS ]
FreeBSD Support
 New Topic  |  Go to Top  |  Go to Topic  |  Search  |  Log In   Previous Message  |  Next Message 
 socket programming--help please
Author: danu kusmana 
Date:   30-10-00 19:46

First of all apologize for my bad english; be patients to write my problems
yah :-)
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
", mysql_error(&mysql));
exit(exitcode);
}

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

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

int
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);
}
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");
exit(1);
}
if ((sig_chld = 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);
kolom++;
if (kolom == MAX_KOLOM) {
sprintf(buf,"%s
",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,"
");
buf[MAX_LINE-1] = 0;
len = strlen(buf) + 1;
send(clientSocket, buf, len, 0);
}
close(clientSocket);
}
}
}

/* client */
<?php
error_reporting (E_ALL);

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

$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) . "
";
} else {
"socket() successful: " . strerror
($socket) . "
";
}

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

$in = "HEAD / HTTP/1.0

";
$out = '';

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

echo "Reading response:

";
while (read ($socket, $out, 2048)) {
echo ("<br><br>");
read ($socket, $out, 2048);
echo $out;
echo "
";
write ($socket, $in, strlen ($in));
file://sleep (1);
}

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

";
?>









 Reply To This Message  |  Forum List  |  Flat View   Newer Topic  |  Older Topic 

 Topics Author  Date
 socket programming--help please   new
danu kusmana 30-10-00 19:46 
 RE: socket programming--help please   new
Jordan Block 31-10-00 21:00 


 Forum List  |  Need a Login? Register Here 
 User Login
 User Name:
 Password:
 Remember my login:
   
 Forgot Your Password?
Enter your email address or user name below and a new password will be sent to the email address associated with your profile.
How to get the most out of the forum

phorum.org