Help with Networking

Ok so I'm using SFML and C++ to do some networking, and I was following a video tutorial on youtube and the program is a chat function, it chats, but what happens is it only allows one person to type unless the other person sends them a message, I want to be able to send messages without having to have the other person send one first, what do I do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>

#include "Prototypes.h"

using namespace std;

int main()
{
    sf::IpAddress ip = sf::IpAddress::getLocalAddress();
    sf::TcpSocket socket;
    string mode;
    char connectionType;
    char buffer[2000];
    size_t recieved;
    string text = "Connected to: ";

    cout << "Enter (s) for server, Enter (c) for client" << endl;
    cin >> connectionType;

    if(connectionType == 's')
    {
        cout << "Server connection type chosen" << endl;
        sf::TcpListener listener;
        listener.listen(2000);
        listener.accept(socket);
        text += "Server";
        mode = "send";
    }
    if(connectionType == 'c')
    {
        cout << "Client connection type chosen" << endl;
        socket.connect(ip, 2000);
        text += "Client";
        mode = "recieve";
    }

    socket.send(text.c_str(), text.length() + 1);

    socket.receive(buffer, sizeof(buffer), recieved);

    bool done = false;

    while(!done)
    {
        if(mode == "send")
        {
            cout << ">";
            getline(cin, text);
            socket.send(text.c_str(), text.length() + 1);
            mode = "recieve";
        }
        else if(mode == "recieve")
        {
            socket.receive(buffer, sizeof(buffer), recieved);
            if(recieved > 0)
            {
                cout << "Recieved: " << buffer << endl;
                mode = "send";
            }
        }
    }

    cin.get();
}
IIANM, for full duplex communication you need two connections.
1. A listens.
2. B connects to A.
3. B listens.
4. A connects to B.
In each connection, the client (the process that was initially listening) talks to the server, and the server responds with token acknowledgement replies.
Topic archived. No new replies allowed.