LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Socket Programming problem (https://www.linuxquestions.org/questions/programming-9/socket-programming-problem-395145/)

lucky6969b 12-21-2005 01:24 AM

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

lucky6969b 12-21-2005 01:44 AM

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();

clinux_rulz 12-25-2005 07:59 AM

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.

nodger 12-25-2005 09:37 AM

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))

enthusiast_abdul 12-27-2005 12:44 AM

the third arg. of connect should by sizeof(serverINETAddress).


All times are GMT -5. The time now is 08:16 AM.