Creating a TCP Socket Loop in C++
<p>I have some problems creating a TCP socket loop. When all the packets were sent the program ends, still if everything is inside in a while(1) loop. A second solution which is working but not how it should(I have many looses) it was to add a for loop inside that while(1) loop until it sends all 4 buffers.</p>

<p><strong>client.cpp</strong></p>

<pre><code>    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;stdint.h&gt;
    #include &lt;unistd.h&gt;
    #include &lt;string.h&gt;
    #include &lt;sys/types.h&gt;
    #include &lt;sys/socket.h&gt;
    #include &lt;netinet/in.h&gt;
    #include &lt;netdb.h&gt;
    #include &lt;arpa/inet.h&gt;
    #include &lt;sys/wait.h&gt;
    #include &lt;signal.h&gt;


    #define SOCKET_PORT                7521
    #define SOCKET_SERVER            "127.0.0.1"
    #define SOCKET_MAXDATASIZE        4800
    #define SOCKET_BACKLOG          10

    int main(int argc, char *argv[]) {
    int socket_sockfd;

    int socket_port;
    int socket_data;
    struct sockaddr_in server_addr;
    struct hostent *socket_server;

    //Define the frame buffers, each 1/4 of 4800 because the maximum dimension of a packet is around 1500 bytes; 1200 will work just fine
    uint8_t socket_buffer_a[SOCKET_MAXDATASIZE/4];
    uint8_t socket_buffer_b[SOCKET_MAXDATASIZE/4];
    uint8_t socket_buffer_c[SOCKET_MAXDATASIZE/4];
    uint8_t socket_buffer_d[SOCKET_MAXDATASIZE/4];
    //Open the main camera port; port "0"

    printf("\n\n");

    //If no arguments are introduced set the "default" host and port.
    if (argc &lt; 3) {
        printf("USAGE: hostname port\nInitializing connection with default parameters:\n  HOST: 127.0.0.1\n  PORT: 7521\n\n");
        socket_port = SOCKET_PORT;
    } else {
        //Convert to int the arg[2] (the port)
        socket_port = atoi(argv[2]);
    }

    //Create socket
    socket_sockfd = socket(AF_INET, SOCK_STREAM, 0);    //The sockets returns a "valid" number

    //Check if the socket can be opened
    if (socket_sockfd &lt; 0) {printf("ERROR: The socket cannot be opened\nExiting...\n\n"); exit(1);}

    //Set the server
    if (argc &lt; 3) {
        //Default server defined above
        socket_server = gethostbyname(SOCKET_SERVER);
    } else {
        //Server inserted by user
        socket_server = gethostbyname(argv[1]);
    }

    if (socket_server == NULL) {printf("ERROR: No such server\nExiting...\n\n"); exit(1);}

    //Set all arguments to "0"
    bzero((char*) &amp;server_addr, sizeof(server_addr));

    //Set the connection type, the port and server address
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SOCKET_PORT);
    server_addr.sin_addr = *((struct in_addr *)socket_server-&gt;h_addr);
    memset(&amp;(server_addr.sin_zero), '\0', 8);

    //Connect to server and check the connection
    if (connect(socket_sockfd,(struct sockaddr *) &amp;server_addr, sizeof(server_addr)) &lt; 0) {printf("ERROR: Connection failed.\nExiting...\n\n"); exit(1);}


while(1) {


                        //Receive the buffer A
                        socket_data = recv(socket_sockfd, socket_buffer_a, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received;
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the buffer B
                        socket_data = recv(socket_sockfd, socket_buffer_b, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}



                        //Receive the buffer C
                        socket_data = recv(socket_sockfd, socket_buffer_c, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the buffer D
                        socket_data = recv(socket_sockfd, socket_buffer_d, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the min, max and center temperatures
                        socket_data = recv(socket_sockfd, socket_temperatures, 24, 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}

}




    return 0;

}
</code></pre>

<p><strong>server.cpp</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;signal.h&gt;


#define SOCKET_MAXDATASIZE        4800
#define SOCKET_PORT                7251
#define SOCKET_BACKLOG            10




int main(int argc, char *argv[])
{

//Socket stuffs
int socket_sockfd;
int socket_newsockfd;
int socket_port;
int socket_data;
int socket_binding;

//Socket buffers; max. data length which can be sent at once is around 1500 bytes, so 1200 will work just fine
uint8_t socket_buffer_a[SOCKET_MAXDATASIZE/4];
uint8_t socket_buffer_b[SOCKET_MAXDATASIZE/4];
uint8_t socket_buffer_c[SOCKET_MAXDATASIZE/4];
uint8_t socket_buffer_d[SOCKET_MAXDATASIZE/4];

uint8_t    socket_buffer_select[6];


socklen_t socket_clientlength;
struct sockaddr_in server_address, client_address;



printf("\n\n");

//If no arguments are introduced set the "default" host and port.
if (argc &lt; 3) {
    printf("USAGE: port\nInitializing connection with default parameters\n  PORT: 7521\n\n");
    socket_port = 7521;
} else {
    //Convert to int the arg[2] (the port)
    socket_port = atoi(argv[2]);
}

//Create socket
socket_sockfd = socket(AF_INET, SOCK_STREAM, 0);

//Check if the socket can be opened
if (socket_sockfd &lt; 0) {printf("ERROR: The socket cannot be opened\nExiting...\n\n"); exit(1);}

//Set all arguments to "0"
bzero((char *) &amp;server_address, sizeof(server_address));

//Set the arguments for server
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(socket_port);

memset(&amp;(server_address.sin_zero), '\0', 8);

//Bind the socket
socket_binding = bind(socket_sockfd, (struct sockaddr *) &amp;server_address, sizeof(server_address));
if (socket_binding &lt; 0) {printf("ERROR: Cannot bind the socket\nExiting...\n\n");exit(1);}

//Init the connection and accept the client
listen(socket_sockfd, SOCKET_BACKLOG);


//Accept the client
socket_clientlength = sizeof(client_address);
socket_newsockfd = accept(socket_sockfd, (struct sockaddr *) &amp;client_address, &amp;socket_clientlength);
if (socket_newsockfd &lt; 0) {printf("ERROR: Client not accepted\nExiting...\n\n");exit(1);}

while(1){


                    //Send buffer A
                    socket_data = send(socket_newsockfd, socket_buffer_a, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}



                    //Send buffer B
                    socket_data = send(socket_newsockfd, socket_buffer_b, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}



                    //Send buffer C
                    socket_data = send(socket_newsockfd, socket_buffer_c, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}

                    //Send buffer D
                    socket_data = send(socket_newsockfd, socket_buffer_d, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}
                    break;

}
//Close the socket
close(socket_newsockfd);
close(socket_sockfd);
return 0; 

}
</code></pre>

<p>And what I tried looks something like this:</p>

<p><strong>server.cpp</strong></p>

<pre><code>while(1){

        for(int i = 0; i &lt; 4; i++){
                    //Send buffer A
                    socket_data = send(socket_newsockfd, socket_buffer_a, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}



                    //Send buffer B
                    socket_data = send(socket_newsockfd, socket_buffer_b, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}



                    //Send buffer C
                    socket_data = send(socket_newsockfd, socket_buffer_c, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}

                    //Send buffer D
                    socket_data = send(socket_newsockfd, socket_buffer_d, (SOCKET_MAXDATASIZE/4), 0);
                    if (socket_data &lt; 0) {printf("ERROR: Data not sent\nExiting...\n\n");exit(1);}
                    break;
        }
}
</code></pre>

<p><strong>client.cpp</strong></p>

<pre><code>while(1) {

        for(int i = 0; i &lt; 4; i++){
                        //Receive the buffer A
                        socket_data = recv(socket_sockfd, socket_buffer_a, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received;
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the buffer B
                        socket_data = recv(socket_sockfd, socket_buffer_b, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}



                        //Receive the buffer C
                        socket_data = recv(socket_sockfd, socket_buffer_c, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the buffer D
                        socket_data = recv(socket_sockfd, socket_buffer_d, (SOCKET_MAXDATASIZE/4), 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}


                        //Receive the min, max and center temperatures
                        socket_data = recv(socket_sockfd, socket_temperatures, 24, 0); //Return the number with received data
                        //Check if the data has been received
                        if (socket_data &lt; 0) {printf("ERROR: Failed to receive data from server\nExiting...\n\n"); exit(1);}
        }

}
</code></pre>

<p>Thanks!</p>