Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TCP Echo server example in winsock
- Live Server on port 8888
- */
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <iostream>
- #include<stdio.h>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <cstdio>
- #include <chrono>
- #include <thread>
- #include <functional>
- #include <winsock2.h>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iterator>
- #pragma comment(lib, "ws2_32.lib") //Winsock Library
- using std::string;
- /* --- Implementacja timera --- */
- void timer_start(std::function<void(void)> func, unsigned int interval)
- {
- std::thread([func, interval]() {
- while (true)
- {
- func();
- std::this_thread::sleep_for(std::chrono::milliseconds(interval));
- }
- }).detach();
- }
- /* --- Implementacja timera --- */
- /* --- Odbior danych --- */
- template<typename Out>
- void split(const std::string &s, char delim, Out result) {
- std::stringstream ss(s);
- std::string item;
- while (std::getline(ss, item, delim)) {
- *(result++) = item;
- }
- }
- std::vector<std::string> split(const std::string &s, char delim) {
- std::vector<std::string> elems;
- split(s, delim, std::back_inserter(elems));
- return elems;
- }
- /* --- Odbior danych --- */
- /* --- Definicja Zmiennych do programu --- */
- int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0, x7 = 0, x8 = 0, x9 = 0, x10 = 0, x11 = 0, x12 = 0;
- int T1 = 0, T2 = 0, T3 = 0, T4 = 0;
- char Z1 = 0, Z2 = 0, Z3 = 0, Z4 = 0, Z5 = 0, Z6 = 0;
- char G1 = 0, G2 = 0, G3 = 0, G4 = 0;
- char M1 = 0, M2 = 0, M3 = 0, M4 = 0;
- int stan1 = 1, stan2 = 1, stan3 = 1, stan4 = 1;
- char tim1 = 0, tim2 = 0, tim3 = 0, tim4 = 0;
- //Bedzie 14 ale sa same zawory
- byte x[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
- //Będzie 16 ale bez temperatury
- int y[12] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
- string stringX;
- string stringY;
- string arrayY[12];
- std::vector<int> vectorY;
- /* --- Definicja Zmiennych do programu --- */
- void implementacja()
- {
- //Przypisanie otrzymanych wartosci do poziomow wody
- x1 = y[0];
- x2 = y[1];
- x3 = y[2];
- x4 = y[3];
- x5 = y[4];
- x6 = y[5];
- x7 = y[6];
- x8 = y[7];
- x9 = y[8];
- x10 = y[9];
- x11 = y[10];
- x12 = y[11];
- /*
- //Przypisanie otrzymanych wartosci do poziomu termometrow
- T1 = y[12];
- T2 = y[13];
- T3 = y[14];
- T4 = y[15];
- */
- //Zbiornik 1
- {
- Z1 = 1;
- }
- //Zbiornik 2
- {
- switch (stan2)
- {
- case 1:
- Z2 = 1;
- Z4 = 0;
- if (x4) {
- stan2 = 2;
- }
- break;
- case 2:
- Z2 = 0;
- Z4 = 1;
- if (!x4)
- stan2 = 1;
- break;
- }
- std::cout << "stan : " << stan2 << std::endl;
- }
- //Zbiornik 3
- {
- }
- //Zbiornik 4
- {
- }
- //Przekazanie danych do obiektu
- x[0] = Z1;
- x[1] = Z2;
- x[2] = Z3;
- x[3] = Z4;
- x[4] = Z5;
- x[5] = Z6;
- /*
- x[6] = G1;
- x[7] = G2;
- x[8] = G3;
- x[9] = G4;
- x[10] = M1;
- x[11] = M2;
- x[12] = M3;
- x[13] = M4;
- */
- //Obsluga timerow
- if (tim1) --tim1;
- if (tim2) --tim2;
- if (tim3) --tim3;
- if (tim4) --tim4;
- }
- /* --- Implementacja timera --- */
- void timer_start2(std::function<void(void)> func, unsigned int interval)
- {
- std::thread([func, interval]() {
- while (true)
- {
- func();
- std::this_thread::sleep_for(std::chrono::milliseconds(interval));
- }
- }).detach();
- }
- /* --- Implementacja timera --- */
- void connects()
- {
- WSADATA wsa;
- SOCKET master, new_socket, client_socket[30], s;
- struct sockaddr_in server, address;
- int max_clients = 30, activity, addrlen, i, valread;
- //size of our receive buffer, this is string length.
- int MAXRECV = 1024;
- //set of socket descriptors
- fd_set readfds;
- //1 extra for null character, string termination
- char *buffer;
- buffer = (char*)malloc((MAXRECV + 1) * sizeof(char));
- for (i = 0; i < 30; i++)
- {
- client_socket[i] = 0;
- }
- printf(" \nInitialising Winsock...");
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
- {
- printf(" Failed.Error Code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf(" Initialised.\n");
- //Create a socket
- if ((master = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
- {
- printf(" Could not create socket : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- printf(" Socket created.\n");
- //Prepare the sockaddr_in structure
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons(16666);
- //Bind
- if (bind(master, (struct sockaddr *)& server, sizeof(server)) == SOCKET_ERROR)
- {
- printf(" Bind failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- puts(" Bind done");
- //Listen to incoming connections
- listen(master, 3);
- //Accept and incoming connection
- puts(" Waiting for incoming connections...");
- addrlen = sizeof(struct sockaddr_in);
- while (TRUE)
- {
- //clear the socket fd set
- FD_ZERO(&readfds);
- //add master socket to fd set
- FD_SET(master, &readfds);
- //add child sockets to fd set
- for (i = 0; i < max_clients; i++)
- {
- s = client_socket[i];
- if (s > 0)
- {
- FD_SET(s, &readfds);
- }
- }
- //wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
- activity = select(0, &readfds, NULL, NULL, NULL);
- if (activity == SOCKET_ERROR)
- {
- printf(" select call failed with error code : %d", WSAGetLastError());
- exit(EXIT_FAILURE);
- }
- //If something happened on the master socket , then its an incoming connection
- if (FD_ISSET(master, &readfds))
- {
- if ((new_socket = accept(master, (struct sockaddr *) &address, (int *)& addrlen)) < 0)
- {
- perror(" accept");
- exit(EXIT_FAILURE);
- }
- //inform user of socket number - used in send and receive commands
- printf(" New connection, socket fd is %d, ip is : %s, port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
- //send new connection greeting message
- //stringX = std::to_string(x[0]) + std::to_string(x[1]) + std::to_string(x[2]) + std::to_string(x[3]) + std::to_string(x[4]) + std::to_string(x[5]) + std::to_string(x[6]) + std::to_string(x[7]) + std::to_string(x[8]) + std::to_string(x[9]) + std::to_string(x[10]) + std::to_string(x[11]) + std::to_string(x[12]) + std::to_string(x[13]) + std::to_string(x[14]);
- /*
- if (send(new_socket, stringX.c_str(), strlen(stringX.c_str()), 0) != strlen(stringX.c_str()))
- {
- perror(" send failed");
- }
- */
- puts(" Welcome message sent successfully");
- //add new socket to array of sockets
- for (i = 0; i < max_clients; i++)
- {
- if (client_socket[i] == 0)
- {
- client_socket[i] = new_socket;
- printf(" Adding to list of sockets at index %d \n", i);
- break;
- }
- }
- }
- //else its some IO operation on some other socket :)
- for (i = 0; i < max_clients; i++)
- {
- s = client_socket[i];
- //if client presend in read sockets
- if (FD_ISSET(s, &readfds))
- {
- //get details of the client
- getpeername(s, (struct sockaddr*)& address, (int*)& addrlen);
- //Check if it was for closing , and also read the incoming message
- //recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one).
- valread = recv(s, buffer, MAXRECV, 0);
- if (valread == SOCKET_ERROR)
- {
- int error_code = WSAGetLastError();
- if (error_code == WSAECONNRESET)
- {
- //Somebody disconnected , get his details and print
- printf(" Host disconnected unexpectedly, ip %s, port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
- //Close the socket and mark as 0 in list for reuse
- closesocket(s);
- client_socket[i] = 0;
- }
- else
- {
- printf(" recv failed with error code : %d", error_code);
- }
- }
- if (valread == 0)
- {
- closesocket(s);
- client_socket[i] = 0;
- }
- //Echo back the message that came in
- else
- {
- vectorY.clear();
- //add null character, if you want to use with printf/puts or other string handling functions
- buffer[valread] = '\0';
- stringX = std::to_string(x[0]) + ":" + std::to_string(x[1]) + ":" + std::to_string(x[2]) + ":" + std::to_string(x[3]) + ":" + std::to_string(x[4]) + ":" + std::to_string(x[5]) +
- ":" + std::to_string(x[6]) + ":" + std::to_string(x[7]) + ":" + std::to_string(x[8]) + ":" + std::to_string(x[9]) + ":" + std::to_string(x[10]) + ":" + std::to_string(x[11]) +
- ":" + std::to_string(x[12]) + ":" + std::to_string(x[13]) + ":" + std::to_string(x[14]);
- send(s, stringX.c_str(), strlen(stringX.c_str()), 0);
- std::cout << "Wyslano : " << stringX.c_str() << std::endl;
- stringY = buffer;
- std::cout << stringY << std::endl;
- std::stringstream ss(stringY);
- int i;
- while (ss >> i)
- {
- vectorY.push_back(i);
- if (ss.peek() == ':')
- ss.ignore();
- }
- for (i = 0; i < 12; i++)
- y[i] = vectorY.at(i);
- }
- }
- }
- }
- closesocket(s);
- WSACleanup();
- }
- int main(int argc, char *argv[])
- {
- // Uruchomienie symulacji co 100 ms
- timer_start(implementacja, 100);
- connects();
- //timer_start2(connects, 100);
- while(true){}
- }
Add Comment
Please, Sign In to add comment