The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
the given code goes in infinite loop and does not increment variable i mrityunjay22 Shell Programming and Scripting 6 12-26-2007 02:20 AM
Infinite Loop in Autosys while running a shell script, Manual run is fine sharmagaurav_2k Shell Programming and Scripting 2 09-04-2007 08:20 AM
BEA Enters the CEP Market with Weblogic Event Server iBot Complex Event Processing RSS News 0 08-09-2007 11:50 PM
ls command in infinite Loop umakant SUN Solaris 3 07-17-2007 01:25 AM
high priority thread contains an infinite loop rvan High Level Programming 0 02-14-2007 09:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-18-2007
arjunjag arjunjag is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 12
pick the bug the server enters an infinite loop

here is the server and client side code now there is a bug after which the server enters an infinite loop.the server is designed as an echo server and if it reads /q then the server closes while the client can send messages till /q now after the frst msg when another msg is send infinite loop is entered
Server code
Code:
 while(j!=0)
       {
	  memset(buffer,0,1024);/*clear buffer*/
	 n = read(newsockfd,buffer,1024);/*read from client*/
	 if (n < 0) perror("\n ERROR reading from socket \n");/*check for errors*/
	 len=strlen(buffer);
	 puts(buffer);/*print message by client*/
	 write(newsockfd,"\n Client wrote \n",12);/*write to client*/
	  n=write(newsockfd,buffer,sizeof(buffer));/*write to client*/
	 if(n<0)perror("Write Failed");
	  while(i<len)
	   {
	     //printf("%d\n",i);
	     j= strcmp(buf,&buffer[i]);
	     if(j==0)
	       break;
	     i+=2;
	   }
Client Code

Code:
while(j==0)
	  {
    printf("\n Enter Message for Server");
    gets(buf);
    /* now that we are connected, start writing to the socket */	
    /* till write() returns 0, meaning the server closed	*/
    /* the connection.				*/
    
    rc = write(s, buf,sizeof(buf)); 
          if(rc<0)
      {
	perror("write failed");
      }
	  else
	    {
	  read(s,buf1,12);
	  read(s,buf2,sizeof(buf));
	    }
	  puts(buf1);/*print messages from server*/
	  puts(buf2);/*print messages from server*/
	  printf("want to write more data");
	  gets(ans);
	  j=strcmp(ans,"yes");
	  break;
      }
      }

Last edited by vino; 07-18-2007 at 09:17 AM.. Reason: added code tags
  #2 (permalink)  
Old 07-18-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Code:
	 n = read(newsockfd,buffer,1024);/*read from client*/
	 if (n <= 0) 
	 {
	 	 perror("\n ERROR reading from socket \n");/*check for errors*/
	 	 break;
	 }
A way out of the loop when read returns zero or -1 would be good.
  #3 (permalink)  
Old 07-19-2007
arjunjag arjunjag is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 12
another bug

hey porter
the frst bug is fixed now another bug has come.if the client side after "want to send another message" types yes the server functions well but when apart from yes any other message is typed in the server again enters infinite loop.how to remove this
  #4 (permalink)  
Old 07-19-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,930
I could see that its not corrected for a snippet in the client code as well.

Use porter's point to address everywhere when expected return value is not returned from any of send / read

Code:
rc = write(s, buf,sizeof(buf)); 
          if(rc<0)
      {
	perror("write failed");
      }
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 11:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0