Sending to client from server : <p>I'm trying to send data from a server to a client whenever the client executes a recv() command. As the code stands right now, I cannot get the client to print any data it receives. I cannot figure out whether something is wrong with my server or client, so any help would be appreciated. Thanks!</p>

<p>client.c</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;netdb.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netinet/ip.h&gt;


#define ECHO_PORT 9999
#define BUF_SIZE 4096

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        fprintf(stderr, "usage: %s &lt;server-ip&gt; &lt;port&gt;",argv[0]);
        return EXIT_FAILURE;
    }

    char buf[BUF_SIZE];

    int status, sock, sock2;
    struct addrinfo hints;
    memset(&amp;hints, 0, sizeof(struct addrinfo));
    struct addrinfo *servinfo; //will point to the results
    hints.ai_family = AF_INET;  //IPv4
    hints.ai_socktype = SOCK_STREAM; //TCP stream sockets
    hints.ai_flags = AI_PASSIVE; //fill in my IP for me

    if ((status = getaddrinfo(argv[1], argv[2], &amp;hints, &amp;servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo error: %s \n", gai_strerror(status));
        return EXIT_FAILURE;
    }

    if ((sock = socket(servinfo-&gt;ai_family, servinfo-&gt;ai_socktype, servinfo-&gt;ai_protocol)) == -1)
    {
        fprintf(stderr, "Socket failed");
        return EXIT_FAILURE;
    }

    if ((connect(sock, (struct sockaddr *) servinfo-&gt;ai_addr, servinfo-&gt;ai_addrlen)) != 0)
    {
        fprintf(stderr, "Connection failed");
        return EXIT_FAILURE;
    }

    if ((sock2 = socket(servinfo-&gt;ai_family, servinfo-&gt;ai_socktype, servinfo-&gt;ai_protocol)) == -1)
    {
        fprintf(stderr, "Socket failed");
        return EXIT_FAILURE;
    }

    if ((connect(sock2, (struct sockaddr *) servinfo-&gt;ai_addr, servinfo-&gt;ai_addrlen)) != 0)
    {
        fprintf(stderr, "Connection failed");
        return EXIT_FAILURE;
    }

    while (1) {
        //char msg[BUF_SIZE] = "ashudfshuhafhu";
        //char msg[BUF_SIZE]; 
        //fgets(msg, BUF_SIZE, stdin);
        //int i = 2;
        //if (strlen(msg) == i) 
        //    break;
        int bytes_received;
       // fprintf(stdout, "Sending %s", msg);
        //send(sock, msg , strlen(msg), 0);
        if((bytes_received = recv(sock, buf, BUF_SIZE, 0)) &gt; 1)
        {
            buf[bytes_received] = '\0';
            fprintf(stdout, "Received %s", buf);
        }
    }


    freeaddrinfo(servinfo);
    close(sock);    
    return EXIT_SUCCESS;

}
</code></pre>

<p>Server.c</p>

<pre><code>#include &lt;netinet/in.h&gt;
#include &lt;netinet/ip.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;unistd.h&gt;

#define ECHO_PORT 9999
#define BUF_SIZE 4096

int close_socket(int sock)
{
        if (close(sock))
        {
                fprintf(stderr, "Failed closing socket.\n");
                return 1;
        }
        return 0;
}

int main(int argc, char* argv[])
{
    int sock, client_sock;
    ssize_t readret;
    socklen_t cli_size;
    struct timeval tv;
    struct sockaddr_in addr, cli_addr;
    char buf[BUF_SIZE] = "wetwetwetwetwetwetwetwet";
    fd_set readfds, writefds;
    fd_set activereadfds, activewritefds;
    cli_size = sizeof(&amp;cli_addr);

    tv.tv_sec = 5;
    tv.tv_usec = 0;

    fprintf(stdout, "----- Echo Server -----\n");

    /* all networked programs must create a socket */
    if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    {
            fprintf(stderr, "Failed creating socket.\n");
            return EXIT_FAILURE;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(ECHO_PORT);
    addr.sin_addr.s_addr = INADDR_ANY;

    /* servers bind sockets to ports---notify the OS they accept connections */
    if (bind(sock, (struct sockaddr *) &amp;addr, sizeof(addr)))
    {
            close_socket(sock);
            fprintf(stderr, "Failed binding socket.\n");
            return EXIT_FAILURE;
    }


    if (listen(sock, 5))
    {
            close_socket(sock);
            fprintf(stderr, "Error listening on socket.\n");
            return EXIT_FAILURE;
    }

    FD_ZERO(&amp;readfds);
    FD_SET(sock, &amp;activereadfds);

    while (1)
    {
        fprintf(stdout,"in here.\n");
        readfds = activereadfds;
        writefds = activewritefds;
        FD_ZERO(&amp;activereadfds);
        FD_ZERO(&amp;activewritefds);

        if (select(51, &amp;readfds, &amp;writefds, NULL, &amp;tv) &lt; 0)
        {
            perror("select");
            return EXIT_FAILURE;
        }


        for (int i = 0; i &lt; 10; ++i)
        {
            if (FD_ISSET (i, &amp;readfds))
            {
                if (i == sock)
                {
                    fprintf(stdout, "main loop. \n");
                    client_sock = accept(sock, 
                                (struct sockaddr *) &amp;cli_addr, &amp;cli_size);
                    FD_SET(client_sock, &amp;activereadfds);
                    FD_SET(client_sock, &amp;activewritefds);
                    if (client_sock &lt; 0)
                    {
                        perror("accept");
                        return EXIT_FAILURE;
                    }
                } else {
                        fprintf(stdout, "second loop \n");
                        readret = send(i,buf, strlen(buf),0);

                        if (readret &lt; 0)
                            fprintf(stdout, "yay");
                        }
                }
            if (FD_ISSET(i, &amp;writefds))
                {   fprintf(stdout, "ugh \n");
                    readret = send(i,buf,BUF_SIZE,0);
                    //if (readret &gt; 0)
                    //while((readret = recv(i, buf, BUF_SIZE, 0)) &gt;= 1)
                    //  {

                                //if (send(i, buf, readret, 0) != readret)
                                //{
                                //  close_socket(i);
                                //  close_socket(sock);
                                //  fprintf(stderr, "Error sending to client.\n");
                                //  return EXIT_FAILURE;
                                //}
                }
            }
        }
    close_socket(sock);

    return EXIT_SUCCESS;
}
</code></pre>
