socket-programming:recv
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:recv [2024/06/18 00:29] – ایجاد شد pejman | socket-programming:recv [2024/06/18 01:01] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()recv | ||
| - | از این تابع برای خواندن از یک سوکت از نوع stream استفاده می شود. | ||
| - | |||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | ssize_t recv(int sockfd, void *buf, size_t len, int flags); | ||
| - | </ | ||
| - | |||
| - | مقدار بازگشتی این تابع برابر است با تعداد بایتی که خوانده و در buf ذخیره شده است. | ||
| - | |||
| - | مثال برای ارسال و دریافت یک پیغام از سمت سرور با کلاینت telnet | ||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define BACKLOG 120 | ||
| - | #define BUFSIZE 1000 | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | int status, sockopt; | ||
| - | |||
| - | fprintf(stdout, | ||
| - | struct addrinfo hints; | ||
| - | memset(& | ||
| - | hints.ai_flags = AI_PASSIVE; | ||
| - | hints.ai_family = AF_INET6; | ||
| - | hints.ai_socktype = SOCK_STREAM; | ||
| - | |||
| - | struct addrinfo *bind_address, | ||
| - | status = getaddrinfo(NULL, | ||
| - | if (status != 0) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | int listen_socket; | ||
| - | for (rp = bind_address; | ||
| - | |||
| - | fprintf(stdout, | ||
| - | listen_socket = socket(rp-> | ||
| - | rp-> | ||
| - | if (listen_socket == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | sockopt = 1; | ||
| - | status = setsockopt(listen_socket, | ||
| - | (void*)& | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | sockopt = 0; | ||
| - | status = setsockopt(listen_socket, | ||
| - | (void*)& | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | fprintf(stdout, | ||
| - | status = bind(listen_socket, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(bind_address); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | fprintf(stdout, | ||
| - | status = listen(listen_socket, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | fprintf(stdout, | ||
| - | fflush(stdout); | ||
| - | |||
| - | int client_socket; | ||
| - | struct sockaddr_storage client_addr; | ||
| - | socklen_t client_addrlen = sizeof(client_addr); | ||
| - | client_socket = accept(listen_socket, | ||
| - | & | ||
| - | if (client_socket == -1) { | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | char *ipver, ipstr[INET6_ADDRSTRLEN]; | ||
| - | void *addr; | ||
| - | if (client_addr.ss_family == AF_INET) { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } else if (client_addr.ss_family == AF_INET6) { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } else { | ||
| - | ipver = " | ||
| - | } | ||
| - | inet_ntop(client_addr.ss_family, | ||
| - | printf(" | ||
| - | |||
| - | printf(" | ||
| - | ssize_t readlen; | ||
| - | char readbuf[BUFSIZE]; | ||
| - | readlen = recv(client_socket, | ||
| - | if (readlen < 1) { | ||
| - | fprintf(stderr, | ||
| - | if(readlen == -1) | ||
| - | perror(" | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | | ||
| - | // null terminating client message | ||
| - | if(readlen > 0 && readlen < BUFSIZE) | ||
| - | readbuf[readlen] = 0; | ||
| - | else | ||
| - | readbuf[0] = 0; | ||
| - | | ||
| - | fprintf(stdout, | ||
| - | |||
| - | fprintf(stdout, | ||
| - | |||
| - | char *msg = "Hello world\n"; | ||
| - | status = send(client_socket, | ||
| - | if (status == -1) | ||
| - | perror(" | ||
| - | else | ||
| - | fprintf(stdout, | ||
| - | |||
| - | printf(" | ||
| - | close(client_socket); | ||
| - | |||
| - | printf(" | ||
| - | close(listen_socket); | ||
| - | |||
| - | printf(" | ||
| - | |||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | کلاینت | ||
| - | $ telnet 127.0.0.1 3435 | ||
| - | Trying 127.0.0.1... | ||
| - | Connected to 127.0.0.1. | ||
| - | Escape character is ' | ||
| - | this is a test message | ||
| - | Hello world | ||
| - | Connection closed by foreign host. | ||
| - | |||
| - | خروجی سرور | ||
| - | |||
| - | Configuring local address ... | ||
| - | Creating socket ... | ||
| - | Binding socket to local address ... | ||
| - | Listening ... | ||
| - | Waiting for connections ... | ||
| - | Client is connected from IPv6 address :: | ||
| - | Reading client request ... | ||
| - | Cleint message: | ||
| - | --- | ||
| - | this is a test message | ||
| - | | ||
| - | --- | ||
| - | Sending message to cleint... | ||
| - | Byte sent = 12 | ||
| - | Closing connection... | ||
| - | Closing listening socket... | ||
| - | Finished. | ||
| - | |||
| - | کلاینت جایگزین telnet در مثال بالا | ||
| - | |||
| - | <code C> | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define SERVER " | ||
| - | #define BUFSIZE 250 | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | char *msg = "This is a test message."; | ||
| - | int status, sockfd; | ||
| - | |||
| - | printf(" | ||
| - | struct addrinfo hints, *res, *rp; | ||
| - | memset(& | ||
| - | hints.ai_family = AF_INET; | ||
| - | hints.ai_socktype = SOCK_STREAM; | ||
| - | status = getaddrinfo(SERVER, | ||
| - | if (status != 0) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | for (rp = res; rp != NULL; rp = rp-> | ||
| - | |||
| - | printf(" | ||
| - | sockfd = socket(rp-> | ||
| - | if (sockfd == -1) { | ||
| - | perror(" | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | status = connect(sockfd, | ||
| - | if (status == -1) { | ||
| - | perror(" | ||
| - | close(sockfd); | ||
| - | continue; | ||
| - | } | ||
| - | |||
| - | break; | ||
| - | } | ||
| - | freeaddrinfo(res); | ||
| - | |||
| - | if (rp == NULL) { | ||
| - | fprintf(stderr, | ||
| - | exit(EXIT_FAILURE); | ||
| - | } | ||
| - | |||
| - | printf(" | ||
| - | status = send(sockfd, | ||
| - | if (status == -1) | ||
| - | perror(" | ||
| - | else | ||
| - | printf(" | ||
| - | |||
| - | printf(" | ||
| - | char buf[BUFSIZE]; | ||
| - | status = recv(sockfd, | ||
| - | if (status < 1) { | ||
| - | fprintf(stderr, | ||
| - | if (status == -1) | ||
| - | perror(" | ||
| - | } | ||
| - | |||
| - | // null terminating client message | ||
| - | if (status > 0 && status < BUFSIZE) | ||
| - | buf[status] = 0; | ||
| - | else | ||
| - | buf[0] = 0; | ||
| - | |||
| - | if (strlen(buf) > 0) | ||
| - | printf(" | ||
| - | | ||
| - | printf(" | ||
| - | close(sockfd); | ||
| - | |||
| - | printf(" | ||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | خروجی کلاینت | ||
| - | |||
| - | Configuring local address ... | ||
| - | Creating socket ... | ||
| - | Connecting to server ... | ||
| - | Sending message to server ... | ||
| - | Byte sent = 23 | ||
| - | Reading server message ... | ||
| - | Client message: | ||
| - | --- | ||
| - | Hello world | ||
| - | | ||
| - | --- | ||
| - | Closing connection ... | ||
| - | Finished. | ||
| - | |||
| - | |||
| - | خروجی سرور | ||
| - | |||
| - | Configuring local address ... | ||
| - | Creating socket ... | ||
| - | Binding socket to local address ... | ||
| - | Listening ... | ||
| - | Waiting for connections ... | ||
| - | Client is connected from IPv6 address :: | ||
| - | Reading client request ... | ||
| - | Cleint message: | ||
| - | --- | ||
| - | This is a test message. | ||
| - | --- | ||
| - | Sending message to cleint... | ||
| - | Byte sent = 12 | ||
| - | Closing connection... | ||
| - | Closing listening socket... | ||
| - | Finished. | ||
socket-programming/recv.1718657998.txt.gz · آخرین ویرایش: 2024/06/18 00:29 توسط pejman
