Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2007
    Messaggi
    136

    [c] Socket:funzione connect

    Salve,
    ho scritto la seguente funzione che si appoggia su connect:

    codice:
    int connect_to_server(int *conn_fd,struct address_port addr_port){ 	
    int n; 	struct sockaddr_in serv_add; 
    memset((void*)&serv_add,0,sizeof(serv_add)); 	
    serv_add.sin_family=AF_INET; 	
    serv_add.sin_port=htons(addr_port.port); 	 	
    if( inet_pton(AF_INET,addr_port.addr,&serv_add.sin_addr) <= 0){ 		
    perror("inet_pton() error"); 		
    return -1; 	
    }  	
    if( (n=connect(*conn_fd,(struct sockaddr*)&serv_add,sizeof(serv_add))) < 0){ 		
    perror("connection error"); 		
    return -1; 	
    } 	
    return 0; 
    }
    Quando ad un indirizzo non è in ascolto nessun server la connect non ritorna.Come mai?Saluti.

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2010
    Messaggi
    466

    Re: [c] Socket:funzione connect

    Originariamente inviato da and77
    Quando ad un indirizzo non è in ascolto nessun server la connect non ritorna.Come mai?Saluti.
    Perchè dovrebbe ritornare?
    RETURN VALUES
    Upon successful completion, a value of 0 is returned. Otherwise, a value
    of -1 is returned and the global integer variable errno is set to indi-
    cate the error.

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2007
    Messaggi
    136
    Non dovrebbe avvisarmi che a quell'indirizzo non c'è alcun server in ascolto?Invece a me rimane bloccato nella funzione.

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2010
    Messaggi
    466
    Originariamente inviato da and77
    Non dovrebbe avvisarmi che a quell'indirizzo non c'è alcun server in ascolto?
    Si in effetti tramite la variabile errno:
    Errors
    ...
    ECONNREFUSED
    No one listening on the remote address.

    ...
    ENETUNREACH
    Network is unreachable.
    ...
    ETIMEDOUT
    Timeout while attempting connection. The server may be too busy to accept new connections. Note that for IP sockets the timeout may be very long when syncookies are enabled on the server.
    Comunque, hai provato a rivedere la condizione dell' if? Può anche dipendere da quello.. Per esempio, io la connect l'ho sempre richiamata così:
    codice:
    if((connect( ... , (struct sockaddr *)& ... , sizeof( ... ))) != -1)
       printf("Ok connect\n");
    else
    {
       perror ... 
       ...
    }
    Ed ha sempre funzionato, se non ricordo male, con condizioni diverse toppava..

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2007
    Messaggi
    136
    Ho fatto varie prove ed ho notato che se l'indirizzo è di tipo 192.168.1.n(cioè se mi trovo all'interno della stessa rete) con relativa porta la funzione ritorna in caso in cui il server non è in ascolto se invece l'indirizzo è di tipo 192.169.1.n questa non ritorna segnalando l'errore.Come mai?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.