리눅스 서버에서 멀티캐스팅 방식을 쓰레드를 써서 표현한건데요 에러랑 워닝 다잡앗는데 안되네요 ㅠㅠ
#include
#include
#include
#include
#include
#include
#include
#include
#include
pthread_cond_t thread_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
#define BUF_LEN 512
void* send_t(void* data);
void* recv_t(void* data);
int send_s, recv_s;
struct sockaddr_in mcast_group;
struct ip_mreq mreq;
char name[20];
int yes = 1;
void* recv_t(void* data){
int n, len;
struct sockaddr_in from;
char name[20],message[BUF_LEN + 1];
for( ; ; ){
printf("Receive Message : ");
len = sizeof(from);
if((n == recvfrom(recv_s, message, BUF_LEN, 0, (struct sockaddr *)&from, &len)) < 0){
pthread_mutex_lock(&mutex_lock);
printf("recvfrom Error!\n");
exit(0);
}
message[n] = 0;
printf(" Received Message : %s\n", message);
}
pthread_mutex_unlock(&mutex_lock);
}
void* send_t(void* data){
int n, len;
struct mcast_group;
char message[BUF_LEN + 1], line[BUF_LEN + 1];
printf("Send Message : ");
while(fgets(message, BUF_LEN, stdin) != NULL){
sprintf(line, "%s %s", name, message);
len = strlen(line);
if(sendto(send_s, line, strlen(line), 0, (struct sockaddr *)&mcast_group, sizeof(mcast_group)) < 0){
pthread_mutex_lock(&mutex_lock);
printf("sendto Error!\n");
exit(0);
}
pthread_mutex_unlock(&mutex_lock);
}
}
int main(int argc, char* argv[]){
int n, len, i;
int th_state; // 스레드 상태 판별 변수
int th_f[2] = {1, 2}; // 스레드 함수 인자 변수
pthread_t p_thread[2];
if( argc != 4){
printf("Use : %s | multicast_address | port_number | My_alias \n", argv[0]);
exit(0);
}
sprintf(name, "[%s]", argv[3]);
memset(&mcast_group, 0, sizeof(mcast_group));
mcast_group.sin_family = AF_INET;
mcast_group.sin_addr.s_addr = htonl(INADDR_ANY);
mcast_group.sin_port = htons(atoi(argv[2]));
if((recv_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
printf("Error : Can't create receive socket\n");
exit(0);
}
mreq.imr_multiaddr = mcast_group.sin_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(recv_s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0){
printf("Error : add membership\n");
exit(0);
}
if(setsockopt(recv_s, SOL_SOCKET, SO_REUSEADDR, &mreq, sizeof(yes)) < 0){
printf("Error : reuse setsockopt\n");
exit(0);
}
if(bind(recv_s, (struct sockaddr *)&mcast_group, sizeof(mcast_group)) < 0){
printf("Error : bind receive socket\n");
exit(0);
}
if((send_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
printf("Error : Can't create send socket\n");
exit(0);
}
if(pthread_create(&p_thread[0], NULL, send_t, (void *)&th_f[0]) < 0){
printf("thread[0] create Error!\n");
exit(0);
}
if(pthread_create(&p_thread[1], NULL, recv_t, (void *)&th_f[1]) < 0){
printf("thread[1] create Error!\n");
exit(0);
}
for(i=0; i<2 ; i++)
pthread_join(p_thread[i], (void *)&th_state);
shutdown(send_s, 2);
shutdown(recv_s, 2);
close(send_s);
close(recv_s);
return 0;
}
pthread_create랑 pthread_join 이거 두 개의 레퍼런스가 정의되어 있지 않다고 하네요
헤더파일에 #include선언해 줬고 안에 파라미터도 맞는데 왜 안되는 건가요??ㅠㅠ
pthread 링크 하셨나요?
-lpthread 옵션 주셨는지?
제가 어제
제가 어제 -lpthread옵션 주라고 댓글 달았었는데 삭제 되었네요 -_-;;
감사합니다^^
스레드는 실행파일 만드는 법이 다르군요 ㅎㅎ
댓글 달기