Results 1 to 3 of 3

Thread: socket programming

  1. #1
    Join Date
    May 2013
    Beans
    27

    Angry socket programming

    hi all

    i wrote a sever with c. i put a non-terminate loop, but not work correctly.while loop not perform permanently.
    in this code, i want to if there were no person to connect, recieve message from other clients that conected already.
    For example: the last statement work a time.
    what is wrong?!
    what should i do?!

    Code:
        while(1)
        {
            int newSocket = accept(tcp_socket,(struct sockaddr*)&server_addr,&s);               
    
            if(newSocket<0)
            {
               // recv msg from client that conected beforhand
            }
    
            else
            {
                // if one client connect
    
            }
            write(1,"what\n",5);
        }

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,220
    Distro
    Kubuntu

    Re: socket programming

    You should take a look at this tutorial to guide you along the journey of learning socket programming.

    After doing a quick check of your "pseudo-code", I think you are misunderstanding as to how the accept() call behaves. If you are using a blocking socket (this is typical) to receive client connections, then the accept() will block indefinitely until a client connects. The accept() call will only return a value of less than 0 when an error occurs, and return a value greater than zero to indicate the socket descriptor of a client that has successfully connected.

    If you want to determine when a client is sending data, you should consider using select() or poll() to manage the various sockets you have. Always remember to manage the server's listen socket (ie. the one that accepts connections) and the sockets for any clients that are connected.

  3. #3
    Join Date
    May 2013
    Beans
    27

    Re: socket programming

    Tanks a lot.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •