Ed Sutherland <digital DeleteThis @twcny.rr.com> wrote:
> I'm using BSD's open() to get my host's socket. The trouble is passing
> it a specific port -- when I try the app, it returns with "connection
> refused." I try the same with telnet -- telnet myhost.com 110 -- and it
> works fine. I specify the port in the socket structure. Here are the
> two pertinent lines:
>
> serverAddress.sin_port = htons( (int *)"110" );
>
> inet_pton( AF_INET, "mail.twcny.rr.com", &serverAddress.sin_addr );
>
> if ( connect( sockfd, (struct sockaddr *)&serverAddress,
> sizeof(serverAddress)) < 0 ) {
> perror( "connect" );
> exit(1);
> }
>
>
> What am I missing?
The port should be an unsigned short, not a pointer to an int. Also,
inet_pton() is expecting a dotted-quad string (like "127.0.0.1"). And,
kinda important: you never actually create the socket with socket()
before you connect().
Here's a Mac-oriented tutorial on sockets:
<http://makeashorterlink.com/?U23516447>
There are a whole bunch of BSD socket tutorials out there on the web,
and they're not hard to find.
HTH.