LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-21-2005, 01:24 AM   #1
lucky6969b
Member
 
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 ("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
 
Old 12-21-2005, 01:44 AM   #2
lucky6969b
Member
 
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
clinux_rulz
Member
 
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 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.

Last edited by clinux_rulz; 12-25-2005 at 08:01 AM.
 
Old 12-25-2005, 09:37 AM   #4
nodger
Member
 
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
enthusiast_abdul
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).
 
  


Reply


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 09:48 AM.

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