socket-programming:send
تفاوتها
تفاوت دو نسخهٔ متفاوت از صفحه را مشاهده میکنید.
| socket-programming:send [2024/06/18 00:29] – ایجاد شد pejman | socket-programming:send [2024/06/18 01:01] (فعلی) – حذف شد pejman | ||
|---|---|---|---|
| خط 1: | خط 1: | ||
| - | ====== تابع ()send ====== | ||
| - | |||
| - | از این تابع برای نوشتن در یک سوکت از نوع stream استفاده می شود. | ||
| - | |||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | ssize_t send(int sockfd, const void *buf, size_t len, int flags); | ||
| - | </ | ||
| - | |||
| - | مقدار بازگشتی این تابع تعداد بایتی است که توانسته ارسال کند. اگر این مقدار کمتر از len باشد ما موظیم مابقی buf را مجدد از طریق [[send]] ارسال کنیم. | ||
| - | |||
| - | مثال ارسال Hello world برروی بستر SOCK_STREAM توسط [[send]] : | ||
| - | |||
| - | <code C> | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #define PORT " | ||
| - | #define BACKLOG 120 | ||
| - | |||
| - | int main() | ||
| - | { | ||
| - | char *msg = "Hello World!"; | ||
| - | void *addr; | ||
| - | char *ipver; | ||
| - | char ipstr[INET6_ADDRSTRLEN]; | ||
| - | |||
| - | int status, sockfd, clientfd, sockopt, len; | ||
| - | struct addrinfo hints, *res, *rp; | ||
| - | struct sockaddr_storage clientaddr; | ||
| - | struct sockaddr *sa; | ||
| - | socklen_t addrlen; | ||
| - | |||
| - | 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); | ||
| - | } | ||
| - | |||
| - | fprintf(stdout, | ||
| - | |||
| - | addrlen = sizeof(clientaddr); | ||
| - | clientfd = accept(sockfd, | ||
| - | |||
| - | sa = (struct sockaddr*)& | ||
| - | if (sa-> | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } else { | ||
| - | ipver = " | ||
| - | addr = & | ||
| - | } | ||
| - | inet_ntop(sa-> | ||
| - | fprintf(stdout, | ||
| - | | ||
| - | fprintf(stdout, | ||
| - | len = strlen(msg); | ||
| - | status = send(clientfd, | ||
| - | if (status == -1) | ||
| - | perror(" | ||
| - | else | ||
| - | fprintf(stdout, | ||
| - | |||
| - | close(clientfd); | ||
| - | close(sockfd); | ||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | کلاینت | ||
| - | |||
| - | $ telnet 127.0.0.1 3450 | ||
| - | Trying 127.0.0.1... | ||
| - | Connected to 127.0.0.1. | ||
| - | Escape character is ' | ||
| - | Hello World!Connection closed by foreign host. | ||
| - | |||
| - | خروجی سرور | ||
| - | |||
| - | Server: waiting for connections... | ||
| - | Server: Got an IPv6 connection from :: | ||
| - | Server: Sending data ... | ||
| - | Server: byte sent = 12 | ||
socket-programming/send.1718657978.txt.gz · آخرین ویرایش: 2024/06/18 00:29 توسط pejman
