Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <iostream>
- #include <string>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #pragma comment(lib, "ws2_32.lib")
- #pragma warning( disable : 4996)
- #define TRUE 1
- #define FALSE 0
- #define PORT 5555
- using namespace std;
- struct t_PLAYERDATA
- {
- int playerID;
- double playerX;
- double playerY;
- } PLAYERDATA;
- int main(int argc, char *argv[])
- {
- int opt = TRUE;
- int master_socket, addrlen, new_socket, client_socket[30], max_clients = 30, activity, i, valread, sd, sdBis;
- int max_sd;
- struct sockaddr_in address;
- WSADATA WSAData;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- char buffer[1024];
- fd_set readfds;
- const char *message = "Welcome to FoD\n";
- for (i = 0; i < max_clients; i++)
- {
- client_socket[i] = 0;
- }
- if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0)
- {
- perror("socket failed");
- exit(EXIT_FAILURE);
- }
- /*int Socketerror = WSAGetLastError();
- cout << Socketerror << endl;*/
- if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0)
- {
- perror("setsockopt");
- exit(EXIT_FAILURE);
- }
- address.sin_family = AF_INET;
- address.sin_addr.s_addr = INADDR_ANY;
- address.sin_port = htons(PORT);
- if (bind(master_socket, (struct sockaddr *)&address, sizeof(address)) < 0)
- {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- printf("Listener on port %d \n", PORT);
- if (listen(master_socket, 3) < 0)
- {
- perror("listen");
- exit(EXIT_FAILURE);
- }
- addrlen = sizeof(address);
- puts("Waiting for connections ...");
- while (TRUE)
- {
- FD_ZERO(&readfds);
- FD_SET(master_socket, &readfds);
- max_sd = master_socket;
- for (i = 0; i < max_clients; i++)
- {
- sd = client_socket[i];
- if (sd > 0)
- {
- FD_SET(sd, &readfds);
- }
- if (sd > max_sd)
- {
- max_sd = sd;
- }
- }
- activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
- if ((activity < 0) && (errno != EINTR))
- {
- printf("select error");
- }
- if (FD_ISSET(master_socket, &readfds))
- {
- if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
- printf("New connection , socket fd is %d , ip is : %s , port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
- if (send(new_socket, message, strlen(message), 0) != strlen(message))
- {
- perror("send");
- }
- puts("Welcome message sent successfully");
- for (i = 0; i < max_clients; i++)
- {
- if (client_socket[i] == 0)
- {
- client_socket[i] = new_socket;
- printf("Adding to list of sockets as %d\n", i);
- break;
- }
- }
- }
- for (i = 0; i < max_clients; i++)
- {
- memset(buffer, 0, sizeof buffer);
- sd = client_socket[i];
- if (FD_ISSET(sd, &readfds))
- {
- if ((valread = recv(sd, reinterpret_cast<char*>(&PLAYERDATA), sizeof(PLAYERDATA), 0)) < 0)
- {
- getpeername(sd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
- printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
- closesocket(sd);
- client_socket[i] = 0;
- }
- else
- {
- cout << "playerID : " << PLAYERDATA.playerID << endl;
- cout << "playerX : " << PLAYERDATA.playerX << endl;
- cout << "playerY : " << PLAYERDATA.playerY << endl;
- cout << endl;
- for (i = 0; i < max_clients; i++)
- {
- send(client_socket[i], buffer, strlen(buffer), 0);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement