ابزار کاربر

ابزار سایت


c-socket-programming:sendto

تابع ()sendto

این تابع برای نوشتن در سوکت های SOCK_DGRAM استفاده می شود.

#include <sys/types.h>
#include <sys/socket.h>
 
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);

مثال echo کلاینت

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
 
#define PORT "3435"
#define SERVER "127.0.0.1"
 
int main()
{
	char buf[BUFSIZ] = "Hello world";
	int status, sockfd;
 
	struct addrinfo hints, *res, *rp;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = 0;
 
	status = getaddrinfo(SERVER, PORT, &hints, &res);
	if (status != 0) {
		fprintf(stderr, "getaddrinfo() error: %s", 
			gai_strerror(status));
		exit(EXIT_FAILURE);
	}
 
	for (rp = res; rp != NULL; rp = rp->ai_next) {
		sockfd = socket(rp->ai_family, rp->ai_socktype, 
				rp->ai_protocol);
		if (sockfd == -1) {
			perror("socket() error ");
			continue;
		}
 
		break;
	}
        freeaddrinfo(res);
 
	if (rp == NULL) {
		fprintf(stderr, "Could not connect()\n");
		exit(EXIT_FAILURE);
	}
 
	status = sendto(sockfd, buf, strlen(buf), 0, rp->ai_addr, 
			rp->ai_addrlen);
	if (status == -1) {
		perror("sendto() error ");
		exit(EXIT_FAILURE);
	}
 
	printf("Sent %d bytes to server\n", status);
 
	ssize_t readlen;
	readlen = recvfrom(sockfd, buf, BUFSIZ, 0, NULL, NULL);
	if (readlen == -1) {
		perror("recvfrom() error");
		exit(EXIT_FAILURE);
	}
 
	printf("---\n%s\n---\n", buf);
 
	printf("Receive %d bytes from server\n", status);
 
	close(sockfd);
	return 0;
}

مثال echo سرور

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
 
#define PORT "3435"
 
int main()
{
	int status, sockfd;
 
	struct addrinfo hints, *res, *rp;
	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = 0;
 
	status = getaddrinfo(NULL, PORT, &hints, &res);
	if (status != 0) {
		fprintf(stderr, "getaddrinfo() error: %s\n",
			gai_strerror(status));
		exit(EXIT_FAILURE);
	}
 
	for (rp = res; rp != NULL; rp = rp->ai_next) {
		sockfd = socket(rp->ai_family, rp->ai_socktype,
				rp->ai_protocol);
		if (sockfd == -1) {
			perror("socket() error ");
			continue;
		}
 
		status = bind(sockfd, rp->ai_addr, rp->ai_addrlen);
		if (status == -1) {
			perror("bind() error ");
			continue;
		}
 
		break;
	}
        freeaddrinfo(res);
 
	if (rp == NULL) {
		fprintf(stderr, "Could not bind()\n");
		exit(EXIT_FAILURE);
	}
 
	struct sockaddr_storage src_addr;
	socklen_t addrlen = sizeof(src_addr);
	char buf[BUFSIZ];
	ssize_t readlen;
	readlen = recvfrom(sockfd, buf, BUFSIZ, 0, 
				(struct sockaddr *)&src_addr, &addrlen);
	if (readlen == -1) {
		perror("recvfrom() error ");
		exit(EXIT_FAILURE);
	}
 
	char *ipver, ipstr[INET6_ADDRSTRLEN];
	void *addr;
	if (src_addr.ss_family == AF_INET6) {
		ipver = "IPv6";
		addr = &(((struct sockaddr_in6 *)&src_addr)->sin6_addr);
	} else {
		ipver = "IPv4";
		addr = &(((struct sockaddr_in *)&src_addr)->sin_addr);
	}
	inet_ntop(src_addr.ss_family, addr, ipstr, INET6_ADDRSTRLEN);
	printf("Got an %s connection from %s\n", ipver, ipstr);
	printf("Receive %d bytes from client\n", readlen);
 
	status = sendto(sockfd, buf, readlen, 0 , 
			(struct sockaddr *)&src_addr , addrlen); 
	if (status == -1) {
		perror("sendto() error ");
		exit(EXIT_FAILURE);
	}
	printf("Sent %d bytes to client\n", readlen);
 
	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
c-socket-programming/sendto.txt · آخرین ویرایش: 2024/06/18 01:02 توسط pejman

به جز مواردی که ذکر می‌شود، مابقی محتویات ویکی تحت مجوز زیر می‌باشند: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki