Socket Programming Question:
Download Job Interview Questions and Answers PDF
What this function sendto() does?
![Socket Programming Interview Answer Socket Programming Interview Question](https://globalguideline.com/public/images/interview/6.png)
Answer:
Sending to a Named Socket - sendto()
int sendto(int s, char *msg, int len, int flags, struct sockaddr *to, int tolen)
This function allows a message msg of length len to be sent on a socket with descriptor s to the socket named by to and tolen, where tolen is the actual length of to. flags will always be zero for our purposes. The number of characters sent is the return value of the function. On error, -1 is returned and errno describes the error.
An example:
struct sockaddr to_name;
to_name.sa_family = AF_UNIX;
strcpy(to_name.sa_data, "/tmp/sock");
if (sendto(s, buf, sizeof(buf),
0, &to_name,
strlen(to_name.sa_data) +
sizeof(to_name.sa_family)) < 0) {
printf("send failuren");
exit(1);
}
int sendto(int s, char *msg, int len, int flags, struct sockaddr *to, int tolen)
This function allows a message msg of length len to be sent on a socket with descriptor s to the socket named by to and tolen, where tolen is the actual length of to. flags will always be zero for our purposes. The number of characters sent is the return value of the function. On error, -1 is returned and errno describes the error.
An example:
struct sockaddr to_name;
to_name.sa_family = AF_UNIX;
strcpy(to_name.sa_data, "/tmp/sock");
if (sendto(s, buf, sizeof(buf),
0, &to_name,
strlen(to_name.sa_data) +
sizeof(to_name.sa_family)) < 0) {
printf("send failuren");
exit(1);
}
Download Socket Programming Interview Questions And Answers
PDF
Previous Question | Next Question |
What this function connect() does? | What this function recvfrom() does? |