Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to Specify port # in BSD Sockets?

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  CFString interrupt safe?  
Author Message
Ed Sutherland

External


Since: Nov 30, 2003
Posts: 11



(Msg. 1) Posted: Sun Feb 01, 2004 5:27 pm
Post subject: How to Specify port # in BSD Sockets?
Archived from groups: comp>sys>mac>programmer>help (more info?)

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?

Ed

--
Back to top
Login to vote
Paul Mitchum

External


Since: Nov 22, 2003
Posts: 154



(Msg. 2) Posted: Sun Feb 01, 2004 5:27 pm
Post subject: Re: How to Specify port # in BSD Sockets? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.
Back to top
Login to vote
Marcus Boxall

External


Since: Nov 21, 2003
Posts: 7



(Msg. 3) Posted: Sun Feb 01, 2004 5:27 pm
Post subject: Re: How to Specify port # in BSD Sockets? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Ed Sutherland <digital.TakeThisOut@twcny.rr.com> wrote in message news:<010220041827239688%digital@twcny.rr.com>...
> serverAddress.sin_port = htons( (int *)"110" );
>
> What am I missing?
>
> Ed
>
> --

I think the above line should be:
serverAddress.sin_port = htons(110);
Back to top
Login to vote
Display posts from previous:   
Related Topics:
[Q] carbon, IO kit and serial port programming - i'm looking for some OSX code that talks to a serial port. I've found some Apple sample code but that's for a modem an...

Mac OS Classic port needed (Open Source SDL app for childr.. - Hello there! I'm the author of "Tux Paint," a paint program for kids that's released as Open Source (read: f...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]