Open tonykang22 opened 2 years ago
#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);
#include <sys/types.h> #include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>
int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in receiver_addr; struct sockaddr_in sender_addr; int sender_len; char buff[1024] = {0,}; int recvlen, sendlen;
if (argc < 2) { fprintf(stderr, "Usage: %s <port>\n", argv[0]); exit(1); } sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket() error"); exit(1); } memset(&receiver_addr, 0, sizeof(receiver_addr)); receiver_addr.sin_family = AF_INET; receiver_addr.sin_addr.s_addr = htonl(INADDR_ANY); receiver_addr.sin_port = htons(atoi(argv[1])); if (bind(sockfd, (struct sockaddr *)&receiver_addr, sizeof(receiver_addr)) == -1) { perror("bind() error"); exit(1); } while(1) { memset(buff, 0, sizeof(buff)); sender_len = sizeof(sender_addr); recvlen = recvfrom(sockfd, buff, sizeof(buff)-1, 0, (struct sockaddr *)&sender_addr, &sender_len); if (recvlen == -1) { perror("recvfrom() error"); exit(1); } buff[recvlen] = '\0'; if (strcmp(buff, ".\n") == 0) break; printf("Received Data: %s\n", buff); snprintf(buff, sizeof(buff), "OK!"); sendlen = strlen(buff); if (sendto(sockfd, buff, sendlen, 0, (struct sockaddr *)&sender_addr, sizeof(sender_addr)) != sendlen) { perror("sendto() error"); exit(1); } } close(sockfd); return 0;
}
<br> - sender.c 의 코드이다. ``` c #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in receiver_addr; char buff[1024] = {0,}; int bufflen, recvlen, receiver_len; if (argc < 3) { fprintf(stderr, "Usage: %s <IP> <Port>\n", argv[0]); exit(1); } sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket() error"); exit(1); } memset(&receiver_addr, 0, sizeof(receiver_addr)); receiver_addr.sin_family = AF_INET; receiver_addr.sin_addr.s_addr = inet_addr(argv[1]); receiver_addr.sin_port = htons(atoi(argv[2])); while(1) { printf("Input message (quit: .): "); memset(buff, 0, sizeof(buff)); fgets(buff, sizeof(buff), stdin); bufflen = strlen(buff); if (sendto(sockfd, buff, bufflen, 0, (struct sockaddr *)&receiver_addr, sizeof(receiver_addr)) != bufflen) { perror("sendto() error"); exit(1); } if (strcmp(buff, ".\n") == 0) break; recvlen = recvfrom(sockfd, buff, sizeof(buff)-1, 0, (struct sockaddr *)&receiver_addr, &receiver_len); if (recvlen == -1) { perror("recvfrom() error"); exit(1); } buff[recvlen] = '\0'; printf("Reply received: %s\n", buff); } close(sockfd); return 0; }
18. UDP & 유닉스 도메인 소켓
UDP 소켓 통신 개요
예제
int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in receiver_addr; struct sockaddr_in sender_addr; int sender_len; char buff[1024] = {0,}; int recvlen, sendlen;
}
유닉스 도메인 소켓