Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Server.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #pragma comment(lib, "ws2_32.lib")
- #include <winsock2.h>
- #include <iostream>
- #include<string>
- #pragma warning(disable: 4996)
- using namespace std;
- int main(int argc, char* argv[]) {
- WSAData wsaData;
- WORD DLLVersion = MAKEWORD(2, 1);
- if (WSAStartup(DLLVersion, &wsaData) != 0) {
- cout << "Error" << endl;
- exit(1);
- }
- SOCKADDR_IN addr;
- int sizeofaddr = sizeof(addr);
- addr.sin_addr.s_addr = inet_addr("127.0.0.1");
- addr.sin_port = htons(1111);
- addr.sin_family = AF_INET;
- SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
- bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
- listen(sListen, SOMAXCONN);
- SOCKET newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr);
- if (newConnection == 0) {
- cout << "Error. Connection not found\n";
- closesocket(newConnection);
- WSACleanup();
- return -1;
- }
- else {
- cout << "Client Connected!\n";
- int msg_size;
- while (true) {
- char msg;
- recv(newConnection, &msg, sizeof(char), NULL);
- if (msg == '\b') {
- cout << '\b' << ' ' << '\b';
- }
- else if (msg == '\n')
- cout << endl;
- else
- cout << msg;
- Sleep(100);
- }
- }
- WSACleanup();
- closesocket(newConnection);
- closesocket(sListen);
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment