在 linux 中获得选择

Getopt in linux

我的一小部分代码:

#include <stdio.h> //printf
#include <string.h> //memset
#include <stdlib.h> //exit(0);
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <netinet/tcp.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <openssl/md5.h>
#include <limits.h>

#define BUFLEN 100  //Max length of buffer

void die(char *s)
{
    perror(s);
    exit(1);
}

struct Wiadomosc{
    int nr_ksiegi;
    int jednostka;
    int czas;
    int port;
};

char *str2md5(const char *str, int length) {
    int n;
    MD5_CTX c;
    unsigned char digest[16];
    char *out = (char*)malloc(33);

    MD5_Init(&c);

    while (length > 0) {
        if (length > 512) {
            MD5_Update(&c, str, 512);
        } else {
            MD5_Update(&c, str, length);
        }
        length -= 512;
        str += 512;
    }

    MD5_Final(digest, &c);

    for (n = 0; n < 16; ++n) {
        snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
    }

    return out;
}


void wyslij_udp(int port, const char* adres_server, struct Wiadomosc moja);
int polacz_tcp(int port, const char* server_adres);

int pobierz_argument(char *optarg)
{
   char *endptr;
   int val;

   val = (int)strtol(optarg,&endptr,0);
   if(endptr == optarg)
   {
    fprintf(stderr,"Niepoprawny paramtr funkcji \n");
    exit(EXIT_FAILURE);
   }
   return val;
}

void print_usage() {
    printf("Usage: \n./client -i numer_ip -o numer_portu -k numer_ksiegi -j jednostka \n-c czas -p port -d plik_wynikowy \n\nnumer_ip - numer ip servera\n-o - numer portu serwera\nnumer_ksiegi - numer ksiegi Pana Tadeusza <1,12> \njednostka - rodzaj wysylanych komunikatow: \na) 1 - wysylanie sa cale linie \nb) 2 - wysylanie sa slowa i nowe linie \nc) 3 - wysylanie sa znaki(litery,sredniki...) \nczas - czas pomiedzy poszczegolnymi komunikatami \nport - port otrzymywanych komunikatow \n");

}

int main(int argc, char *argv[])
{
    int option = 0;
    int ksiega = -1, jednostka = -1, czas = -1, port =-1,port2=-1;

     int sockfd = 0, n = 0;
     char *line;
     size_t len = 0;
     char recvBuff[LINE_MAX];
     char* adres;
     struct Wiadomosc moja;  
     int fd;
     char* plik_wynikowy;

    if(argc !=15)
    {
    print_usage();
    exit(EXIT_FAILURE);
    }

    while ((option = getopt(argc, argv,"k:j:c:p:i:o:d:")) != -1) {
        switch (option) {
             case 'i' :
            if(strlen(optarg) < 7 || strlen(optarg) > 21)
            {
            fprintf(stderr,"niepoprawny adres ip \n");
                exit(EXIT_FAILURE);
            }
                    adres = optarg;
                break;
             case 'o' :
                    port2 = pobierz_argument(optarg);
                    if( port2 < 0 || port2 > 65535 )
                    {
                        fprintf(stderr,"niepoprawna wartosc ksiegi! \n");
                        exit(EXIT_FAILURE);
                    }
                break;
         case 'k' :
            ksiega = pobierz_argument(optarg);
            if( ksiega < 0 || ksiega > 12 )                
            {
            fprintf(stderr,"niepoprawna wartosc ksiegi! \n");
                exit(EXIT_FAILURE);
            }
        break;
             case 'j' :
            jednostka = pobierz_argument(optarg);
            if(jednostka < 1 || jednostka > 3)
            {
                        fprintf(stderr,"niepoprawna wartosc jednostki! \n");
                        exit(EXIT_FAILURE);
                    }
                 break;
             case 'c' :
                    czas = pobierz_argument(optarg);
                    if(czas < 0 || czas > 2000)
                    {
                        fprintf(stderr,"niepoprawna wartosc czasu! \n");
                        exit(EXIT_FAILURE);
                    }
                 break;
             case 'p' :
                    port = pobierz_argument(optarg);
                    if(port < 0 || port > 65535)
                    {
                        fprintf(stderr,"niepoprawna wartosc portu! \n");
                        exit(EXIT_FAILURE);
                    }
                 break;
             case 'd' :
                    if(strlen(optarg) < 2 )
                    {
                        fprintf(stderr,"niepoprawny plik wynikowy  \n");
                        exit(EXIT_FAILURE);
                    }              
                    plik_wynikowy = optarg;
                 break;
             default: print_usage();
                 exit(EXIT_FAILURE);
        }
    }

    moja.nr_ksiegi = ksiega;
    moja.jednostka = jednostka;
    moja.czas = czas;
    moja.port = port;


     wyslij_udp(port2,adres,moja);

     // now WAITING FOR CONNECTION - ENJOY !   

     sleep(1);

     fd = open(plik_wynikowy, O_WRONLY | O_CREAT | O_TRUNC);


     memset(recvBuff,0,sizeof(recvBuff));
     sockfd = polacz_tcp(5000, adres);

     fprintf(stdout,"Trwa transmisja...\n");   
     while( ( recv(sockfd,recvBuff,sizeof(recvBuff),0) ) > 0 )
     {
        char *wiad;
        //recvBuff[n] = 0;     
        write(fd,recvBuff,n);
/*      if(fprintf(stdout,"%s",recvBuff) == EOF)
        {
            perror("fprintf");
        }
        fflush(stdout);
*/  

        // odebral - teraz wyslac
        wiad = str2md5(recvBuff,strlen(recvBuff));
        fprintf(stdout,"Wiadomosc %s \n",recvBuff);
        if( send(sockfd,wiad,33,0) == -1 )
        {
            fprintf(stderr, "Failure Sending Message\n");
        }
        free(wiad);
        bzero( recvBuff, sizeof( recvBuff ) );

    }
    if(n<0)    
    {
        printf("read error");
    }

    close(fd);

    return 0;

}
void wyslij_udp(int port, const char* adres_server, struct Wiadomosc moja)
{
    struct sockaddr_in si_other;
    int s, i ;
    int slen = sizeof(si_other);

    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(port);

    if (inet_aton(adres_server , &si_other.sin_addr) == 0)
    {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }

   //send the message
   if (sendto(s, (struct Wiadomosc*)&moja, 1024 + sizeof(moja) , 0 , (struct sockaddr *) &si_other,slen)==-1)
        {
            die("sendto()");
        }
   close(s);
}

int polacz_tcp(int port, const char* server_adres)
{
    int sockfd = 0,r;
    struct sockaddr_in serv_tcp;    

    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        die("socket");
    }
    memset(&serv_tcp,0,sizeof(serv_tcp));

    serv_tcp.sin_family = AF_INET;
    serv_tcp.sin_port = htons(port);  

    if(inet_pton(AF_INET,server_adres,&serv_tcp.sin_addr) < 0)
    {
        die("inet pton");
    }


    if(connect(sockfd,(struct sockaddr *)&serv_tcp, sizeof(serv_tcp))< 0)
    {
        die("conect");
    }

    return sockfd;
}

我尝试 运行 我的程序是这样的:

./C -i 127.0.0.1 -o 12345 -k 4 -j2 -c 1 -p 5432 -d XX

程序有效,但我也尝试了:

./C -i 127.0.0.1 -o 12345 -k 4 -j 2 -c 1 -p 5432 -d XX

失败! j 字母之间的唯一区别在于。我在最后一行之后的输出:print function print_usage()

如何让它发挥作用?

原因是你检查的是arc变量,每个情况都不一样,所以getopt甚至不参与。