开发者

Send bigger amount of data [closed]

开发者 https://www.devze.com 2023-03-20 21:11 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消