Accepting Sockets with accept()

This is a discussion on Accepting Sockets with accept() within the Networking/Device Communication forums, part of the General Programming Boards category; Code: if ((new_socket = accept(master_socket, (struct sockaddr *)&address, sizeof(address);/*&addrlen*/))<0) If i didnt use new_socket what would i use to send ...

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    Question Accepting Sockets with accept()

    Code:
    if ((new_socket = accept(master_socket, (struct sockaddr *)&address, sizeof(address);/*&addrlen*/))<0)
    If i didnt use new_socket what would i use to send and recv from?
    I mean how does accept work? Do i have to make new_socket equal accept?
    or can i just send information to mastersocket and exclude the over bearing
    integer new_socket.


    oh one more thing because i havent compiled my program yet I was wondering if
    sizeof(address) would take the place of addrlen=sizeof(address) as I posted above.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by linuxrelik View Post
    Code:
    if ((new_socket = accept(master_socket, (struct sockaddr *)&address, sizeof(address);/*&addrlen*/))<0)
    If i didnt use new_socket what would i use to send and recv from?
    I mean how does accept work? Do i have to make new_socket equal accept?
    or can i just send information to mastersocket and exclude the over bearing
    integer new_socket.
    If you didn't use new_socket, you couldn't send or recv anything. The socket you pass to accept has to have had bind() and listen() applied to it, so it's a server socket which can have many connections. It represents the socket at your local machine. The socket returned from accept() (assuming the call was successful) represents a connection to a remote peer. Although they are both represented by file descriptors, they are very different things. If you don't use the return value from accept(), you won't be able to talk to the remote peer in any way.


    Quote Originally Posted by linuxrelik View Post
    oh one more thing because i havent compiled my program yet I was wondering if
    sizeof(address) would take the place of addrlen=sizeof(address) as I posted above.
    You can't just pass in sizeof(address), because the third argument to accept() is a pointer to type socklen_t. Do something like this:

    Code:
    struct sockaddr addr;
    socklen_t addrlen = sizeof(addr);
    if ((new_socket = accept(master_socket, &addr, &addrlen)) < 0) {
        ...

  4. #4
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    Red face You did understand the question right?

    I just wanted to mkae sure you understand the question and gave me the correct answer. if i use accept without using new_socket as the intiger then i cant send to mastersocket instead of sending to new_socket.

    By the way thanks for you help.

  5. #5
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    and hows this lookf so far?

    Code:
    int mastersocket;
    fd_set descrip;
    int activity, user;	
    int client_socket[700];
    int maxclients = 700;
    
    if (mastersocket=socket(AF_INET,SOCK_STREAM
    {
    	fatal("Creating mastersocket, AF_INET SOCK
    	exit(EXIT_FAILURE);  }
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(port);            
    /*Binding a socket callled  mastersocket */
    if(bind(mastersocket,(struct sockaddr *)&ad
    {
    	fatal("Binding mastersocket, AF_INET SOCK_
    	exit(EXIT_FAILURE);  }
    /* Listening to mastersocket */
       	if(listen(mastersocket, 3) == -1) 
    {
    	fatal("Listening to mastersocket, AF_INET 
    	exit(EXIT_FAILURE);  }
    fcntl(mastersocket, F_SETFL, FNDELAY);

  6. #6
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    I got another question:)

    Does anyone here know of a duplicate function like sleep
    that accepts numbers like .5 or 1.75?

  7. #7
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    And are these acceptable?
    Code:
    if (send(new_socket, message, strlen(message), 0) != strlen(message))
                    fatal("Sending to new_socket");
    
    if ((new_socket = accept(mastersocket, (struct sockaddr *)&address, &addrlen))<0)
    Last edited by errigour; 11-09-2010 at 10:07 AM.

  8. #8
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Can i ask one last question while i'm burdining you?

    if I use this function
    send(new_socket, message, strlen(message), 0) != strlen(message)

    What happens to the returned number if I don't use the if statement?
    would there be a -1 located somewhere on my hardrive?
    Last edited by errigour; 11-09-2010 at 09:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. accept()
    By ~Kyo~ in forum Networking/Device Communication
    Replies: 11
    Last Post: 11-28-2009, 07:57 PM
  3. Help with multithreaded sockets program
    By ceclauson in forum C Programming
    Replies: 3
    Last Post: 12-13-2008, 03:15 PM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 04:59 PM

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