Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Client-Server Socket Program.

Posted on 2003-02-25
5
Medium Priority
?
790 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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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 opening and reading files in the C programming language.
Suggested Courses

581 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