forked from JelleClaes2/P2P-chatServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpclient.cpp
131 lines (115 loc) · 3.8 KB
/
tcpclient.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "tcpclient.h"
//#include "CreateJson.h"
#include <QCoreApplication>
#include <QObject>
#include <QTcpSocket>
#include <qtcpserver.h>
#include <QAbstractSocket>
#include <QDebug>
#include <QInputDialog>
#include <QApplication>
#include <iostream>
#include <sstream>
#include <string>
#include <QtWidgets>
TcpClient::TcpClient(QObject *parent, QString NickName)
: QObject(parent), userInterface(this), nickname(NickName)
{
// Create the server socket and listen for connections
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
server->listen(QHostAddress::Any, 24042);
}
void TcpClient::handleNewConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
std::string peers = getPeers();
socket->write(peers.c_str());
connect(socket, SIGNAL(readyRead()), this, SLOT(readFromAll()));
m_sockets.append(socket);
emit newConnection(socket);
}
void TcpClient::sendToAll(QString message)
{
for (QTcpSocket *socket : m_sockets)
{
socket->write(message.toUtf8());
}
}
void TcpClient::readFromAll()
{
for (QTcpSocket *socket : m_sockets)
{
while (socket->bytesAvailable() > 0)
{
QString message = QString::fromUtf8(socket->readAll());
emit newMessageReceived(message);
}
}
}
void TcpClient::firstConnect(std::string firstIp, int firstPort)
{
QTcpSocket *firstSocket = new QTcpSocket(this);
// Get me a damn socket! Grrrr
firstSocket->connectToHost(firstIp.c_str(), firstPort);
firstSocket->write("Hello there!");
firstSocket->waitForBytesWritten(1000);
firstSocket->waitForReadyRead(1000);
if (firstSocket->waitForConnected())
{
connect(firstSocket, SIGNAL(readyRead()), this, SLOT(readFromAll()));
m_sockets.append(firstSocket);
emit newConnection(firstSocket);
qDebug() << "Connected to: " << firstIp.c_str() << ":" << firstSocket;
std::string recv;
recv = firstSocket->readAll();
// Parse the received buffer filled with new IP and Ports.
std::istringstream ss(recv);
std::string token, newIP, newPort;
getline(ss, token); // discard first line
while (getline(ss, token))
{
std::string temp;
std::stringstream tokenStream(token);
getline(tokenStream, newIP, ':');
getline(tokenStream, newPort);
QTcpSocket *socket = new QTcpSocket(this);
socket->connectToHost(newIP.c_str(), atoi(newPort.c_str()));
socket->write("Hello there!");
socket->waitForBytesWritten(1000);
socket->waitForReadyRead(1000);
temp = socket->readAll();
// Are we connected?
if (socket->state() == QAbstractSocket::ConnectedState)
{
std::cout << "Connected to " << newIP << ":" << newPort << " successfully!" << std::endl;
connect(socket, SIGNAL(readyRead()), this, SLOT(readFromAll()));
m_sockets.append(socket);
emit newConnection(socket);
}
else
{
std::cout << "Failed to connect to " << newIP << ":" << newPort << "." << std::endl;
}
}
}
else
{
qDebug() << "Failed to connect to: " << firstIp.c_str() << ":" << firstSocket;
}
}
std::string TcpClient::getPeers(void)
{
QString peerList = "NEWCON";
peerList += '\n';
for (QTcpSocket *socket : m_sockets)
{
if(socket->state()== QTcpSocket::ConnectedState)
{
QString address = QHostAddress(socket->peerAddress().toIPv4Address()).toString();
peerList += address + ":" + "24042" + '\n';
}
}
std::string temp = peerList.toStdString();
return temp;
}