Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C linux中的Getopt_C_Linux_Getopt - Fatal编程技术网

C linux中的Getopt

C linux中的Getopt,c,linux,getopt,C,Linux,Getopt,我的代码的一小部分: #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/

我的代码的一小部分:

#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;
}
#包括//printf
#include//memset
#包括//退出(0);
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义BUFLEN 100//缓冲区的最大长度
空模(字符*s)
{
佩罗尔(s);
出口(1);
}
Wiadomosc结构{
内努克西吉;
内杰德诺斯卡;
int czas;
国际港口;
};
字符*str2md5(常量字符*str,整数长度){
int n;
MD5_-CTX-c;
无符号字符摘要[16];
char*out=(char*)malloc(33);
MD5_Init(&c);
而(长度>0){
如果(长度>512){
MD5_更新(&c,str,512);
}否则{
MD5_更新(&c、str、长度);
}
长度-=512;
str+=512;
}
MD5_最终版(摘要和c);
对于(n=0;n<16;++n){
snprintf(&(out[n*2]),16*2,“%02x”,(unsigned int)摘要[n]);
}
返回;
}
void wyslij_udp(int端口,常量字符*地址服务器,结构Wiadomosc moja);
int polacz_tcp(int端口,常量字符*服务器地址);
int-pobierz_参数(char*optarg)
{
char*endptr;
int-val;
val=(int)strtol(optarg和endptr,0);
如果(endptr==optarg)
{
fprintf(标准,“Niepoprawny paramtr funkcji”);
退出(退出失败);
}
返回val;
}
无效打印_用法(){
printf(“用法:\n./client-i numer\u ip-o numer\u portu-k numer\u ksiegi-j jednostka\n-c czas-p port-d plik\u wynikowy\n\n numer\u ip-numer-ip-servera\n-o-numer portu serwera\n numer\u ksiegi-numer ksiegi Pana Tadeusza\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\n港口-Otzymywanch komunikatow”;
}
int main(int argc,char*argv[])
{
int选项=0;
int ksiega=-1,jednostka=-1,czas=-1,port=-1,port2=-1;
int sockfd=0,n=0;
字符*行;
尺寸长度=0;
char recvBuff[LINE_MAX];
char*adres;
Wiadomosc moja结构;
int-fd;
char*plik_wynikowy;
如果(argc!=15)
{
打印_用法();
退出(退出失败);
}
而((option=getopt(argc,argv,“k:j:c:p:i:o:d:”)!=-1){
开关(选件){
案例“i”:
如果(斯特伦(optarg)<7 | |斯特伦(optarg)>21)
{
fprintf(标准,“niepoprawny地址ip\n”);
退出(退出失败);
}
adres=optarg;
打破
案例“o”:
port2=pobierz_参数(optarg);
如果(端口2<0 | |端口2>65535)
{
fprintf(标准,“niepoprawna wartosc ksiegi!\n”);
退出(退出失败);
}
打破
案例“k”:
ksiega=pobierz_参数(optarg);
如果(ksiega<0 | | ksiega>12)
{
fprintf(标准,“niepoprawna wartosc ksiegi!\n”);
退出(退出失败);
}
打破
案例“j”:
jednostka=pobierz_参数(optarg);
如果(jednostka<1 | | jednostka>3)
{
fprintf(stderr,“niepoprawna wartosc jednostki!\n”);
退出(退出失败);
}
打破
案例“c”:
czas=pobierz_参数(optarg);
如果(czas<0 | | czas>2000)
{
fprintf(stderr,“尼波普拉瓦-瓦托斯-捷克!\n”);
退出(退出失败);
}
打破
案例“p”:
port=pobierz_参数(optarg);
如果(端口<0 | |端口>65535)
{
fprintf(标准,“尼波普拉纳-瓦托斯港!\n”);
退出(退出失败);
}
打破
案例“d”:
如果(strlen(optarg)<2)
{
fprintf(stderr,“niepoprawny plik wynikowy”);
退出(退出失败);
}              
plik_wynikowy=optarg;
打破
默认值:print_usage();
退出(退出失败);
}
}
moja.nr_ksiegi=ksiega;
moja.jednostka=jednostka;
moja.czas=czas;
moja.port=端口;
wyslij_udp(端口2,地址,moja);
//现在等待连接-享受!
睡眠(1);
fd=开放(plik_wynikowy,O_WRONLY | O|u CREAT | O|u TRUNC);
memset(recvBuff,0,sizeof(recvBuff));
sockfd=polacz_tcp(5000,adres);
fprintf(标准格式,“Trwa传输…\n”);
而((recv(sockfd,recvBuff,sizeof(recvBuff),0))>0)
{
char*wiad;
//recvBuff[n]=0;
写入(fd,recvBuff,n);
/*if(fprintf(stdout,“%s”,recvBuff)=EOF)
{
佩罗(“fprintf”);
}
fflush(stdout);
*/  
//奥德布拉尔-特拉斯威斯拉克
wiad=str2md5(recvBuff,strlen(recvBuff));
fprintf(stdout,“Wiadomosc%s\n”,recvBuff);
如果(发送(sockfd,wiad,33,0)=-1)
{
fprintf(stderr,“发送消息失败”);
}
免费(wiad);
bzero(recvBuff,sizeof(recvBuff));
}

如果(n原因是您正在检查
arc
变量,这在每种情况下都是不同的,因此getopt甚至不参与其中。

请提供一个。第一次调用和第二次调用之间的唯一区别似乎是
-j
之后的空格(如果您明确指出这一点也会有所帮助)。但您甚至还没有显示处理该选项的代码。好的,我添加了更多代码并指出了错误所在。:)这远远不是最简单的工作示例。好的,现在应该是确定的。代码应该是演示问题所需的最短代码。