c++ - UDP client-server program compiles using gcc but not g++ -


server:

int main(int argc, char *argv[]) {     struct sockaddr_in client, server;     int s, i=0;     socklen_t n;     char buf[31];     s=socket(af_inet,sock_dgram,0);     server.sin_family=af_inet;     server.sin_port=atoi(argv[1]);     inet_pton(af_inet, "localhost", &(server.sin_addr));     bind(s,(struct sockaddr *)&server,sizeof(server));     n=sizeof(client);      while(1) {         recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&client, &n);         printf("%s", buf);     }     close(s);     return 0; } 

client:

int main (int argc, char *argv[]) {     struct sockaddr_in client, server;      int s;     socklen_t n;     char buf[31];     char data[23];     s=socket(af_inet,sock_dgram,0);     server.sin_family=af_inet;     server.sin_port=atoi(argv[2]);     inet_pton(af_inet, argv[1], &(server.sin_addr));      n=sizeof(server);      while(1) {         data[0] = 't';         data[1] = 'e';         data[2] = 's';         data[3] = '\0';         sendto(s, data, strlen(data), 0, (struct sockaddr *) &server, n);     }     close(s);     return 0; } 

this works fine when compiled using gcc instead of g++. since want use separate c++ class file, these need compiled in g++.

what missing?

here's big problem lead problems:

inet_pton(af_inet, "localhost", &(server.sin_addr)); 

the inet_pton function wants ip address not hostname. function no name lookup. if checked errors have noticed how failed.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -