I am working with sockets in C (linux) and I need to send a bit longer text (60characters) over the network. I've tried 开发者_C百科a char pointer, but it is too short. Any suggestions what should I use?
char *data = "A lot of text....";
...
if (send(new_fd, data, 13, 0) == -1)
perror("send");
I'm not really a c
person, so what does that number 13
mean?
This is how you should send the data.
char *data = "A lot of text....";
ssize_t rc;
...
rc = send(new_fd, data, strlen(data), 0);
/* Check rc. */
From the manual:
ssize_t send(int socket, const void *buffer, size_t length, int flags);
So the 13
is the number of bytes sent.
One thing to consider is that send(2)
doesn't guarantee it will be able to send all of it in one go. You need to loop and check how much it wrote. A good way to do it is using the writen
function of Stevens.
精彩评论