c++ - Server Socket Compilation Error -


i having trouble debugging following code, throwing typecast error:

#include <stdio.h> #include <stdlib.h>      #include <string.h> #include <cstring> #include <sys/types.h>  #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <netdb.h>  int main(int argc , char *argv[]) {    int socket_desc , new_socket , c;    struct sockaddr_in server ,client;    char *message;     //create socket;    socket_desc = socket(af_inet , sock_stream , 0);    if (socket_desc == -1)    {       printf("could not create socket");    }     //prepare sock addr structure    server.sin_family = af_inet;    server.sin_addr.s_addr = inaddr_any;    server.sin_port = htons( 8888 );     //bind    if(bind(socket_desc, (struct sockaddr *)&server , sizeof(&server)) < 0)    {       printf("bind failed");    }    printf("bind done");     //listen    listen(socket_desc , 3);     //accept incoming connection    printf("waiting incoming connection ... ");     c =sizeof(struct sockaddr_in);    new_socket = accept(socket_desc, (struct sockadrr *)&client,sizeof(&client));    while(new_socket < 0);    {       puts("connection accepted");       message = "hello client ...recieved message \n";       write(new_socket , message , strlen(message));    }     if (new_socket < 0)    {       perror("accept failed");       return 1;    }     return 0; } 

here error:

error: cannot convert 'main(int, char**)::sockadrr*' 'sockaddr*' argument '2' 'int accept(int, sockaddr*, socklen_t*)'

the fish:

you have typo.

new_socket = accept(socket_desc, (struct sockadrr *)&client,sizeof(&client));                                          ^^^^^^^^ should sockaddr 

because of typo, unknown type struct sockadrr scoped function, in case main. so, main(int, char**)::sockadrr showed in error message.

how fish:

the error points out cannot convert sockadrr* sockaddr*. comparing 2 strings, becomes clear 2 types indeed different. in case, difference happens because of misspelling due typographical error.

sockadrr* sockaddr*       .      /|\       | 

when compiler complains "cannot convert", or "invalid conversion", indicates providing type incompatible expected. examine type providing in code, , determine more suitable type should used instead.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -