Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

p2desai

macrumors newbie
Original poster
Jun 10, 2008
4
0
Hey all

I'm trying to write an application (simple TCP Server) that uses sockets. I'm using XCode. I'm unable to get my socket to bind to an address. I've posted my code... does anyone see any mistakes?

The app returns an error when I use the bind command... but I can't figure out why.

Code:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT    80


int main (int argc, const char * argv[]) 
{
    //____________________________________________
    // Create the server socket
    
    //create socket
    int dataRx;
    dataRx = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    //error checking
    if ( dataRx < 0 )
    {
        printf("\nerror -> Could not create socket");
        printf("\nExiting...");
        //sleep (3);
        return 0;
    }
    else {
        printf("\n->Created server socket"); }
        
        
    //____________________________________________
    // Get local machine (server) address info
    
    char myName [256];
    struct sockaddr_in local;
    struct hostent *hp;

    //get local host info
    gethostname (myName, sizeof(myName));                     
    printf("\n->This machine's hostname is: %s", myName);    
    hp = gethostbyname (myName);                        
    
    if (hp == NULL)
    {
        printf("\nerror -> Could not get local host info");
        printf("\nExiting...");
        return 0;
    }
    else {
        printf("\n->Got local host info"); }
    
    //fill in info
    local.sin_family = AF_INET;
    local.sin_port = htons (80);
    local.sin_addr.s_addr = htonl (INADDR_ANY);
    
    //____________________________________________
    // Bind socket to address
    
    int errCh = 0;
    errCh = bind (dataRx, (struct sockaddr*)&local, sizeof(local) );
    
    if (errCh < 0)
    {
        printf("\nerror -> Could not bind socket to address");
        printf("\n %d", errCh);
        printf("\nExiting...");
        return 0;
    }
    
    else {
        printf("\n->Socket bound to local address"); }
    
    //____________________________________________
    // Start listening for connections
    
    
    
    
    getchar();
    return 0;
}

TiA
-Preet
 

sord

macrumors 6502
Jun 16, 2004
352
0
Are you running this as root? I believe OS X blocks binding to all ports below 1024 for non-root users.

Also, make sure Web Sharing (Apache) isn't enabled.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Are you running this as root? I believe OS X blocks binding to all ports below 1024 for non-root users.

Also, make sure Web Sharing (Apache) isn't enabled.

I don't think web sharing has anything to do with it. Port 80 should be open in this occasion. However, in my situation, I must open port 80 in the port mapping settings of my airport express base station.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
you should be able to open the terminal and do a netstat to determine what ports are used and by whom.

The command is "netstat -an" on linux, it should be something similar on OS X.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.