zachpescitelli
zachpescitelli

Reputation: 13

C++ Network program runs but give no output

My simple program gives no errors through the compiler and runs fine but it does not give the output it is supposed too until someone is connected. I have done a good bit of research and editing but can not figure it out.Also how do I let more than one person connect? Any help to get this to work would be appreciated. Thanks in advance!!! Code is below.

#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>

char msg[20];

using namespace std;

int main(int argc, char *argv[])
{
    cout << "Made it to main!";

    int listener_d = socket(PF_INET, SOCK_STREAM, 0);

    struct sockaddr_in name;
    name.sin_family = PF_INET;
    name.sin_port = (in_port_t)htons(30000);
    name.sin_addr.s_addr = INADDR_ANY;
    if(bind (listener_d, (struct sockaddr *) &name, sizeof(name)) == -1)
    {
        cout << "Can't bind the port!";
    }
    else
    {
        cout << "The port has been bound.";
    }

    listen(listener_d, 10);
    cout << "Waiting for connection...";

    while(1)
    {
        struct sockaddr_storage client_addr;
        unsigned int address_size = sizeof(client_addr);
        int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);
        cin >> msg;

        send(connect_d, msg, strlen(msg), 0);
    }
    return 0;
}

Upvotes: 0

Views: 317

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283644

Maybe you should try flushing the output.

std::cout << "Waiting for connection..." << std::flush;

Upvotes: 2

Related Questions