Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment (lib, "WS2_32.lib")
- #include <WinSock2.h>
- #include <windows.h>
- #include <iostream>
- #include <vector>
- std::vector<SOCKET> Connections;
- int main() {
- WSADATA wsaData;
- WSAStartup(MAKEWORD(2, 1), &wsaData);
- SOCKADDR_IN addr;
- int sizeofaddr = sizeof(addr);
- addr.sin_addr.s_addr = INADDR_ANY;
- addr.sin_port = htons(80);
- addr.sin_family = AF_INET;
- SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, NULL);
- bind(listenSocket, (SOCKADDR*)(&addr), sizeofaddr);
- listen(listenSocket, SOMAXCONN);
- u_long iMode = 1;
- ioctlsocket(listenSocket, FIONBIO, &iMode);
- FD_SET readfds;
- std::cout << "Running on 127.0.0.1:80..." << std::endl;
- while (1) {
- FD_ZERO(&readfds);
- FD_SET(listenSocket, &readfds);
- for (int i = 0; i < Connections.size(); i++) {
- FD_SET(Connections[i], &readfds);
- }
- select(0, &readfds, 0, 0, 0);
- if (FD_ISSET(listenSocket, &readfds)) {
- SOCKET newConnection = accept(listenSocket, NULL, NULL);
- if (newConnection != INVALID_SOCKET) {
- std::cout << "new connection! N: " << (Connections.size() + 1) << std::endl;
- Connections.push_back(newConnection);
- }
- }
- for (int i = 0; i < Connections.size(); i++) {
- if (FD_ISSET(Connections[i], &readfds)) {
- char buffer[1024];
- int recvret = recv(Connections[i], buffer, 1024, 0);
- if (recvret == SOCKET_ERROR || recvret == 0) {
- if (WSAGetLastError() == WSAEWOULDBLOCK || recvret == 0) {
- std::cout << "Lost connection :(" << std::endl;
- closesocket(Connections[i]);
- continue;
- }
- }
- if (recvret > 0) {
- std::cout << "Got data from connection" << std::endl;
- char buf[] = "";
- send(Connections[i], buf, 4, 0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement