socket-programming:recvfrom
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:recvfrom [2024/06/18 00:30] – ایجاد شد pejman | socket-programming:recvfrom [2024/06/18 01:01] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()recvfrom ====== | ||
| - | |||
| - | این تابع برای خواندن از سوکت های SOCK_DGRAM استفاده می شود. | ||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); | ||
| - | </ | ||
| - | |||
| - | مثال برای خواندن درخواست های DNS از کلاینت dig و نمایش آنها | ||
| - | |||
| - | <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(" | ||
| - | close(sockfd); | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | ssize_t readlen; | ||
| - | char buf[BUFSIZ]; | ||
| - | struct sockaddr_storage src_addr; | ||
| - | socklen_t addrlen = sizeof(src_addr); | ||
| - | 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(" | ||
| - | |||
| - | unsigned char c; | ||
| - | int i, j, k, space; | ||
| - | printf(" | ||
| - | for (i = 0; i < readlen; i+=16) { | ||
| - | printf(" | ||
| - | for (j = i; j < i+16 && j < readlen; j++) { | ||
| - | c = (unsigned char)buf[j]; | ||
| - | printf(" | ||
| - | } | ||
| - | |||
| - | if (j == i+16) | ||
| - | space = 1; | ||
| - | else | ||
| - | space = 1 + (16*3) - (j-i)*3; | ||
| - | |||
| - | for (k = 0; k < space; k++) | ||
| - | printf(" | ||
| - | |||
| - | for (j = i; j < i+16 && j < readlen; j++) { | ||
| - | c = (unsigned char)buf[j]; | ||
| - | if (c <= 32 || c >= 127) | ||
| - | c = ' | ||
| - | printf(" | ||
| - | } | ||
| - | printf(" | ||
| - | } | ||
| - | printf(" | ||
| - | |||
| - | close(sockfd); | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | کلاینت dig | ||
| - | |||
| - | $ dig @127.0.0.1 google.com | ||
| - | | ||
| - | ; <<>> | ||
| - | ; (1 server found) | ||
| - | ;; global options: +cmd | ||
| - | ;; connection timed out; no servers could be reached | ||
| - | |||
| - | |||
| - | خروجی سرور | ||
| - | |||
| - | Got an IPv4 connection from 127.0.0.1 | ||
| - | | ||
| - | Client message: 51 byte | ||
| - | | ||
| - | 0x0000 6C 29 01 20 00 01 00 00 00 00 00 01 06 67 6F 6F l)...........goo | ||
| - | 0x0010 67 6C 65 03 63 6F 6D 00 00 01 00 01 00 00 29 10 gle.com.......). | ||
| - | 0x0020 00 00 00 00 00 00 0C 00 0A 00 08 72 1C F7 76 B8 ...........r..v. | ||
| - | 0x0030 B2 D5 BF ... | ||
| - | |||
socket-programming/recvfrom.1718658018.txt.gz · آخرین ویرایش: 2024/06/18 00:30 توسط pejman
