Character string problem!

character string ouput problem

Page 1 of 1

6 Replies - 623 Views - Last Post: 05 February 2010 - 11:18 PM Rate Topic: -----

#1 infinateone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 10

Character string problem!

Posted 05 February 2010 - 10:54 PM

Hi all! I am having a problem with a networking program I am putting together. I send a char* string to a server and print it out using cout. The problem is when I go to print out the char* string it comes out as "My message###############*&^^$%%$###", it displays my message with a bunch of funny symbols at the end.

Here is the code:

I send "My Message" from:
//Sets character data.
void ModularSocket::SendData(char* sendData)
{
    //Holds result of sending.
    int sendResult;

    //Sends buffer.
    sendResult = send(Socket, sendData, (int)strlen(sendData), 0);
    //Checks if send was successful.
    if (sendResult == SOCKET_ERROR) 
        cout << "Send Data Failed!\n";
    else
	cout << "Send Data Succeeded!\n";
}//End send data.

sendData is the char* string i send in from:

SendData("My Message");


the message is then put into a struct that looks like:

//
struct Packet
{
    char text[80];
} CustomPacket;


It is set up by this function in the server:

//Recieves data from a client.
void ModularSocket::RecieveDataFromClient()
{
	// Receive until the peer shuts down the connection
    do 
    {
	RecievedResult = recv(Socket, (char*)&CustomPacket, sizeof(CustomPacket), 0);

	if (RecievedResult > 0) 
	{
	    HandleRecievedPacket(CustomPacket);
	    printf("Bytes received: %d\n", RecievedResult);
	} 
	else if (RecievedResult == 0)
	    printf("Connection closing...\n");
	else
	{
	    printf("Recieving Failed: %d\n", WSAGetLastError());
	    closesocket(Socket);
	    WSACleanup();
	}
    } while (RecievedResult > 0);
}//End recieve data from client.


And the print function is HandleRecievedPacket(CustomPacket) which looks like this:

//
void ModularSocket::HandleRecievedPacket(Packet packet)
{
    cout << packet.text << endl;
}//


So my big question is why does my output off of HandleRecievedData come out as "My Message##################################$^%^*%*&%^*&%&*" ?

Any help is apreciated.

This post has been edited by skyhawk133: 05 February 2010 - 11:04 PM
Reason for edit:: Added [code] Tags


Is This A Good Question/Topic? 0
  • +

Replies To: Character string problem!

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Character string problem!

Posted 05 February 2010 - 10:59 PM

Have you tried stepping through your code with a debugger and looking at what is actually stored in packet.text?

I don't know anything about sockets so I'm not going to pretend I do but, I had problems similar to this where extra characters would print out. Look and see what I being stored in the packet variable first.

Also, dont forget to use code tags when posting code. It makes it much easier to read.
( Code Tags are the little <> brackets in the toolbar )

This post has been edited by eker676: 05 February 2010 - 11:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Character string problem!

Posted 05 February 2010 - 11:01 PM

View Postinfinateone, on 05 February 2010 - 09:54 PM, said:

....
//Sends buffer.
sendResult = send(Socket, sendData, (int)strlen(sendData), 0);
...

I am guessing this.I think it should be strlen(sendData)+1...that will include the null character.So you you wont have problems on the recieving end.
Was This Post Helpful? 1
  • +
  • -

#4 infinateone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 10

Re: Character string problem!

Posted 05 February 2010 - 11:10 PM

View Posteker676, on 05 February 2010 - 09:59 PM, said:

Have you tried stepping through your code with a debugger and looking at what is actually stored in packet.text?

I don't know anything about sockets so I'm not going to pretend I do but, I had problems similar to this where extra characters would print out. Look and see what I being stored in the packet variable first.

Also, dont forget to use code tags when posting code. It makes it much easier to read.
( Code Tags are the little <> brackets in the toolbar )


sorry about the formatting, I have never used this site before. Its not a network code problem at all, it is a char* string problem. I think it has something to do with the struct text member being 80 bytes and the data coming in being less.
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Character string problem!

Posted 05 February 2010 - 11:13 PM

You have to zero the memory of a char* before you use it because it will contain left-over memory:
memset(some_char_pointer, 0, sizeof(some_char_pointer));

Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Character string problem!

Posted 05 February 2010 - 11:14 PM

You could try padding the text member then with blank spaces or null characters.

Again, I don't know how sockets work so this is just a shot in the dark.
Was This Post Helpful? 0
  • +
  • -

#7 infinateone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 10

Re: Character string problem!

Posted 05 February 2010 - 11:18 PM

View PostMunawwar, on 05 February 2010 - 10:01 PM, said:

View Postinfinateone, on 05 February 2010 - 09:54 PM, said:

....
//Sends buffer.
sendResult = send(Socket, sendData, (int)strlen(sendData), 0);
...

I am guessing this.I think it should be strlen(sendData)+1...that will include the null character.So you you wont have problems on the recieving end.


You were right! is was just missing the terminating "\0" character. Adding 1 to the length of the string got it in there and it displays right. Thanks for your help and everyone elses who tried. It was a simple thing as it usualy is. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1