pick the bug the server enters an infinite loop


 
Thread Tools Search this Thread
Top Forums Programming pick the bug the server enters an infinite loop
# 1  
Old 07-18-2007
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 10:17 AM.. Reason: added code tags
# 2  
Old 07-18-2007
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  
Old 07-19-2007
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  
Old 07-19-2007
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");
      }

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies

2. Shell Programming and Scripting

how do I infinite loop (reconnect) to auto disconnect telnet server

I want the text based Star Wars movie at towel.blinkenlights.nl to loop infinitely. It plays the movie and then disconnects my session? Can anyone think of a way to make my unix machine automatically reconnect over and over? EDIT no commands are required are the connection its just in and... (3 Replies)
Discussion started by: herot
3 Replies

3. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

4. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

5. Shell Programming and Scripting

Call a infinite loop

Hi All, I need to run an infinite loop. requirement below: function1 --> creates a file file1 function2 ---> need to call if the file creates i am running these both function via a script --> script.sh i need to run the function1 first and if the file file1 creates then need to run the... (3 Replies)
Discussion started by: satyaranjon
3 Replies

6. UNIX for Advanced & Expert Users

Procmail and infinite loop

I wanted to copy (not forward but copy) all incoming email to another address of mine. It worked, but now I encountered an infinite loop problem: When the second address doesn't like the content and bounces the message back, the bounce message will be sent back and forth. So, what I have in... (1 Reply)
Discussion started by: distill
1 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

9. Solaris

ls command in infinite Loop

Hi, whenever I am giving a 'ls' command system is going into infinite loop displaying the current home directory. There is no separate shell script/file with ls name anywhere in the system. I am using Solaris 10. Any help / guidance in solving this problem is highly appreciated. ... (3 Replies)
Discussion started by: umakant
3 Replies
Login or Register to Ask a Question