Hi,
I wanna do something really simple.....but can't seem to find my way around it....I have three client programs connecting to one server client. I'm using fork() to create a new thread for each client that connects. The problem is i noticed that when one client changes a global float vairiable (or any vafriable for that matter)...it does not update for all the other clients. I tried t make the float variable static but its as if each of the clients have complelty seperated values.
I want to share that variable among the three processes..am i doing somthing wrong in my server? heres the code...
I have a global vaiable:
static float highest_bid = 50.00;
this is the code hat coems right after a client successfuly connects:
Code:
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd);
sprintf(str, "Welcome the current highest bid price for the new DVD player is %0.2f", highest_bid);
n = write(newsockfd,str,strlen(str));
CheckBid(newsockfd, inet_ntoa(cli_addr.sin_addr));
exit(0);
}
else close(newsockfd);
Here is the check bid function if you need to see it:
Code:
void CheckBid (int sock, char* cur_ip)
{
int n;
float bid;
char buffer[256];
char end_msg[256];
while(1)
{
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
time(&after);
if((after - before) > SETTIME)
{
sprintf(end_msg, "Sorry, Bidding is over and the winner is %s for $%0.2f", cur_ip, highest_bid);
n = write(sock,end_msg,strlen(end_msg));
}
else
{
bid = atof(buffer);
if( bid > highest_bid)
{
highest_bid = bid;
sprintf(end_msg,"Congrats: you have the highest bid at $%0.2f", bid);
n = write(sock,end_msg,strlen(end_msg));
}
else
{
sprintf(end_msg,"Sorry, the current highest bid is now $%0.2f", highest_bid);
n = write(sock,end_msg,strlen(end_msg));
}
}
}
}
thanx in advance
Boomba,