#include#include #include #include #include #include using namespace std; int main() { int sock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons(8080); if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1) { cout << "Error: Connection failed.\n"; return 1; } char buf[1024]; memset(buf, 0, sizeof(buf)); read(sock, buf, sizeof(buf)); cout << "Received: " << buf << endl; close(sock); return 0; }
#includeIn this example, SocketInputStream Read is used to read a binary message sent over a socket connection. The code reads the message into a buffer, and then converts each byte of the message to its corresponding hex value using stringstream. Finally, the hex values are printed to the console. The package library for SocketInputStream Read is likely to be a networking library such as Boost.Asio or SFML.Network.#include #include #include #include #include using namespace std; int main() { int sock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons(8080); if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1) { cout << "Error: Connection failed.\n"; return 1; } char buf[1024]; memset(buf, 0, sizeof(buf)); int length = read(sock, buf, sizeof(buf)); stringstream ss; for (int i = 0; i < length; i++) { ss << hex << (int)buf[i]; } cout << "Received: " << ss.str() << endl; close(sock); return 0; }