Professional Web Applications Themes

How to Specify port # in BSD Sockets? - Mac Programming

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

  1. #1

    Default How to Specify port # in BSD Sockets?

    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

    --
    Ed Guest

  2. #2

    Default Re: How to Specify port # in BSD Sockets?

    Ed Sutherland <rr.com> wrote:
     

    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.
    Paul Guest

  3. #3

    Default Re: How to Specify port # in BSD Sockets?

    Ed Sutherland <rr.com> wrote in message news:<010220041827239688%rr.com>... 

    I think the above line should be:
    serverAddress.sin_port = htons(110);
    Marcus Guest

Similar Threads

  1. ? Moving from port 8500 to port 80 on XP
    By b1boogie in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: February 27th, 12:19 AM
  2. sockets on an HPUX (thanks for the port scanner!)
    By Chris McMahon in forum PERL Beginners
    Replies: 1
    Last Post: January 9th, 08:38 PM
  3. IRC & SOCKETS
    By Paulo in forum PHP Development
    Replies: 2
    Last Post: October 7th, 10:48 PM
  4. Replies: 2
    Last Post: September 15th, 08:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139