?
Solved

Client-Server Socket Program.

Posted on 2003-02-25
5
Medium Priority
?
786 Views
Last Modified: 2011-10-03
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.
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
5 Comments
 
LVL 4

Expert Comment

by:as2003
ID: 8019466
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
 
LVL 1

Accepted Solution

by:
catbutt earned 60 total points
ID: 8019996
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8022661
i guess u have posted this question somewhere else also?
0
 
LVL 4

Expert Comment

by:havman56
ID: 8126058
see my site, i hosted a free socket program use it! if u need
http://micro_mangal.tripod.com


0
 

Author Comment

by:raj27
ID: 8614254
Please delete this question. I got a better solution elsewhere.
Thanks for the replies.
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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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