tools/hexnet: Implement bi-directional communication
parent
a1ba08b7db
commit
b5c18cd0de
|
@ -148,6 +148,26 @@ void readFromTCP() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeToTCP() {
|
||||||
|
while (!exitProgram) {
|
||||||
|
if (outgoing) {
|
||||||
|
if (tcpConnector.write(readByteFromStdin()) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
tcpConnector.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << tcpConnector.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (tcpSocket.write(readByteFromStdin()) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
tcpSocket.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << tcpSocket.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void readFromTCP6() {
|
void readFromTCP6() {
|
||||||
ssize_t byteCount;
|
ssize_t byteCount;
|
||||||
while (!exitProgram && (outgoing? (byteCount = tcp6Connector.read(networkBuffer, bufferSize)) > 0 : (byteCount = tcp6Socket.read(networkBuffer, bufferSize)) > 0)) {
|
while (!exitProgram && (outgoing? (byteCount = tcp6Connector.read(networkBuffer, bufferSize)) > 0 : (byteCount = tcp6Socket.read(networkBuffer, bufferSize)) > 0)) {
|
||||||
|
@ -158,6 +178,26 @@ void readFromTCP6() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeToTCP6() {
|
||||||
|
while (!exitProgram) {
|
||||||
|
if (outgoing) {
|
||||||
|
if (tcp6Connector.write(readByteFromStdin()) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
tcp6Connector.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << tcp6Connector.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (tcp6Socket.write(readByteFromStdin()) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
tcp6Socket.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << tcp6Socket.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void readFromUDP() {
|
void readFromUDP() {
|
||||||
ssize_t byteCount;
|
ssize_t byteCount;
|
||||||
sockpp::udp_socket::addr_t peer;
|
sockpp::udp_socket::addr_t peer;
|
||||||
|
@ -170,6 +210,17 @@ void readFromUDP() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeToUDP(sockpp::udp_socket::addr_t peer) {
|
||||||
|
while (!exitProgram) {
|
||||||
|
if (udpSocket.send_to(readByteFromStdin(), peer) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
udpSocket.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << udpSocket.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void readFromUDP6() {
|
void readFromUDP6() {
|
||||||
ssize_t byteCount;
|
ssize_t byteCount;
|
||||||
sockpp::udp6_socket::addr_t peer;
|
sockpp::udp6_socket::addr_t peer;
|
||||||
|
@ -182,6 +233,17 @@ void readFromUDP6() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeToUDP6(sockpp::udp6_socket::addr_t peer) {
|
||||||
|
while (!exitProgram) {
|
||||||
|
if (udp6Socket.send_to(readByteFromStdin(), peer) == -1 && !exitProgram) {
|
||||||
|
exitProgram = true;
|
||||||
|
udp6Socket.shutdown(SHUT_RD);
|
||||||
|
std::cerr << "Error while sending data: " << udp6Socket.last_error_str() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
std::signal(SIGINT, signalHandler);
|
std::signal(SIGINT, signalHandler);
|
||||||
std::signal(SIGTERM, signalHandler);
|
std::signal(SIGTERM, signalHandler);
|
||||||
|
@ -263,10 +325,10 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromTCP = std::thread(readFromTCP);
|
std::thread threadReadFromTCP = std::thread(readFromTCP);
|
||||||
//std::thread threadWriteToTCP = std::thread(writeToTCP);
|
std::thread threadWriteToTCP = std::thread(writeToTCP);
|
||||||
|
|
||||||
threadReadFromTCP.join();
|
threadReadFromTCP.join();
|
||||||
//threadWriteToTCP.join();
|
threadWriteToTCP.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -280,10 +342,10 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
|
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
|
||||||
//std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
|
std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
|
||||||
|
|
||||||
threadReadFromTCP6.join();
|
threadReadFromTCP6.join();
|
||||||
//threadWriteToTCP6.join();
|
threadWriteToTCP6.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -296,16 +358,17 @@ int main(int argc, char* argv[]) {
|
||||||
std::cerr << "Error creating UDP socket: " << udpSocket.last_error_str() << std::endl;
|
std::cerr << "Error creating UDP socket: " << udpSocket.last_error_str() << std::endl;
|
||||||
}
|
}
|
||||||
// Btw: Did you know that UDP has no concept of a connection?
|
// Btw: Did you know that UDP has no concept of a connection?
|
||||||
if (!udpSocket.connect(sockpp::inet_address(host, port))) {
|
sockpp::udp_socket::addr_t peer = sockpp::inet_address(host, port);
|
||||||
|
if (!udpSocket.connect(peer)) {
|
||||||
std::cerr << "Error associating socket with " << host << " port " << port << std::endl;
|
std::cerr << "Error associating socket with " << host << " port " << port << std::endl;
|
||||||
std::cerr << udpSocket.last_error_str() << std::endl;
|
std::cerr << udpSocket.last_error_str() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromUDP = std::thread(readFromUDP);
|
std::thread threadReadFromUDP = std::thread(readFromUDP);
|
||||||
//std::thread threadWriteToUDP = std::thread(writeToUDP);
|
std::thread threadWriteToUDP = std::thread(writeToUDP, peer);
|
||||||
|
|
||||||
threadReadFromUDP.join();
|
threadReadFromUDP.join();
|
||||||
//threadWriteToUDP.join();
|
threadWriteToUDP.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -315,16 +378,17 @@ int main(int argc, char* argv[]) {
|
||||||
std::cerr << "Error creating UDP socket: " << udp6Socket.last_error_str() << std::endl;
|
std::cerr << "Error creating UDP socket: " << udp6Socket.last_error_str() << std::endl;
|
||||||
}
|
}
|
||||||
// Btw: Did you know that UDP has no concept of a connection?
|
// Btw: Did you know that UDP has no concept of a connection?
|
||||||
if (!udp6Socket.connect(sockpp::inet6_address(host, port))) {
|
sockpp::udp6_socket::addr_t peer = sockpp::inet6_address(host, port);
|
||||||
|
if (!udp6Socket.connect(peer)) {
|
||||||
std::cerr << "Error associating socket with " << host << " port " << port << std::endl;
|
std::cerr << "Error associating socket with " << host << " port " << port << std::endl;
|
||||||
std::cerr << udp6Socket.last_error_str() << std::endl;
|
std::cerr << udp6Socket.last_error_str() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromUDP6 = std::thread(readFromUDP6);
|
std::thread threadReadFromUDP6 = std::thread(readFromUDP6);
|
||||||
//std::thread threadWriteToUDP6 = std::thread(writeToUDP6);
|
std::thread threadWriteToUDP6 = std::thread(writeToUDP6, peer);
|
||||||
|
|
||||||
threadReadFromUDP6.join();
|
threadReadFromUDP6.join();
|
||||||
//threadWriteToUDP6.join();
|
threadWriteToUDP6.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -350,10 +414,10 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromTCP = std::thread(readFromTCP);
|
std::thread threadReadFromTCP = std::thread(readFromTCP);
|
||||||
//std::thread threadWriteToTCP = std::thread(writeToTCP);
|
std::thread threadWriteToTCP = std::thread(writeToTCP);
|
||||||
|
|
||||||
threadReadFromTCP.join();
|
threadReadFromTCP.join();
|
||||||
//threadWriteToTCP.join();
|
threadWriteToTCP.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -374,10 +438,10 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
|
std::thread threadReadFromTCP6 = std::thread(readFromTCP6);
|
||||||
//std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
|
std::thread threadWriteToTCP6 = std::thread(writeToTCP6);
|
||||||
|
|
||||||
threadReadFromTCP6.join();
|
threadReadFromTCP6.join();
|
||||||
//threadWriteToTCP6.join();
|
threadWriteToTCP6.join();
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -398,6 +462,7 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromUDP = std::thread(readFromUDP);
|
std::thread threadReadFromUDP = std::thread(readFromUDP);
|
||||||
|
// Can't send bc we have no idea where to send to.
|
||||||
//std::thread threadWriteToUDP = std::thread(writeToUDP);
|
//std::thread threadWriteToUDP = std::thread(writeToUDP);
|
||||||
|
|
||||||
threadReadFromUDP.join();
|
threadReadFromUDP.join();
|
||||||
|
@ -415,6 +480,7 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread threadReadFromUDP6 = std::thread(readFromUDP6);
|
std::thread threadReadFromUDP6 = std::thread(readFromUDP6);
|
||||||
|
// Can't send bc we have no idea where to send to.
|
||||||
//std::thread threadWriteToUDP6 = std::thread(writeToUDP6);
|
//std::thread threadWriteToUDP6 = std::thread(writeToUDP6);
|
||||||
|
|
||||||
threadReadFromUDP6.join();
|
threadReadFromUDP6.join();
|
||||||
|
|
Loading…
Reference in New Issue