HI
I need to implement a client/server TCP application. the customer is the client and the bartender is the server.
When the customer enter the Bar, client connects to the server
Server should reply the client immediately. Other wise if the server is busy, it should send an update message every 10 seconds.
Depending on which message replied by the server:
10 seconds expired, no reply: client leave, that is disconnect
server is busy update: client reply --waiting or "leave and disconnect"
select beer: client reply -- light or black
select quantity: client reply liter or bottle, then disconnect
I have done the part before 'connection', but i don't know how to complement the function about read() and write(), so, please help me have a look at it, many thanks!!
#include <sys/wait.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int socket1,n;
int blond=0 , brun=0;//string buf;
char buffer[256];
if(argc<2) {
printf ("usage socket_clt \n");
exit(0);
}
printf("client1 is coming\n");
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
bzero(&from, sizeof(from));
from.sin_family=AF_INET;
from.sin_port=htons(5000); //lisening 5000
from.sin_addr.s_addr=INADDR_ANY; //address of local computer
/*creat a socket*/
if (( socket1=socket(AF_INET, SOCK_STREAM,0))<0)
{
printf("socket error ourverture\n");exit(0);
}
if (connect(socket1, (struct sockaddr*)&from, sizeof(from))<0)
{
printf("socket error connection\n");
exit(0);
}
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(socket1,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(socket1,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
I need to add a function of 'timeout' on read(),but i don't know how to do that.
thus how can i add a circle to implement many customers entering?