?
Solved

Please Help Urgent Client send() recv() problem

Posted on 2003-03-18
2
Medium Priority
?
181 Views
Last Modified: 2010-04-01
Question is changed a bit

Following is the code from my server program. As far as sending and receiving data is concerned, it does 'almost' right job. It does receive data without any problem from server side. However, when i send data from client to server, it sends it but it sends like 12 blank lines as well to the server. Can anyone tell me whats wrong?//

 memset(buffer, 0, sizeof(buffer));
memset(xbuffer, 0, sizeof(xbuffer));

unsigned long on = 1;
ioctlsocket(skt, FIONBIO, &on);


while (true)
{
       int bytesRead;
             bytesRead = recv(skt, xbuffer, sizeof(xbuffer), 0);

         if (0 == bytesRead)
              {
              cout << "Socket closed on other side" << endl;
              break;
              }
         else if(bytesRead >0)
              {
             
             cout << "<" << xbuffer << endl;
                      //cout << xbuffer;
              }

           
          if(_kbhit())
               {
                    cin.getline(buffer, sizeof(buffer));
                    buffer[strlen(buffer)] = '\0';
                    send(skt, buffer, sizeof(buffer)+1, 0);
                    cout << endl;
               }
               
                 Sleep(500);
}
0
Comment
Question by:azizs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
bsnh99 earned 500 total points
ID: 8163552
You're sending sizeof(buffer), but buffer only contains strlen(buffer) of valid data. Change the call to send to:

send(skt, buffer, strlen(buffer)+1, 0);
0
 
LVL 3

Expert Comment

by:cmaryus
ID: 8164558
yep, that's right, but it's ok if you do:
send(skt, buffer, strlen(buffer), 0); - send exactly the buffer size (without terminator '\0')
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question