You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My startup sequence is to start the server first, and then the client. The client transmits the binary content of an image file to the server, and the server writes it into the file after receiving it. However, I found that the server did not respond when I started the client for the first time. , it does not even show that there is a client connected to the server. Only when I start the client for the second time can the server receive the file correctly. I want to know why this is. The client and server codes are as follows:
client:
#include <iostream>
#include <fstream>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
// Define a WebSocket++ client type based on the ASIO network transport
using WebSocketClient = websocketpp::client<websocketpp::config::asio_client>;
int main() {
// Create a WebSocket++ client
WebSocketClient client;
try {
// Set up the client
client.init_asio();
// Specify the WebSocket server URI you want to connect to
std::string serverUri = "ws://localhost:9002"; // Replace with your WebSocket server's URL
// Set up the client connection
websocketpp::lib::error_code ec;
WebSocketClient::connection_ptr con = client.get_connection(serverUri, ec);
if (ec) {
std::cerr << "Error connecting to server: " << ec.message() << std::endl;
return 1;
}
// Connect to the WebSocket server
client.connect(con);
// Read an image file (e.g., image.jpg) for transmission
std::ifstream inputFile("file1.jpg", std::ios::binary);
std::string imageData((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
// Register a callback for successful connection
con->set_open_handler([&client, &con, &imageData](websocketpp::connection_hdl) {
std::cout << "Connected to server" << std::endl;
// Send the image data to the server as a binary message
client.send(con, imageData, websocketpp::frame::opcode::binary);
});
// Run the WebSocket++ event loop
client.run();
} catch (websocketpp::exception const &e) {
std::cerr << "WebSocket++ exception: " << e.what() << std::endl;
return 1;
}
return 0;
}
server:
#include <iostream>
#include <fstream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
// Define a WebSocket++ server type based on the ASIO network transport
using WebSocketServer = websocketpp::server<websocketpp::config::asio>;
int main() {
// Create a WebSocket++ server
WebSocketServer server;
try {
// Initialize the server
server.init_asio();
// Bind the server to a specific port (e.g., 9002)
server.set_reuse_addr(true);
server.listen(9002);
// Start the server accept loop
server.start_accept();
server.set_open_handler([&server](websocketpp::connection_hdl hdl) {
std::cout << "New connection accepted." << std::endl;
});
// Set up the message handler
server.set_message_handler([&server](websocketpp::connection_hdl hdl, WebSocketServer::message_ptr msg) {
try {
// Extract the received binary data (image) from the message
const std::string& receivedImageData = msg->get_payload();
printf("begin to receive\n");
// Save the received image to a file (e.g., received_image.jpg)
std::ofstream outputFile("received_image.jpg", std::ios::binary);
outputFile.write(receivedImageData.c_str(), receivedImageData.size());
outputFile.close();
std::cout << "Received and saved an image." << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error handling message: " << e.what() << std::endl;
}
});
// Start the server event loop
server.run();
} catch (websocketpp::exception const &e) {
std::cerr << "WebSocket++ exception: " << e.what() << std::endl;
return 1;
}
return 0;
}
The text was updated successfully, but these errors were encountered:
My startup sequence is to start the server first, and then the client. The client transmits the binary content of an image file to the server, and the server writes it into the file after receiving it. However, I found that the server did not respond when I started the client for the first time. , it does not even show that there is a client connected to the server. Only when I start the client for the second time can the server receive the file correctly. I want to know why this is. The client and server codes are as follows:
client:
server:
The text was updated successfully, but these errors were encountered: