[해결]2개의 main함수를 하나로....

number1s의 이미지

안녕하세요
기존에 있던 프로그램에다가 이번에 프로그램을 하나 더 만들었는데, 하나로 합쳐야 합니다.
어떻게 하면 될까요?
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를 호출하세요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.