Click to See Complete Forum and Search --> : Print from buffer
dellthinker
June 22nd, 2007, 02:16 PM
Hi all, im trying to do the following:
Print a string of chars to a buffer, and print from the buffer using the sockets send() function. So far i havnt found a way.
The function in question:
bool TextSender(const char* Text)
{
if(send(dataSock, Text, strlen(Text), 0) == -1)
return false;
else
return true;
}
I was wondering if i printed information to either dataSock or Text it would work, but it does nothing.
Does it matter if i define char Text; at the top ? Because its not doing a thing, any suggestions? Thanx in advance!
MikeAThon
June 24th, 2007, 01:15 PM
What do you mean by "print" to/from a buffer? The send() function sends data over a socket to a (usually) remote recipient. It doesn't print anything.
dellthinker
June 28th, 2007, 02:04 PM
What i mean is sprint a string of characters to a buffer, then send() them to a server. What im trying to do is this:
if(strcmp("hi", recvbuffer) == 0)){
sprintf(sendbuffer, "Hello!");
Send(sendbuffer);
}
bool Send()
{
if(send(sock, sendbuffer, strlen(sendbuffer), 0) == -1)
return false;
else
return true;
}
Or something similar to this, only that it has to work. This maybe wrong in some aspect, i just dont see where my mistake is, any suggestions? Thanx in advance!
MikeAThon
June 28th, 2007, 07:14 PM
Show us what you tried. The "code" you posted above obviously isn't real code.
Mike
PS: Remeber that TCP is a stream protocol not a message protocol. TCP sends and receives a stream of bytes; it doesn't care what the bytes represent, and it makes no effort to maintain groupings of bytes corresponding to your messages, even though you might try to enforce particular groupings in the way you call send(). Maybe you assumed something different about TCP when you wrote the logic of your code.
dellthinker
June 29th, 2007, 03:59 PM
The reason why i only pasted the problematic code was because the entire source itself is about 300 lines long. Please believe that function is the only thing thats giving me trouble. All i need to do is figure out the proper argument to use when i call it in the if statement.
char sendbuffer[512];
bool SendToServer(char* msg, ...);
void HandleMessage()
{
char* servermsg;
for (int i = 0; i<512; i++)
if(recvbuffer[i] == '\n') recvbuffer[i++] = '\0';
chanmsg=recvbuffer;
printf("%s\n", servermsg);
if(strcmp("hi",servermsg) == 0){
sprintf(sendbuffer, "Hello\n");
SendToServer(sendbuffer); // This is where im having difficulty
}
}
bool SendToServer(char* msg, ...)
{
if(send(sock, sendbuffer, strlen(sendbuffer), 0) == -1)
return false;
else
return true;
}
MikeAThon
June 29th, 2007, 08:02 PM
It's a waste of time to debug fake code. Sorry, but if you can't post the code then we can't help.
It's better to post a small project that you write specifically to exhibit the performance problems you are seeing. This is a tall order, but many times, when writing the small project, you suddenly see the answer and solve the problem yourself.
Incidentally, think carefully about my comments on the "stream" nature of TCP. Often, when working with TCP, the problem is in the code that receives, not the code that sends.
Mike
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.