c언어 소켓프로그래밍좀질문드리겟습니다 ㅜ
들찬
예제를 그대로쳐서해봣는데 컴파일은 되는데 ... 자꾸 recvfrom 이함수부분에서 엉키는거같더라구요 ㅜㅜ
멀티캐스트를이용한 채팅프로그램입니다 ㅜ ㅜ어디가문제일까요
#include stdio.h
#include sys/types.h
#include sys/socket.h
#include netinet/in.h
#include arpa/inet.h
#include stdlib.h
#include unistd.h
#include string.h
char *msg_exit = exit\n;
#define MAXLINE 1024
int main(int argc, char **argv)
{
int read_fd;
int send_fd;
pid_t pid;
int flag;
struct sockaddr_in from;
int addrlen;
struct sockaddr_in mcast_group;
struct ip_mreq mreq;
char name[16];
char message[MAXLINE];
if(argc != 4){
printf(Usage : %s\n, argv[0]);
return 1;
}
sprintf(name, %s, argv[3]);
memset(&mcast_group, 0x00, sizeof(mcast_group));
mcast_group.sin_family=AF_INET;
mcast_group.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &mcast_group.sin_addr);
if( (read_fd = socket(AF_INET, SOCK_DGRAM, 0 )) 0 )
{
return 1;
mreq.imr_multiaddr = mcast_group.sin_addr;
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(read_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) 0){
perror(error: add group\n);
return 1;
}
flag=1;
if(setsockopt(read_fd, SOL_SOCKET, SO_REUSEADDR, &flag,
sizeof(flag)) 0)
{
printf(Socket Reuseaddr erro\n);
return 1;
}
if(bind(read_fd, (struct sockaddr *)&mcast_group,
sizeof(mcast_group)) 0){
return 1;
}
if( (send_fd = socket(AF_INET, SOCK_DGRAM, 0)) 0)
{
return 1;
}
pid=fork();
if(pid 0)
{
printf(for error\n);
return 1;
}
else if(pid == 0)
{
while(1)
{
addrlen=sizeof(from);
memset(message, 0x00, sizeof(message));
if(readn=recvfrom(read_fd, message, MAXLINE, 0,
(struct sockaddr*)&from, &addrlen) 0)
{
perror(recvfrom:);
return 1;
}
printf(%s, message);
if(strstr(message, msg_exit) != NULL)
{
printf(EXIT\n);
if(setsockopt(read_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
&mreq, sizeof(mreq)) 0)
{
return 1;
}
}
}
}
else
{
while(1)
{
memset(message, 0x00, MAXLINE);
read(0, message, MAXLINE);
sprintf(message, %s : %s, name, message);
if(sendto(send_fd, message, strlen(message), 0,
(struct sockaddr *)&mcast_group,
&; sizeof(mcast_group)) strlen(message))
{
return 1;
}
}
}
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |
2699816 | 오류 질문입니다.. (1) | 해비치 | 2025-06-29 |
2699763 | 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) | 미라 | 2025-06-28 |
2699555 | c언어 다항식을 입력을 했는데 왜 출력이 안될까요? | 피스케스 | 2025-06-27 |
2699528 | C언어 포인터연산 질문입니다. (3) | 안녕나야 | 2025-06-26 |
2699476 | 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) | 클라우드 | 2025-06-26 |