Javier Zayas
Javier Zayas

Reputation: 56

devKitPro network.h: Cannot create network socket, returns "Illegal byte sequence"

I am trying to create a HTML web server for the Wii. It'll only serve static content, nothing dynamic.

However, I'm running into issues when it comes to actually creating the socket.

This code had been developed within Arch Linux, and had successfully compiled under such (simply g++ main.cpp). Visiting http://localhost:8080/ returned Hello, world!, and 404 error handling worked just fine.

devKitPro's network API for the Wii seemed pretty straightforward, with all of the POSIX sys/socket.h functions just having a net_ prefix to them. The code compiled completely fine, albeit adjusted to compile. However when running the application, the network is initialized but when making the socket, I receive socket error: Illegal byte sequence.

This is how I initialize the socket, set, bind, and listen to it.

class WebServer {
public:
    WebServer(unsigned short port) : port_(port) {}

    void Run() {
        int server, ssocket, valread;
        struct sockaddr_in address;
        int addrlen = sizeof(address);
        char buffer[1024] = {0};

        if ((server = net_socket(AF_INET, SOCK_STREAM, 0)) == 0) {
            perror("socket failed");
            exit(EXIT_FAILURE);
        }

        int opt = 1;
        if (net_setsockopt(server, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
            perror("setsockopt failed");
            exit(EXIT_FAILURE);
        }

        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons(port_);

        if (net_bind(server, (struct sockaddr *)&address, sizeof(address)) < 0) {
            perror("bind failed");
            exit(EXIT_FAILURE);
        }

        if (net_listen(server, 3) < 0) {
            perror("listen failed");
            exit(EXIT_FAILURE);
        } else {
            std::cout << "Server is now running at port " << port_ << std::endl;
        }

        while (true) {
            if ((ssocket = net_accept(server, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
                perror("accept failed");
                exit(EXIT_FAILURE);
            }

            valread = read(ssocket, buffer, 1024);
            std::string request(buffer, valread);
            size_t start = request.find(' ') + 1;
            size_t end = request.find(' ', start);
            std::string path = request.substr(start, end - start);

            if (path.back() == '/') {
                path += "index.html";
            }

            if (!fatInitDefault()) {
                std::cout << "\033[31mError: Could not (re)initialize the FAT subsystem. Exiting to system menu." << std::endl << "\033[0m";
                SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
            }

            std::string webp = "./html" + path;

            std::ifstream file(webp, std::ios::binary);
            if (file) {
                std::stringstream ss;
                ss << file.rdbuf();
                std::string contents = ss.str();
                std::string response = "HTTP/1.1 200 OK\r\nContent-Length: " + std::to_string(contents.size()) + "\r\n\r\n" + contents;
                write(ssocket, response.c_str(), response.size());
            } else {
                std::string error_page = "<html>\n\t<head>\n\t\t<title>404 Not Found</title>\n\t</head>\n\t<body>\n\t\t<h1>404 Not Found</h1>\n\t\t<p>The requested URL <i>" + path + "</i> was not found on this server.</p>\n\t</body>\n</html>";
                std::string response = "HTTP/1.1 404 Not Found\r\nContent-Length: " + std::to_string(error_page.size()) + "\r\n\r\n" + error_page;
                write(ssocket, response.c_str(), response.size());
            }

            close(ssocket);
        }
    }


private:
    unsigned short port_;
};

I'm thinking it could be the endianness between the two systems, which is tried to htons the port to see if it'd do anything different. I've also tried to run this within emulation and real hardware, both to no avail. Thank you for your help.

Upvotes: 1

Views: 105

Answers (0)

Related Questions