?
Solved

Unexplicable Socket problem - mysterious bug !!!

Posted on 2003-02-27
3
Medium Priority
?
260 Views
Last Modified: 2006-11-17
Hello!
I'm writing my first c/s program in Borland builder C++.I try to check how sockets work by sending a message of 10 characters through a socket from client to server.This message is typed by the user at run time in a TMemo on the client side.When the message is up to 9 characters, i've got no problem,the message is transmitted and displayed on my server as i want,ok!
But when the message is 10 characters long i get a mysterious display on my server:I get the 10 characters message+"listen".eg if i want to send 1234567890 i will see on my server :
    1234567890LISTEN

It's so strange, note that i use WSAGetLastError but it returns no errors!On my server the code is:
....
if (listen(ClientSocket,0)==SOCKET_ERROR)
        {
         Form1->Label2->Caption="ERROR AT LISTEN";
         closesocket(ClientSocket);
         WSACleanup();
         return;
         }

ServerSocket=accept(ClientSocket, (SOCKADDR *)&claddr, &claddrsize);

if (ServerSocket==INVALID_SOCKET)
        {
         Form1->Label1->Caption="ERROR AT ACCEPT";
         closesocket(ClientSocket);
         closesocket(ServerSocket);
         WSACleanup();
         return;
         }
else
{
  Form1->Label1->Caption="CLIENT CONNECTED!";

  y=recv(ServerSocket, buff, 10, 0);
 
   Form1->Label1->Caption=WSAGetLastError();

StaticText1->Caption=buff;
}
...
NOTE that when I remove from the code the line
  Form1->Label2->Caption="ERROR AT LISTEN";
and i run the program again i get this message on my server:
      1234567890ACCEPT
and when I also remove the line
  Form1->Label1->Caption="ERROR AT ACCEPT";
i get the message:
      1234567890ONNECTED!
and when I also remove the line
  Form1->Label1->Caption="CLIENT CONNECTED!";
i get the pure message
      1234567890
Strange!!!What's wrong?..
Also the code from the client is:
...
 int Size = Memo1->GetTextLen(); //Get length of string
                                  //in Memo1
  Size++;               //Add room for null character

  char *Buffer = new char[Size];  //Creates Buffer
                                 //dynamic variable
  Memo1->GetTextBuf(Buffer,Size); //Puts Memo1->Text into
                                  //Buffer
  send(ParamSocket,Buffer, 10,0);

  Label1->Caption=WSAGetLastError();
  delete Buffer;

I'm so confused!!! Could somebody help me?
0
Comment
Question by:classobaniera
[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
3 Comments
 

Author Comment

by:classobaniera
ID: 8032289
sorry i ve only got 25 points to give to this question!! But i would give 500 if i had...It's difficult to answer,i believe..
0
 
LVL 12

Accepted Solution

by:
Salte earned 100 total points
ID: 8034104
Question:

What is the length of the data you receive?

if you receive a socket with 10 bytes then you get only 10 bytes at the other end:

int len = recv(.....);

len should return 10.

I believe it is simply a matter of when length is less than 10 the code will add and also send the terminating null byte but when the length is exactly 10 or more, only 10 bytes are sent and so the receiving end does not get any null byte terminator and so it will attempt to deal with a string as if it had a null terminator when it doesn't.

so what do you do with that variable 'y' of yours?

A natural thing would be to make sure that at most 10 bytes of buff is handled as valid text.

so your:

StaticText1->Caption=buff;

is in error since buff does not have a terminating null byte when the length is exactly 10.

probably one way to solve it would be to have buff always at least 11 bytes and then always set:

buff[y] = 0; after recv is done.

Alf

0
 
LVL 11

Expert Comment

by:bcladd
ID: 9847328
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte: Grade A

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 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