• C

Client-Server Socket Program.

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?

I had tried to use break and return statements but wouldn't work for me. I want the server to keep waiting for more messages from the client and respond when the client is run again.

Following is the portion of the code from the server where the server reads and prints the message.

while (1) {


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

 }

Thanks.
raj27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

as2003Commented:
i'm not sure if this will help but if you use 'recv' instead of 'read' it will return 0 if the connection has been closed.
0
catbuttCommented:
I think read() does the same thing, so you should simply check for <= 0 rather than simply <.

However I recommend using recv(), since not all platforms treat sockets as files (that obscure platform called win32 comes to mind :) )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akshayxxCommented:
i guess u have posted this question somewhere else also?
0
havman56Commented:
see my site, i hosted a free socket program use it! if u need
http://micro_mangal.tripod.com


0
raj27Author Commented:
Please delete this question. I got a better solution elsewhere.
Thanks for the replies.
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.