View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-21-2005, 01:24 AM   #1
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
Socket Programming problem

serverSockAddrPtr = (struct sockaddr*) &serverINETAddress;

serverLen = sizeof (serverINETAddress);

bzero ((char *)&serverINETAddress, sizeof (serverINETAddress));
inetAddress = nameToAddr ("");
serverINETAddress.sin_family = AF_INET;
serverINETAddress.sin_addr.s_addr = inetAddress;
serverINETAddress.sin_port = htons(HTTP_PORT);

// create unnamed socket

// 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.

Old 12-21-2005, 01:44 AM   #2
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
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();
Old 12-25-2005, 07:59 AM   #3
Registered: Nov 2005
Distribution: Gentoo
Posts: 51

Rep: Reputation: 16
Thats not the problem, because the std::string class's constructor already allocates space and copies the literal string data over to itself.

Does have a HTTP server running like "apache"?
Or does 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.

Last edited by clinux_rulz; 12-25-2005 at 08:01 AM.
Old 12-25-2005, 09:37 AM   #4
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
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))
Old 12-27-2005, 12:44 AM   #5
LQ Newbie
Registered: Oct 2005
Location: India
Distribution: Redhat 9.0
Posts: 20

Rep: Reputation: 0
the third arg. of connect should by sizeof(serverINETAddress).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
pthread socket programming problem hk_michael Programming 1 04-03-2005 08:54 PM
socket programming???? harbir Linux - Networking 2 07-05-2004 02:52 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM
Socket programming problem krivi Programming 3 01-14-2002 02:04 AM

All times are GMT -5. The time now is 06:12 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration