socket-programming:getpeername
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:getpeername [2024/06/18 00:31] – ایجاد شد pejman | socket-programming:getpeername [2024/06/18 01:03] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()getpeername ====== | ||
| - | |||
| - | این تابع برای پیدا کردن آدرس سوی دیگر connection یک سوکت استفاده می شود. | ||
| - | <code C> | ||
| - | #include < | ||
| - | |||
| - | int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | ||
| - | </ | ||
| - | |||
| - | مثال: مانند خواندن آدرس کلاینت بعد از [[accept]] منتها این بار نه از پارامتر های [[accept]] بلکه با [[getpeername]] | ||
| - | |||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define BACKLOG 120 | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | int status, sockfd, clientfd, sockopt; | ||
| - | struct addrinfo hints, *res, *rp; | ||
| - | |||
| - | memset(& | ||
| - | hints.ai_flags = AI_PASSIVE; | ||
| - | hints.ai_family = AF_INET6; | ||
| - | hints.ai_socktype = SOCK_STREAM; | ||
| - | 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; | ||
| - | } | ||
| - | |||
| - | sockopt = 1; | ||
| - | status = setsockopt(sockfd, | ||
| - | & | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | sockopt = 0; | ||
| - | status = setsockopt(sockfd, | ||
| - | & | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | status = bind(sockfd, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | status = listen(sockfd, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | |||
| - | clientfd = accept(sockfd, | ||
| - | if (clientfd == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | struct sockaddr_storage peeraddr; | ||
| - | socklen_t peeraddrlen = sizeof(peeraddr); | ||
| - | status = getpeername(clientfd, | ||
| - | & | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | | ||
| - | void *addr; | ||
| - | char *ipver; | ||
| - | char ipstr[INET6_ADDRSTRLEN]; | ||
| - | struct sockaddr *sa = (struct sockaddr*)& | ||
| - | if (sa-> | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } else { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } | ||
| - | inet_ntop(sa-> | ||
| - | printf(" | ||
| - | |||
| - | close(clientfd); | ||
| - | close(sockfd); | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | |||
| - | </ | ||
socket-programming/getpeername.1718658092.txt.gz · آخرین ویرایش: 2024/06/18 00:31 توسط pejman
