Using C to create a Sockets application

Discussion in 'Mac Programming' started by p2desai, Aug 7, 2008.

  1. p2desai macrumors newbie

    Jun 10, 2008
    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.

    #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");
            //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");
            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);
            return 0;
        else {
            printf("\n->Socket bound to local address"); }
        // Start listening for connections
        return 0;
  2. sord macrumors 6502

    Jun 16, 2004
    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.
  3. Soulstorm macrumors 68000


    Feb 1, 2005
    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.
  4. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    If web sharing is active then it will be using port 80 itself (unless you change it in the httpd.conf file). In general it is best to not use use ports < 1024 for your own projects though unless you are using an already established protocol.
  5. pilotError macrumors 68020


    Apr 12, 2006
    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.

Share This Page