socket-programming:sendto
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:sendto [2024/06/18 00:30] – ایجاد شد pejman | socket-programming:sendto [2024/06/18 01:02] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()sendto ====== | ||
| - | |||
| - | این تابع برای نوشتن در سوکت های SOCK_DGRAM استفاده می شود. | ||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); | ||
| - | </ | ||
| - | |||
| - | مثال echo کلاینت | ||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define SERVER " | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | char buf[BUFSIZ] = "Hello world"; | ||
| - | int status, sockfd; | ||
| - | |||
| - | struct addrinfo hints, *res, *rp; | ||
| - | memset(& | ||
| - | hints.ai_family = AF_INET; | ||
| - | hints.ai_socktype = SOCK_DGRAM; | ||
| - | hints.ai_protocol = 0; | ||
| - | |||
| - | status = getaddrinfo(SERVER, | ||
| - | if (status != 0) { | ||
| - | fprintf(stderr, | ||
| - | gai_strerror(status)); | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | for (rp = res; rp != NULL; rp = rp-> | ||
| - | sockfd = socket(rp-> | ||
| - | rp-> | ||
| - | if (sockfd == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | status = sendto(sockfd, | ||
| - | rp-> | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | |||
| - | ssize_t readlen; | ||
| - | readlen = recvfrom(sockfd, | ||
| - | if (readlen == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | |||
| - | printf(" | ||
| - | |||
| - | close(sockfd); | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | |||
| - | </ | ||
| - | |||
| - | مثال echo سرور | ||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | int status, sockfd; | ||
| - | |||
| - | struct addrinfo hints, *res, *rp; | ||
| - | memset(& | ||
| - | hints.ai_flags = AI_PASSIVE; | ||
| - | hints.ai_family = AF_INET; | ||
| - | hints.ai_socktype = SOCK_DGRAM; | ||
| - | hints.ai_protocol = 0; | ||
| - | |||
| - | status = getaddrinfo(NULL, | ||
| - | if (status != 0) { | ||
| - | fprintf(stderr, | ||
| - | gai_strerror(status)); | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | for (rp = res; rp != NULL; rp = rp-> | ||
| - | sockfd = socket(rp-> | ||
| - | rp-> | ||
| - | if (sockfd == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | status = bind(sockfd, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | struct sockaddr_storage src_addr; | ||
| - | socklen_t addrlen = sizeof(src_addr); | ||
| - | char buf[BUFSIZ]; | ||
| - | ssize_t readlen; | ||
| - | readlen = recvfrom(sockfd, | ||
| - | (struct sockaddr *)& | ||
| - | if (readlen == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | char *ipver, ipstr[INET6_ADDRSTRLEN]; | ||
| - | void *addr; | ||
| - | if (src_addr.ss_family == AF_INET6) { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } else { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } | ||
| - | inet_ntop(src_addr.ss_family, | ||
| - | printf(" | ||
| - | printf(" | ||
| - | |||
| - | status = sendto(sockfd, | ||
| - | (struct sockaddr *)& | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | printf(" | ||
| - | |||
| - | close(sockfd); | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | خروجی client | ||
| - | |||
| - | Sent 11 bytes to server | ||
| - | --- | ||
| - | Hello world | ||
| - | --- | ||
| - | Receive 11 bytes from server | ||
| - | | ||
| - | | ||
| - | خروجی server | ||
| - | |||
| - | Got an IPv4 connection from 127.0.0.1 | ||
| - | Receive 11 bytes from client | ||
| - | Sent 11 bytes to client | ||
| - | |||
socket-programming/sendto.1718658036.txt.gz · آخرین ویرایش: 2024/06/18 00:30 توسط pejman
