Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //server
- #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;
- 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, buffer, 1024, 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
- {
- buffer[valread] = '\0';
- cout << buffer << endl;
- for (i = 0; i < max_clients; i++)
- {
- send(client_socket[i], buffer, strlen(buffer), 0);
- }
- }
- }
- }
- }
- system("pause");
- }
- //client
- #include <string>
- #include <iostream>
- #include <winsock2.h>
- #include <Ws2tcpip.h>
- #include <thread>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- void *sendMessage(void *sock_desc)
- {
- while (TRUE)
- {
- string talk = "";
- getline(cin, talk);
- if (send(*((int *)sock_desc), talk.c_str(), sizeof(talk) + 1, 0) < 0)
- {
- cout << "Send fail" << endl;
- }
- }
- }
- void *receiveMessage(void *sock_desc)
- {
- char responce[2000];
- while (TRUE)
- {
- memset(responce, 0, sizeof responce);
- if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
- {
- puts("recv failed");
- }
- cout << responce << endl;
- }
- }
- int main()
- {
- WSADATA WSAData;
- SOCKET server;
- int sock;
- SOCKADDR_IN addr;
- WSAStartup(MAKEWORD(2, 0), &WSAData);
- server = socket(AF_INET, SOCK_STREAM, 0);
- InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5555);
- sock = server;
- int * newSocket = static_cast<int*>(malloc(1));
- *newSocket = sock;
- if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
- {
- cout << "Connected to server!" << endl;
- /*while (talk != "quit")
- {
- memset(responce, 0, sizeof responce);
- if (recv(server, responce, sizeof(responce), 0) > 0)
- {
- cout << responce << endl;
- }
- else
- {
- getline(cin, talk);
- if (talk.c_str() != NULL)
- {
- if (send(server, talk.c_str(), sizeof(talk), 0) <= 0)
- {
- talk = "quit";
- }
- }
- }
- }*/
- //thread(&sendThread, NULL, sendMessage((void *)server));
- //sendThread = thread(sendMessage((void *)server), this);
- thread sendThread(sendMessage, (void *)newSocket);
- thread receiveThread(receiveMessage, (void *)newSocket);
- sendThread.join();
- receiveThread.join();
- closesocket(server);
- WSACleanup();
- cout << "Server has closed." << endl << endl;
- }
- else
- {
- cout << "Couldn't connect to server." << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement