sento() 에 인자를 좀 바꾸어서 명령어줄 인자를 받으면 재미있긴 한데..
TCP 처럼 클라이언트 별로 accept 해주는 구조가 아니라
개별 클라이언트를 어떻게 구분해야 하지? 라고 고민중
server
// Server side implementation of UDP client-server model #include #include #include #include #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #define PORT 8080 #define MAXLINE 1024 // Driver code int main() { int sockfd; char buffer[MAXLINE]; char *hello = "Hello from server"; struct sockaddr_in servaddr, cliaddr; // Creating socket file descriptor if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror("socket creation failed"); exit(EXIT_FAILURE); } memset(&servaddr, 0, sizeof(servaddr)); memset(&cliaddr, 0, sizeof(cliaddr)); // Filling server information servaddr.sin_family = AF_INET; // IPv4 servaddr.sin_addr.s_addr = INADDR_ANY; servaddr.sin_port = htons(PORT); // Bind the socket with the server address if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) { perror("bind failed"); exit(EXIT_FAILURE); } while(1) { int len, n; n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &cliaddr, &len); buffer[n] = '\0'; printf("Client : %s\n", buffer); // sendto(sockfd, (const char *)hello, strlen(hello), sendto(sockfd, (const char *)buffer, strlen(buffer), MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len); printf("Hello message sent.\n"); } return 0; } |
client
// Client side implementation of UDP client-server model #include #include #include #include #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #define PORT 8080 #define MAXLINE 1024 // Driver code int main(int argc, char **argv) { int sockfd; char buffer[MAXLINE]; char *hello = "Hello from client"; struct sockaddr_in servaddr; // Creating socket file descriptor if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { perror("socket creation failed"); exit(EXIT_FAILURE); } memset(&servaddr, 0, sizeof(servaddr)); // Filling server information servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = INADDR_ANY; int n, len; // sendto(sockfd, (const char *)hello, strlen(hello), sendto(sockfd, (const char *)argv[1], strlen(argv[1]), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr)); printf("Hello message sent.\n"); n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *) &servaddr, &len); buffer[n] = '\0'; printf("Server : %s\n", buffer); close(sockfd); return 0; } |
[링크 : https://www.geeksforgeeks.org/udp-server-client-implementation-c/]
[링크 : https://www.geeksforgeeks.org/tcp-and-udp-server-using-select/]
+
[링크 : https://scrapsquare.com/notes/udp-length]
[링크 : https://en.wikipedia.org/wiki/User_Datagram_Protocol]
+
class (cpp) 기반 UDP socket 예제
[링크 : http://youngmok.com/udp-server-c-class-listening-thread/]
'Linux API > network' 카테고리의 다른 글
UDS (Unix Domain Socket) (0) | 2020.09.01 |
---|---|
raw socker과 promiscous mode (0) | 2019.07.03 |
리눅스 TCP 소켓 (0) | 2019.05.24 |
linux udp cpp example (0) | 2019.05.16 |
linux socket 관련 (0) | 2015.01.22 |