[해결]2개의 main함수를 하나로....
안녕하세요
기존에 있던 프로그램에다가 이번에 프로그램을 하나 더 만들었는데, 하나로 합쳐야 합니다.
어떻게 하면 될까요?
c에 계념이 없는지라 도와주시면 감하겠습니다.
수고하세요~
#include
#include
#include
#include
#define PORT 4486 // the port client will be connecting to
//############################################################
int DaemonAlive(char *proc_name);
main()
{
if (DaemonAlive("hydra2"))
printf("hydra2 alive\n");
else
printf("hydra2 dead\n");
if (DaemonAlive("hydra3"))
printf("hydra3 alive\n");
else
printf("hydra3 dead\n");
}
int DaemonAlive(char *proc_name)
{
FILE *fp;
char buf[80];
int result;
result = 0;
sprintf(buf, "ps -C %s", proc_name);
fp = popen(buf, "r");
while (fgets(buf, sizeof(buf), fp)) {
if (strstr(buf, proc_name)) {
result = 1;
break;
}
}
pclose(fp);
return result;
}
//############################################################
int main(int argc, char **argv)
{
int client_len;
int client_sockfd;
//int fd
FILE *fp_in;
char buf_in[255];
char buf_get[255];
struct sockaddr_in clientaddr;
if (argc != 2) {
fprintf(stderr,"usage: Server ip\n");
exit(1);
}
if ((client_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(PORT);
clientaddr.sin_addr.s_addr = inet_addr (argv[1]);
memset(&(clientaddr.sin_zero), '\0', 8); // zero the rest of the struct
client_len = sizeof(clientaddr);
if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) < 0)
{
perror("Connect error: ");
exit(0);
}
strcpy(buf_in, "request");
buf_in[strlen(buf_in) - 1] = '\0';
write(client_sockfd, buf_in, sizeof(buf_in));
if (strncmp(buf_in, "quit", 4) == 0)
{
close(client_sockfd);
exit(0);
}
while(1)
{
read(client_sockfd, buf_get, 255);
/*intf("%s", buf_get);*/
break;
}
if ((client_sockfd=open("/tmp/chgjudge", O_WRONLY | O_CREAT, 0644)) ==-1) {
perror("open failed");
exit(1);
}
if (write(client_sockfd, buf_get, 1) == -1) {
perror("write failed");
exit(1);
}
close(client_sockfd);
exit(0);
}
간단히 둘중에
간단히 둘중에 필요없는 main은 주석으로 처리하던지 지우세요.
기존 main 작업이 끝나고 새로운 main 작업이 추가 되어야 한다는 개념이라면
(소스를 자세히 보지 않아서 의도는 모르겠지만 만약 이런 생각으로 작성하셨다면)
새로 만들어진 main의 이름을 main2로 바꾸고 기존 main함수 말미에 main2를 호출하세요.
댓글 달기