J
Jeff Crouse
In my project, I have successfully opened a sock_stream style socket
connection between two processes and am trying to send 'buffer' to a
connected client, 'fd'.
The following code works just fine.
void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
}
But, obviously, it is not sending the desired string.
The following code does not work.
void Macsock::writeData(char *buffer, int fd)
{
char buf[strlen(buffer)];
strcpy(buf, buffer);
buf[sizeof(buf)] = '\0';
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}
I'm sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I'm just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.
Any suggestions as to why the string in the function parameter would
cause a problem, whereas the string declared in the function does not
would be greatly appreciated. I welcome any other questions about my
situation (I'm sure I have forgotten something important), but I would
like to avoid questions about the client, as it is not written in C++
and is therefore off-topic. Suffice to say, it has been tested with
other hosts and works just fine. I am 99% sure that the problem is
with the host, not the client.
connection between two processes and am trying to send 'buffer' to a
connected client, 'fd'.
The following code works just fine.
void Macsock::writeData(char *buffer, int fd)
{
char message[] = "This is a message from the front.";
if(write(fd, message, sizeof(message)) < 0 )
perror("writing on stream socket");
}
But, obviously, it is not sending the desired string.
The following code does not work.
void Macsock::writeData(char *buffer, int fd)
{
char buf[strlen(buffer)];
strcpy(buf, buffer);
buf[sizeof(buf)] = '\0';
if(write(fd, buf, sizeof(buf)) < 0 )
perror("writing on stream socket");
}
I'm sure that this very simple code is horrifying to any C programmer,
but I am not a C programmer, I'm just a hack trying to get something
finished. All of the stcpy stuff was done because the connected
client I mentioned is very picky about messages being terminated by a
null character.
Any suggestions as to why the string in the function parameter would
cause a problem, whereas the string declared in the function does not
would be greatly appreciated. I welcome any other questions about my
situation (I'm sure I have forgotten something important), but I would
like to avoid questions about the client, as it is not written in C++
and is therefore off-topic. Suffice to say, it has been tested with
other hosts and works just fine. I am 99% sure that the problem is
with the host, not the client.