Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 7:50 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
send recieve sockets

I am not able to recieve ASCII characters from the server..?
i send somehting like this:
²Íq,’/ÝwrHúUÌ ¹»º»
using
sendto(sd, buffer, (int)strlen(buffer),0, (struct sockaddr *)&host, sizeof(struct sockaddr_in))
sd is the socket descriptor, buffer is a char*.
I recieve like this
recvfrom(sd,buffer,strlen(buffer),0,(struct sockaddr *) &client,&clen)
The message received would be:
================
basicly garbage.. why does this happen? should i convert to a binary (1's and 0's) then send and decode after recieving..?
hbe02 is offline   Reply With Quote
Old May 25th, 2006, 8:07 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
There is a good chance you're getting garbage for the same reason I said in your other thread, which you chose to ignore. Stop thinking that 'strlen()' is going to return the size of the array, cos it wont. And neither will sizeof() for a char*.
Cache is offline   Reply With Quote
Old May 25th, 2006, 8:27 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
But when i give give that parameter sizeof( ) which returns 4 for my char*. i only recieve up to 4 characters of my message.. this is when i send normal mesages like "HELLO SERVER"
On the other hand, when i give strlen() to that parameter. i recieve my complete message, so i assume that the parameter is like the upper limit of the mesage to be recieved. i just say strlen because when i declare my char* i say = new char[50] so strlen would be large enough (around 50) to accomodate any message i would send.
In the case of sending ASCII. im sending messages that around aroun 13000 ASCII characters. so i declare char * buffer = new char[25000] and i use strlen to recieve the whole 13000 ASCII characters..
But apparently from first post. im not Recieving them correctly. But this is my intention for using strlen() and not sizeof()
hbe02 is offline   Reply With Quote
Old May 25th, 2006, 9:06 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
To quote myself (note the part in bold):
Quote:
Stop thinking that 'strlen()' is going to return the size of the array, cos it wont. And neither will sizeof() for a char*.
In the other thread I didn’t know the buffer was a char*, because you never said. Now, strlen() returns the number of characters in the array, not the size of the array itself. So it’s obviously not suitable to be passed as a function parameter that asks for the length of the array. Just to make clear: I’m talking about the third parameter in the ‘recvfrom’ function.

Since you know the buffer is 2500 bytes, why not just pass the number 2500 for the size? Which also makes me wonder why dynamically allocate the buffer at all, given that you always allocate the same amount of memory to it...

EDIT: Keep in mind that this stands regardless of any other problems in your code.

Cache.

Last edited by Cache; May 25th, 2006 at 9:19 PM.
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC