2011-02-03 17 views
0

次のコードで、クライアントに接続しようとすると、サーバーに次のエラーが表示されます。 "引数が無効です"、エラーが表示されません。conectionを受け入れる際に「無効な引数」が表示されるのはなぜですか?

if((l_sock=socket(AF_INET,SOCK_STREAM,0))!=-1) 
{ 
    struct sockaddr_in srv_dir; 

    srv_dir.sin_family=AF_INET; 
    srv_dir.sin_port=8500; 
    srv_dir.sin_addr.s_addr=INADDR_ANY; 

    if((bind(l_sock,(struct sockaddr *)&srv_dir,sizeof(struct sockaddr_in)))!=-1) 
    { 
     if(!(listen(l_sock,5))) 
     { 
      signal(SIGINT,cerraje); 
      int t_sock; 
      struct sockaddr_in cli_dir; 
      socklen_t tam; 
      time_t tstmp; 
      struct tm * res; 
      res=(struct tm *)malloc(sizeof(struct tm)); 


      while(!key) 
      { 
       if((t_sock=accept(l_sock,(struct sockaddr *)&cli_dir,&tam))!=-1) 
       { 
        tstmp=time(&tstmp); 
        res=gmtime(&tstmp); 
        send(t_sock,res,sizeof(struct tm),0); 
        wr_hora(*res,cli_dir.sin_addr);   
       } 
       else 
        perror("Petición no atendida");//The error is printed here. 

ありがとうございました。

答えて

9

accept(2)上のドキュメントを読む:

The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in.

をだからsizeof(cli_dir)acceptに渡さtamの値を初期化する必要があります。あなたは未定義のメモリを渡しているので、ソケットライブラリがあなたのエラーを捕まえることができて幸運です。その結果、未定義の動作が起こります。

+0

これは、ありがとうございます。変数tamは出力値だけだったので、初期値が必要だとは思っていません。 –

関連する問題