udp 연결 단방향 전송 (서버가 연결된 udp 클라이언트에게만 데이터를 전송할 수 있음)

server.c
UDP 소켓에 클라이언트가 연결됨: 1.클라이언트만 서버에 데이터를 보낼 수 있습니다. 2.서비스 측은 서버처럼 데이터를 보낼 수 없습니다 (모든 발송 함수 포함)
#include 
#include 
#include 
#include           /* See NOTES */
#include 
#include 
#include 
#include 
#include 
#include 

typedef struct sockaddr	   sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct in6_addr	   in6_addr_t;
typedef	unsigned int		u32;
#define CUR				printf("%s : %d
", __FILE__, __LINE__); #define SeverIP "192.168.1.104" #define ClientIP "192.168.1.107" #define THRAN_1024 1025 #define PORT THRAN_1024 static int sfd, ret, len = sizeof(sockaddr_t); static sockaddr_in_t server_addr; // static sockaddr_in_t client_addr; // /* ctr +c */ static void sig_int(int sig) { close(sfd); exit(0); } #if 0 void *work_sendto(void *arg) { char buf[50]; int ret; while (1) { memset(buf, 0, 50); fgets(buf, 50, stdin); // ret = sendto(sfd, buf, strlen(buf), 0, NULL, 0); printf("s: %d
", ret); } pthread_exit(NULL); } #endif void *work_recvfrom(void *arg) { char buf[50]; int ret; while (1) { memset(buf, 0, 50); ret = recvfrom(sfd, buf, sizeof(buf), 0, NULL, 0); printf("recvfrom %d: %s", ret, buf); if (!ret) { perror("client shutdown"); exit(0); } } pthread_exit(NULL); } int main(int argc, char **argv) { pthread_t tid_sendto, tid_recvfrom; // ID, ID sfd = socket(AF_INET, SOCK_DGRAM, 0); signal(SIGINT, sig_int); // memset(&server_addr, 0, sizeof(sockaddr_in_t)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); // server_addr.sin_addr.s_addr = inet_addr(SeverIP); if(-1 == bind(sfd, (sockaddr_t *)&server_addr, len)) { perror("bind err"); goto err; } /* 1 */ /* 2 */ //if(pthread_create(&tid_sendto, NULL, work_sendto, NULL)) //perror("pthread_create err"); // UDP , if(pthread_create(&tid_recvfrom, NULL, work_recvfrom, NULL)) perror("pthread_create err"); pthread_join(tid_sendto, NULL); pthread_join(tid_recvfrom, NULL); return 0; err: close(sfd); return -1; }

client.c
#include 
#include 
#include 
#include           /* See NOTES */
#include 
#include 
#include 
#include 
#include 
#include 

typedef struct sockaddr	   sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct in6_addr	   in6_addr_t;
typedef	unsigned int		u32;
#define CUR				printf("%s : %d
", __FILE__, __LINE__); #define SeverIP "192.168.1.104" #define ClientIP "192.168.1.107" #define THRAN_1024 1025 #define PORT THRAN_1024 static int cfd, ret, len = sizeof(sockaddr_t); static sockaddr_in_t server_addr, client_addr; // /* ctr +c */ static void sig_int(int sig) { close(cfd); exit(0); } void *work_sendto(void *arg) { int ret; char buf[50]; while (1) { memset(buf, 0, 50); fgets(buf, 50, stdin); // ret = sendto(cfd, buf, strlen(buf), 0, NULL, 0); // , printf("sendto: %d
", ret); } pthread_exit(NULL); } void *work_recvfrom(void *arg) { char buf[50]; int ret; while (1) { memset(buf, 0, 50); CUR; ret = recvfrom(cfd, buf, sizeof(buf), 0, NULL, 0); CUR; printf("recvfrom %d: %s", ret, buf); if (!ret) { perror("server shutdown"); exit(0); } } pthread_exit(NULL); } int main(int argc, char **argv) { pthread_t tid_sendto, tid_recvfrom; // ID, ID cfd = socket(AF_INET, SOCK_DGRAM, 0); signal(SIGINT, sig_int); // memset(&server_addr, 0, sizeof(sockaddr_in_t)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); // server_addr.sin_addr.s_addr = inet_addr(SeverIP); connect(cfd, (sockaddr_t *)&server_addr, len); /* 1 */ /* 2 */ if(pthread_create(&tid_sendto, NULL, work_sendto, NULL)) perror("pthread_create err"); if(pthread_create(&tid_recvfrom, NULL, work_recvfrom, NULL)) perror("pthread_create err"); pthread_join(tid_sendto, NULL); pthread_join(tid_recvfrom, NULL); return 0; err: close(cfd); return -1; }

좋은 웹페이지 즐겨찾기