Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:37:09+00:00 2026-06-13T11:37:09+00:00

I have a problem with MailSlots in windows (C/C++). I am trying to make

  • 0

I have a problem with MailSlots in windows (C/C++).

I am trying to make two simple programs right now, but the communication is not good at the last step.

This is my int main from the server.cpp

    int main()
{
HANDLE      ss, sc, sc2r;
   LPCTSTR     errMsg;

   ss = CreateMailslot("\\\\.\\mailslot\\ss", 0, MAILSLOT_WAIT_FOREVER, NULL);
   if (ss == INVALID_HANDLE_VALUE) 
   {
       printf("Invalid ss value");
       return -1;
   }

   for (;;)
   {
       DWORD   msgSize;
       DWORD nr;
       BOOL    err;

       /* Get the size of the next record */
       err = GetMailslotInfo(ss, 0, &msgSize, 0, 0);
           char x[100];
           char nrr[10];

       if (msgSize != (DWORD)MAILSLOT_NO_MESSAGE)
       {
               DWORD   numRead;
               /* Read the record */
               err = ReadFile(ss, x, msgSize, &numRead, 0);
               int wrds=count(x)+1;
               sc = CreateFile("\\\\*\\mailslot\\sc", GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
               itoa(wrds,nrr,10);

               err = WriteFile(sc, nrr, sizeof(nrr), &nr, 0);
               //cout<<err<<endl;

               //cout<<x;

               //cout<<err;
               strrev(x);
               err=WriteFile(sc, x, sizeof(x), &nr, 0);
           }   
       }
  return(0);
}

Here is the client source:

int main()
{
   HANDLE      ss, sc, sc2;
   LPCTSTR     errMsg;
   BOOL        err;
   DWORD       numWritten;

   sc = CreateMailslot("\\\\.\\mailslot\\sc", 0, MAILSLOT_WAIT_FOREVER, NULL);
   ss = CreateFile("\\\\*\\mailslot\\ss", GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

   if (ss == INVALID_HANDLE_VALUE) 
   {
       printf("CreateFile failed. ");  
       // Close any mailslot we opened
       if (ss != INVALID_HANDLE_VALUE) CloseHandle(ss);
       return -1;
   }

   char x[100];
   char z[100];

   printf("Write the damn sentence:");
   cin.getline(x,100);
   err = WriteFile(ss, x, sizeof(x), &numWritten, 0);
   if (!err) printf("WriteFile failed. ");
   DWORD rd;

   ReadFile(sc,x,sizeof(x),&rd,NULL);
   cout<<x<<endl;
   ReadFile(sc,z,sizeof(z),&rd,NULL);
   cout<<z;

   return 0;
}

It seems like the server is sending the same thing three times. I tested the client in debugger and he gets it right, but can’t figure it out why the server is sending three times the same thing.

Do you have any suggestions ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:37:10+00:00Added an answer on June 13, 2026 at 11:37 am

    You are confusing sizeof with strlen. Calling sizeof(nrr) will always return 10. The server program will do a single write of 10 bytes, even if the buffer only contains 2 valid bytes.

    Replace sizeof with 1+strlen to fix the problem.

    For example, in server.cpp if wrds is 1, then nrr will be { 0x31, 0x00 } in memory. What looks like a repeated write is really a single write of uninitialzed memory. strlen will give you the count of valid characters, +1 for the terminating null.

    It might be a good idea to initialize nrr, with *nrr = 0 first. You can test that itoa succeeeded with if(*nrr) and handle the failure as you see fit.

    Oh, and one more thing : you are leaking handles. It might not matter much in the client, but server leaks a handle to a mailslot at every iteration. You should reuse the mailslot handle or close it at every iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
I have problem in my PHP code. I'm trying to make internet shop and
I have problem to make work a resource file. This is my simple code
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem in generating or getting decimal value in python. for now, I
I have problem with php's preg_match.I want to make something like this : When
I have problem with reading buffer, when reading is not completed. If the received
I have problem with Razor syntax and jQuery, I'm trying to append some items
I have problem in my xampp server php file not work if I write
I have problem to find a good namespace design and class order. Imagine we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.