Page 74 - 2578
P. 74
#define MAXBUF 256
#define FILE "echo.serv"
char buf[MAXBUF];
main()
{
struct sockaddr_un serv_addr, clnt_addr;
int sockfd;
int saddrlen, caddrlen, max_caddrlen, n;
if((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) <
0){
printf("Nemozhlyvo stvoryty socket\n");
exit(1);
}
unlink(FILE);
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, FILE);
saddrlen=sizeof(serv_addr.sun_family)+strlen(serv_add
r.sun_path);
if(bind(sockfd, (struct sockaddr*)&serv_addr,
saddrlen) < 0){
printf("Pomylka svyazuvannya socketa z
adresoyu\n");
exit(1);
}
max_caddrlen = sizeof(clnt_addr);
for(;;){
caddrlen = max_caddrlen;
n = recvfrom(sockfd, buf, MAXBUF, 0, (struct
sockaddr*)&clnt_addr, &caddrlen);
if(n < 0){
printf("Pomylka pryjmannya\n");
exit(1);
}
if(sendto(sockfd, buf, n, 0, (struct
sockaddr*)&clnt_addr, caddrlen) !=n){
printf("Pomylka peredavannya\n");
exit(1);
}
73