소켓프로그래밍 중에서 문자 출력 말고 특정 파일을 같이 실행
Hello world 예제인데 메시지 말고 ls 같음 명령어를 실행 시키고 싶은데 어떤 방법이 있나요
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define MYPORT 3490 /* the port users will be connecting to */
#define BACKLOG 10 /* how many pending connections queue will hold */
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) \
== -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n", \
inet_ntoa(their_addr.sin_addr));
if (!fork()) { /* this is the child process */
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
}
음...
ls 명령의 결과를 클라이언트에게 보여주고 싶다는 말인가요?
만약 그렇다면 파이프를 사용하시면 되겠네요.
man popen
코드로 잠깐 생각해 본다면 아래와 같이 하면 되겠네요.
H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!
Re: 소켓프로그래밍 중에서 문자 출력 말고 특정 파일을 같이 실
중간에 send하는 부분을 recv 하는 부분에서 처리해 주어야 합니다.
http://bbs.kldp.org/viewtopic.php?t=29827
보시면 같은 결과를 원하시는듯 싶네요.
댓글 달기