?
Solved

C Sockets.

Posted on 2003-02-24
7
Medium Priority
?
196 Views
Last Modified: 2010-05-18
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
Comment
Question by:raj27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:raj27
ID: 8013152
Plz ignore the first statement in the while loop above. That has nothing to do with the problem I have.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8013374
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8014160
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:raj27
ID: 8016621
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
 
LVL 8

Accepted Solution

by:
akshayxx earned 75 total points
ID: 8022448
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 8022876
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
 

Author Comment

by:raj27
ID: 8614231
Could U plz delete this question. I got a more suitable answer from elsewhere.
Thanks to everyone for replying promply.
Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question