• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Unexplicable Socket problem - mysterious bug !!!

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
classobaniera
Asked:
classobaniera
1 Solution
 
classobanieraAuthor Commented:
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
 
SalteCommented:
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
 
bcladdCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now