news_sender.c: In function ‘main’:
news_sender.c:42:8: warning: passing argument 1 of ‘close’ makes integer from pointer without a cast [-Wint-conversion]
close((FILE*)fp);
^
In file included from news_sender.c:4:0:
/usr/include/unistd.h:356:12: note: expected ‘int’ but argument is of type ‘FILE * {aka struct _IO_FILE *}’
extern int close (int __fd);
^~~~~
mes@ubuntu:~/TcpCom
공부좀 더해야겟음
news_sender.c 보내는애
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define TTL 64
#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc, char *argv[])
{
int send_sock;
struct sockaddr_in mul_adr;
int time_live=TTL;
FILE *fp;
char buf[BUF_SIZE];
if(argc!=3) {
printf("Usage : %s <GroupIP> <PORT>\n", argv[0]);
exit(1);
}
send_sock=socket(PF_INET, SOCK_DGRAM, 0);
memset(&mul_adr, 0, sizeof(mul_adr));
mul_adr.sin_family=AF_INET;
mul_adr.sin_addr.s_addr=inet_addr(argv[1]);
mul_adr.sin_port=htons(atoi(argv[2]));
setsockopt(send_sock, IPPROTO_IP,
IP_MULTICAST_TTL, (void*)&time_live, sizeof(time_live));
if((fp=fopen("news.txt", "r"))==NULL)
error_handling("fopen() error");
while(!feof(fp)) /* Broadcasting */
{
fgets(buf, BUF_SIZE, fp);
sendto(send_sock, buf, strlen(buf),
0, (struct sockaddr*)&mul_adr, sizeof(mul_adr));
sleep(2);
}
close((FILE*)fp);
close(send_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
이애러 뜨는데 알아봐야할듯
news_receiver.c 받는에
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc, char *argv[])
{
int recv_sock;
int str_len;
char buf[BUF_SIZE];
struct sockaddr_in adr;
struct ip_mreq join_adr;
if(argc!=3) {
printf("Uszge : %s <GroupIP> <PORT>\n", argv[0]);
exit(1);
}
recv_sock=socket(PF_INET, SOCK_DGRAM, 0);
memset(&adr, 0, sizeof(adr));
adr.sin_family=AF_INET;
adr.sin_addr.s_addr=htonl(INADDR_ANY);
adr.sin_port=htons(atoi(argv[2]));
if(bind(recv_sock, (struct sockaddr*) &adr, sizeof(adr))==-1)
error_handling("bind() error");
join_adr.imr_multiaddr.s_addr=inet_addr(argv[1]);
join_adr.imr_interface.s_addr=htonl(INADDR_ANY);
setsockopt(recv_sock, IPPROTO_IP,
IP_ADD_MEMBERSHIP, (void*)&join_adr, sizeof(join_adr));
while(1)
{
str_len=recvfrom(recv_sock, buf, BUF_SIZE-1, 0, NULL, 0);
if(str_len<0)
break;
buf[str_len]=0;
fputs(buf, stdout);
}
close(recv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
'[ 충남인력개발원 ] (2019) > ┗TCP&IP' 카테고리의 다른 글
송수신 사진으로 보기 (0) | 2019.11.26 |
---|---|
Linux(리눅스) fg, bg, kill, Ctrl+Z (0) | 2019.11.26 |
sendto() 소켓으로 데이터를 전송 (0) | 2019.11.26 |
좀 보기 편하다 기본 리눅스 단축키 워드 (0) | 2019.11.26 |
github.com 설정하기 (0) | 2019.11.25 |