• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

C Sockets.

I am writing a simple client-server application,using C sockets, sending 4 messages across from the client to the server and printing them at the server. After receiving and printing the final message, the server program goes into an infinite loop, repeatedly printing the final message. Meanwhile, the client program terminates by closing the socket, after sending the final message. How can I stop the server from going into that infinite loop and why at all is that happenning? Following is the portion of the code from the server where the server reads and prints the message.

 while (1) {

   r_sa_l = sizeof(r_sa);


   if (read(u, &Pack, sizeof(Pack)) < 0)
     report_error("read");
 printf("\nData read from source %d \n",Pack.Source_Node);
 printf("\nMessage: %s----%d\n", Pack.Message,Pack.seqnum);

  }

Thanks.
0
raj27
Asked:
raj27
  • 3
  • 2
  • 2
1 Solution
 
raj27Author Commented:
Plz ignore the first statement in the while loop above. That has nothing to do with the problem I have.
0
 
akshayxxCommented:
with minimal changes in your program (see the else block).. this should avoid infinite printing.. btw so many things to be done when client closes .. like to close the server side of the socket as well..

while (1) {

  r_sa_l = sizeof(r_sa);


  if (read(u, &Pack, sizeof(Pack)) > 0)
{
printf("\nData read from source %d \n",Pack.Source_Node);
printf("\nMessage: %s----%d\n", Pack.Message,Pack.seqnum);
}
 else   report_error("read");
 }



0
 
ssnkumarCommented:
Try this one:

while (1) {
  r_sa_l = sizeof(r_sa);

  if (read(u, &Pack, sizeof(Pack)) < 0){
    report_error("read");
    break;
  }

  printf("\nData read from source %d \n", Pack.Source_Node);
  printf("\nMessage: %s----%d\n", Pack.Message, Pack.seqnum);
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
raj27Author Commented:
akshayxx, Ur solution works fine but for just one session b/w the sever & the client. Once all the 4 messages are received, the server immediately stops by reporting a read error. I want the server to continuously wait for the client, and become active whenever the client sends a message, still not going into any infinite loop.

Thanx.
0
 
akshayxxCommented:
well for that case .. as soon as u get a connection on accept .. fork the process and in the child process ..do the processing of talking to the client..
guess u need simple server implementation using fork()
0
 
ssnkumarCommented:
Then your server algorithm will be like this:
1. sd = socket
2. bind
3. listen
4. Do for ever
5.   new_sd = accept(sd,...)
6.   fork
7.   if (child)
8.      do all the processing, close new_sd and finally exit.
9.   if (parent)
10.     close socket new_sd.
11. End of Do loop.

I think you can use the above algorithm and do the coding to get a concurrent server (I think that's what you are asking for).

-Narendra
0
 
raj27Author Commented:
Could U plz delete this question. I got a more suitable answer from elsewhere.
Thanks to everyone for replying promply.
Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now