The UNIX and Linux Forums  

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
SugarCRM CEO John Roberts tells us how his company developed (video) iBot UNIX and Linux RSS News 0 04-18-2008 01:30 PM
New wrapper script will be developed to wrap two ksh scripts ishai82 Shell Programming and Scripting 3 03-17-2008 12:45 AM
How was the Gollum Developed? Legend986 Post Here to Contact Site Administrators and Moderators 2 12-09-2007 04:37 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2007
Registered User
 

Join Date: Jul 2007
Posts: 12
Stumble this Post!
Bug has developed

Guys there is another bug that has developed.........now messaged above 6 bytes are trimmed and echoed back to client.After the first message the echo is not regular.I have put a loop in both client and server to check for closing.Moreover for connection close i also need to put another clause that if there is a connection break in either server or client the connection should be closed from other side.Below are the server and clinet side codes after connection

Server
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);
      for(i=0;i<=len;i++)
	{
	  //strcpy(buf1,&buffer[i]);
	  //strcat(buf1,&buffer[i+1]);
	  k= strcmp("/",&buffer[i]);
	
	  if(k==0)
	    break;
	  break;
	}
	      for(p=i;p<=len;p++)
		{
		  j= strcmp("q",&buffer[p]);
		
	      if(j==0)
		{
		  close(newsockfd);
		  close(sockfd);
		  exit(0);
		  
		}
		}
	
	      puts(buffer);/*print message by client*/
	      write(newsockfd,"\n Client wrote",14);/*write to client*/
	      n=write(newsockfd,buffer,sizeof(buffer));/*write to client*/
	      if(n<0)perror("Write Failed");
	
      
    }
Client Side
Code:
	while(k!=0)
	  {
	    memset(&buf, 0, sizeof(buf));
    printf("\n Enter Message for Server");
    gets(buf);
    rc = write(s, buf,sizeof(buf)); 
     if(rc<0)
      {
	perror("write failed");
      }
    /* now that we are connected, start writing to the socket */	
    /* till write() returns 0, meaning the server closed	*/
    /* the connection.				*/
    len=strlen(buf);
    for(i=0;i<=len;i++)
      {
	//strcpy(b1,&buf[i]);
	//strcat(b1,&buf[i+1]);
	       
	l= strcmp("/",&buf[i]);
      
	if(l==0)
	  break;
	break;
      }
	    for(p=i;p<=len;p++)
	      {
	    k=  strcmp("q",&buf[p]);
	    if(k==0)
	      {
		close(s);
		exit (0);
	      }
	      }

	    read(s,buf1,14);
	    read(s,buf2,1024);
	    puts(buf1);/*print messages from server*/
	    puts(buf2);/*print messages from server*/
    
    
	  }
      }

Last edited by reborg; 07-23-2007 at 05:03 AM. Reason: code tags
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Stumble this Post!
Code:
if(k==0)
break;
break;
What is that supposed to achieve?

I suggest you don't use C++ style comments in C code. It's a bad habit for portable code.
Reply With Quote
  #3 (permalink)  
Old 07-23-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,660
Stumble this Post!
arjunjag, be aware of the rules. No homework questions. You are getting the whole college project done by the members of the forum.
Reply With Quote
  #4 (permalink)  
Old 07-23-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,572
Stumble this Post!
Very first request please use CODE tags

Code:
n = read(newsockfd,buffer,1024);/*read from client*/
if (n < 0) perror("\n ERROR reading from socket \n");/*check for errors*/
Why do you continue here even after an error ? ( after perror )
Do you want to continue even when n == 0

Code:
if(k==0)
break;
break;
Does that mean to break irrespective of the value of ' k '. Then why is that part of the code ? Anything specific here ?


Code:
if(j==0)
{
close(newsockfd);
close(sockfd);
exit(0);
}
Parent not waiting for the child to complete


Code:
write(newsockfd,"\n Client wrote",14);/*write to client*/
Flusing before writing and not after that.

Code:
n=write(newsockfd,buffer,sizeof(buffer));/*write to client*/
if(n<0)perror("Write Failed");
}

The same pattern where it continues processing even after write call returns a negative value


Code:
gets(buf);
This function would not check any buffer overrun.

try to use fgets.


Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0