2015-01-16 2 views
0

UDP를 사용하여 클라이언트/서버 기반 응용 프로그램을 개발하여 원격 서버에서 프로그램을 실행합니다. 즉 클라이언트가 실행 파일을 서버에 보내고 서버가 파일을 실행하고 결과를 파일에 저장 한 다음 클라이언트로 다시 보냅니다.UDP를 통해 파일 내용을 전송 한 후 가비지 문자 받기

은 내가 2 개 파일

// 서버에 실행 파일 이름을 전송

를 코딩 한; 서버가 그것을 실행 // 에코 프로그램; 이

//이 프로그램은 클라이언트에서 실행 파일을 받아

 #include <sys/types.h> 
     #include <sys/socket.h> 
     #include <stdio.h> 
     #include <stdlib.h> 
     #include <string.h> 
     #include <netinet/in.h> 
    #include <arpa/inet.h> 
    #include<fcntl.h> 
    #include<unistd.h> 
    #include<stdlib.h> 
    void usage(const char *progname) 
     { 
     fprintf(stderr, "Usage: %s <server-IP> <server-port>\n",   
       progname); 
     } 
    int main(int argc, char *argv[]) 
     { 

     if(argc != 3) 
     { 
     usage(argv[0]); 
     exit(1); 
     } 
    int len, result, sockfd,clilen; 
     int n; 
     struct sockaddr_in seraddr; 
     char buf[310], buf1[500]={0}; 
     sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
     if(sockfd<0) 
      { // Check error condition 
       perror("socket failed"); return -1; 
      } 


    bzero(&seraddr, sizeof(seraddr)); 

    seraddr.sin_family = AF_INET; 
    seraddr.sin_addr.s_addr = INADDR_ANY; 
    seraddr.sin_port=htons(atoi(argv[2])); 
    int ap; 

      clilen=sizeof(struct sockaddr_in); 
    //read contents of file into buf; send to server 
    int fd = open("Executable_Client.c", O_RDWR); 
     int n1 = read(fd, buf, sizeof(buf)); 

     n = write(sockfd,buf,n1); 
     ap=sendto(sockfd,buf,n1,0,(const struct sockaddr 
     *)&seraddr, sizeof(struct sockaddr)); 

     int n2 = recvfrom(sockfd, buf1, sizeof(buf1), 0, (struct  
         sockaddr *)&seraddr, &clilen); 
     n = read(sockfd,buf1, n2); 

     printf("The output is: %s\n", buf1); 
     return 0; 
     } 

Server.c 클라이언트의 코드이며, 그것을 // 에코 프로그램을 실행; 이것은 또한

 #include<stdio.h> 
     #include<string.h> 
     #include<sys/types.h> 
     #include<sys/socket.h> 
     #include<netinet/in.h> 
     #include<fcntl.h> 
     #include<unistd.h> 
     #include<fcntl.h> 
     #include<unistd.h> 
     #include<stdlib.h> 

     void usage(const char *progname) 
     { 
     fprintf(stderr, "Usage: %s <server-port>\n", progname); 
     } 


     int main(int argc, char *argv[]) 
     { 

     if(argc != 2) 
     { 
     usage(argv[0]); 
     exit(1); 
     } 

    int sockfd, newsockfd,portno, client, n, ans,clilen; 
    char buf[310], buf1[256] = {0}; 

    struct sockaddr_in seraddr, cliaddr; 
    int i, value; 
    sockfd = socket(AF_INET, SOCK_DGRAM, 0); 

    if(sockfd<0) 
    { 
    perror("socket failed"); return -1; 
    } 

    int size=sizeof(struct sockaddr); 
     bzero(&seraddr,size); 

    seraddr.sin_family = AF_INET; 
    seraddr.sin_addr.s_addr =inet_addr("127.0.0.1"); 
    seraddr.sin_port = htons(atoi(argv[1])); 


    int a=bind(sockfd, (struct sockaddr *)&seraddr, size); 
      if (a<0) 
      { 
      perror("bind"); close(sockfd); return -1; 
      } 

      clilen=sizeof(struct sockaddr_in); 

    int fd1 = open("Executable_Server.c", O_RDWR); 
    //write contents into Executable_Server.c 
    int n1 = recvfrom(sockfd, buf, sizeof(buf), 0, (struct  
         sockaddr *)&seraddr, &clilen); 
      n = write(fd1, buf, n1); 

    system("gcc Executable_Server.c -o execute.out"); 
    system("./execute.out>Output.txt");//>"Output.txt"); 

    //read contents from Output.txt 
    int fd = open("Output.txt", O_RDWR); 
    int n3 = read(fd, buf1, n1); 

    //send contents to client 

    n = write(sockfd, buf1,n3); 
      n = sendto(sockfd, buf1, n3, 0, (const struct 
         sockaddr *)&seraddr, sizeof(struct 
         sockaddr)); 
    } 

, Executable_Client.c의 코드가

가 //이 파일의하는 실행 파일이 클라이언트에서 서버로 전송 될 것이다 서버의 코드; 이 파일의 Executable_Client.c. 실제 코드 외에도, 가비지 많은 가치가 또한 존재하는 파일 실행시

 #include<stdio.h> 
     #include<stdlib.h> 
     int main() 
     { 
     int a, b, c; 
     a = 3; 
     b = 4; 

     c = a+b; 

     printf("a+b = %d\n", c); 
       return 0; 
     } 

, Executable_Server.c에

의 내용을 판독한다. 결과적으로 우분투 12.04의 터미널 창은 실행 부분에서 나타나는 표식 기호를 언급하는 많은 오류를 보여줍니다. 누구나이 이형성의 원인을 제안 할 수 있습니까?

+0

** 당신은 UDP를 사용하고 싶지 않습니다 **. 그렇게하는 것은 잘못된 것입니다. 심지어 재미 있지도 않습니다. TCP로 이동하십시오. –

+0

나는 그 선택권이 없다. 질문을 변경할 수 없습니다. – APD

+0

대신 TCP를 사용하는 옵션이 없다면 패킷 순서 변경, 패킷 손실 및 패킷 복제를 처리해야합니다. TCP는 UDP 대신 UDP를 사용합니다. –

답변

2

코드가 여러 곳에서 잘못되어 있습니다. 짧은 모양에서 가장 명백한 것을 언급하기 만하면됩니다.

recvfrom을 읽기와 혼합하여 동일한 데이터를 읽습니다 (sendto를 믹싱하고 나중에 쓰는 것과 동일).

int n2 = recvfrom(sockfd, buf1, sizeof(buf1), 0, (struct  
        sockaddr *)&seraddr, &clilen); 
    n = read(sockfd,buf1, n2); 

실제로받은 데이터의 양에 관계없이 버퍼를 인쇄합니다. 이것은 물론 읽기 전에 버퍼에 있던 쓰레기 데이터를 인쇄하고 읽을 때 덮어 쓰지 않는 데이터는 출력합니다.

printf("The output is: %s\n", buf1); 

그리고 물론

는 패킷 재정렬, 패킷 손실 및 패킷 중복 걱정하지 않는 한 UDP와 같은 일을하는 나쁜 생각입니다.

+0

감사합니다 :). 나는 오랜 시간 이래 그것을 시도하고 있었다. 나는 read 라인을 주석 처리하고 printf ("%. * s", stringLength, pointerToString)와 일치하도록 printf 문을 변경했습니다. – APD

관련 문제