Error with bind()

This is a discussion on Error with bind() within the Networking/Device Communication forums, part of the General Programming Boards category; Hi. I recently started learning network programming (from Beej's guide); here's a simple server I'm trying to make - its ...

  1. #1
    zvn
    zvn is offline
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Error with bind()

    Hi. I recently started learning network programming (from Beej's guide); here's a simple server I'm trying to make - its supposed to send a file to a specified port (both file_path and port number are provided from the terminal. I am getting a warning "server.c:78: warning: ‘client_address’ may be used uninitialized in this function" though it compiles. When I try to run it I get an error with bind(), but I'm not sure what's wrong with it.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <assert.h>
    #include <stdlib.h>
    
    int main(int argc, char** argv)
    {
           /*Return in case of insufficient input*/ 
    	if(argc!=3)
    	{
    		fprintf(stderr,"Usage: server path_to_file port_number");
    		return 1;
    	}
    	
    		
    	struct addrinfo hints,*res;
    	
    	memset(&hints,0,sizeof(hints));
           
            /*Setup hints for getaddrinfo*/
    	hints.ai_family=AF_UNSPEC;
    	hints.ai_flags=AI_PASSIVE;
    	hints.ai_socktype=SOCK_STREAM;
    	
    	int status;
    	
    	status=getaddrinfo(NULL,argv[2],&hints,&res);
    	if(status!=0)
    	{
    		fprintf(stderr,"Error with getaddrinfo");
    		return 2;
    	}
    	
    	int fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
    	assert(fd!=-1);
    	
            /*Something wrong here?*/
    	status=bind(fd,res->ai_addr,res->ai_addrlen);
    	
    	if(status!=0)
    	{
    		fprintf(stderr,"Error with bind");
    		return 3;
    	}
    	
           /*Listen for incoming connections*/
    	status=listen(fd,10);
    	
    	if(status!=0)
    	{
    		fprintf(stderr,"Error with listen");
    		return 4;
    	}
    	
           /*accept*/
    	struct sockaddr *client_address;
    	socklen_t *client_addrlen;
    	int new_fd=accept(fd,client_address,client_addrlen);
    	assert(new_fd!=-1);
    	
           /*Open file that is to be sent*/
    	FILE  *file_to_send=fopen(argv[1],"r");
    	assert(file_to_send!=NULL);
    	
            /*Buffer for storing bytes*/
    	void *buff=malloc(500);
    	
    	fread(buff,1,250,file_to_send);
    	
    	send(new_fd,buff,250,0);
    	freeaddrinfo(res);
    	fclose(file_to_send);
    	
    	return 0;
    	
    }
    Thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Quote Originally Posted by manual_page
    RETURN VALUES

    Upon successful completion, a value of 0 is returned. Otherwise, a value
    of -1 is returned and the global integer variable errno is set to indi-
    cate the error.

    So look in errno to find out why!
    man page bind section 2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    zvn
    zvn is offline
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    I sort of got this thing working now..thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 12:58 PM
  2. What does bind() do, exactly?
    By Hunter2 in forum Networking/Device Communication
    Replies: 9
    Last Post: 07-07-2005, 12:12 PM
  3. Bind();
    By SirCrono6 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-26-2005, 07:01 PM
  4. bind
    By chrismiceli in forum Linux Programming
    Replies: 3
    Last Post: 08-30-2003, 11:44 PM
  5. bind() error
    By PutoAmo in forum C Programming
    Replies: 5
    Last Post: 05-23-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21