P
P
Pinkman2021-11-21 18:28:44
C++ / C#
Pinkman, 2021-11-21 18:28:44

How to redirect requests to another address?

I have a single-threaded server, works through select. And everything seems to be fine, I can read requests from several clients, and send them answers. But I don't understand how I can send client requests further, to another server, and then send the response from the database back to the clients.

int dbSock = socket(PF_INET, SOCK_STREAM, 0);
            if (dbSock < 0) {
              std::cerr << "Error: Cant open db socket\n";
              connectClose = 1;
            }
            struct sockaddr_in dbSi;
            memset(&dbSi, 0, sizeof(dbSi));
            dbSi.sin_family = PF_INET;
            dbSi.sin_addr.s_addr = inet_addr("127.0.0.1");
            dbSi.sin_port = htons(3306);
// Тут пробовал и accept и без bind'a, все равно работать не хочет
            if (bind(dbSock, (struct sockaddr*)&dbSi, sizeof(dbSock)) < 0)
              std::cerr << "Bind error\n";
            buf[rb] = '\0';
            rb = send(dbSock, buf, rb, 0);
            rb = recv(dbSock, buf, sizeof(buf), 0);
            if (rb == -1)
              std::cerr << "-1\n";
            std::cerr << "From db: " << buf;

Please tell me where to dig...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question