Socket Programming problem
serverSockAddrPtr = (struct sockaddr*) &serverINETAddress;
serverLen = sizeof (serverINETAddress); bzero ((char *)&serverINETAddress, sizeof (serverINETAddress)); inetAddress = nameToAddr ("192.168.0.124"); serverINETAddress.sin_family = AF_INET; serverINETAddress.sin_addr.s_addr = inetAddress; serverINETAddress.sin_port = htons(HTTP_PORT); // create unnamed socket clientfd = socket (AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL); // connect to host do { result = connect (clientfd, serverSockAddrPtr, sizeof (serverSockAddrPtr)); } while (result == -1); //////////// I was stuck here, result always -1, the word 'connect' is in blue color. Thanks Jack |
if I say I pass nameToAddr as char * while the prototype requires std::string. Would that be a problem? how do you translate char * to std::string
I only know the opposite way s.c_str(); |
Thats not the problem, because the std::string class's constructor already allocates space and copies the literal string data over to itself.
Does 192.168.0.124 have a HTTP server running like "apache"? Or does 192.168.0.124 have server code running and binded to the http port (port 80 i think)? It is posible that your code does work, but has nothing to connect to. |
The reason connect() is returning -1 is because you're passing in the length of the pointer to the address rather than the length of the sockaddr structure itself.
try changing it to: connect (clientfd, serverSockAddrPtr, sizeof (struct sockaddr)) |
the third arg. of connect should by sizeof(serverINETAddress).
|
All times are GMT -5. The time now is 08:16 AM. |