d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Client/server Problems (pthreads)
Add Reply New Topic New Poll
Member
Posts: 22,878
Joined: Jun 30 2008
Gold: 1,065.00
Nov 10 2012 06:33pm
Im having trouble where my client wont output whats in the buffer (character array) until it receives another message from the server.

The server appends this 2nd message to the 1st, so it appears to be putting the message into the server buffer and not sending it, then sending it when the next message is sent.

MSGGET function:

Code
if(strcmp(tmpInput,"MSGGET\n") == 0)
 {
  if(totnumMessages > 0)   //There must be at least 1 message in order to send a message back
  {
   inFile.open("MessageOfTheDay.txt");
     if( !inFile ) { // file couldn't be opened
          cout << "Error: file could not be opened" << endl;
        }
   cout << tmpInput;
   /*convert c++ string from array back to c-string in order to send to client*/
   while(inFile)
   {
    getline (inFile, line);
         if (lineno == currentOutMessage)
         {
     tmpString = line;      
     strcpy(tmpMsg, tmpString.c_str());
     send (childSocket, tmpMsg, strlen(tmpMsg) + 1, 0);
     memset(&tmpMsg[0], 0, sizeof(tmpMsg));
    }
    lineno++;
   }
   inFile.close();
   if(currentOutMessage == (totnumMessages-1))   //Loop through messages
   {
    currentOutMessage = 0;
   }
   else
   {
    currentOutMessage++;
   }
  }
  else
  {
   strcpy(tmpMsg,"There are currently no messages.\n");
   send (childSocket, tmpMsg, strlen(tmpMsg) + 1, 0);
   memset(&tmpMsg[0], 0, sizeof(tmpMsg));
  }


Client print method:

Code
if (recv(s, buf, sizeof(buf), 0) > 0) {
 cout << buf;
 memset(&buf[0], 0, sizeof(buf));
           }
Member
Posts: 22,878
Joined: Jun 30 2008
Gold: 1,065.00
Nov 12 2012 11:25am
I figured it out, I wasnt sending a newline character from the server to the client, thus cout wasnt being flushed by the client.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll