How can my client send two queries (in two different terminals) to the server at the same time? When i try it, only one works, the other closes socket.
main ()
{
readData ();
int serverFd, clientFd, clientFd2,serverLen, clientLen;
struct sockaddr_un serverAddress;/* Server address */
struct sockaddr_un clientAddress; /* Client address */
struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
struct sockaddr* clientSockAddrPtr; /* Ptr to client address */
/* Ignore death-of-child signals to prevent zombies */
signal (SIGCHLD, SIG_IGN);
serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);
clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);
/* Create a socket, bidirectional, default protocol */
serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Set domain type */
strcpy (serverAddress.sun_path, "countries"); /* Set name */
unlink ("countries"); /* Remove file if it already exists */
bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
listen (serverFd, 5); /* Maximum pending connection length */
while (1) /* Loop forever */
{
/* Accept a client connection */
clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);
while (fork () == 0) /* Create child to send recipe */
{
int recvquery;
char countrynamereceivedquery[200];
while (recvquery=read(clientFd,countrynamereceivedquery,sizeof(countrynamereceivedquery)))
{
//print results
}
}
Thats my server program. I run it as a background process and then run client program which can search the textfile stored in an array in server. Right now, when i open two terminals and run the client at teh same time, one client quits program, the other client receives the jus-quit-client's query and searches the server. I did create two sockets but the client just quits in both terminals.