Thread: bind() error

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    bind() error

    Hey guys,
    I'm not sure if this is something wrong with xCode or my code itself (or perhaps a permissions issue in mac os x). Anyways my code looks like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>
    #include <errno.h>
    
    unsigned short port;
    int rSocket;
    int clientSocket;
    int serverSocket;
    
    struct sockaddr_in clientAddr;
    struct sockaddr_in serverAddr;
    
    struct equation
    {
    	float first, second;
    	char operation[15];
    };
    
    int main (int argc, const char * argv[]) {
    	if (argc < 2)
    	{
    		printf("Usage: calculator [port]\n--using: 65530\n");
    		port = 65530;
    	}
    	else
    		port = (unsigned short) atoi(argv[1]);
    	
    	memset(&serverAddr, 0, sizeof(serverAddr));
    	memset(&clientAddr, 0, sizeof(clientAddr));
    	serverAddr.sin_family = AF_INET;
    	serverAddr.sin_port = htons(port);
    	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    	serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    	//Bind
    	if (bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 1)
    	{
    		printf("Error: bind()\nError Number:%d\n", errno);
    		exit(-1);
    	}
    	if (listen(serverSocket, 1) < 0)
    	{
    		printf("Error: listen()\nError Number:%d\n", errno);
    		exit(-1);
    	}
    	for (;;)
    	{
    		unsigned int length = sizeof(clientAddr);
    		if (clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &length) < 0)
    		{
    			printf("error: accept()\n");
    			exit(-1);
    		}
    		struct equation current;
    		printf("%d\n", sizeof(current));
    		int bR = 0;
    		if (recv(clientSocket, &current, sizeof(current), 0) < 0)
    		{
    			printf("Error: recv()");
    			exit(-1);
    		}
    		printf("bR: %d\n", bR);
    		printf("%f %s %f\n", current.first, current.operation, current.second);
    	}
    }

    Running yields this:
    Code:
    sudo ./calculator
    Usage: calculator [port]
    --using: 65530
    Error: bind()
    Error Number:0
    TIA

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    bind returns 0 on success, yet you consider 0 to be a failure.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    I figured that out shortly after posting, and a little banging my head against the wall.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread