Advertisement
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";
- }
- else {
- cout << "Client Connected!\n";
- int msg_size;
- while (true) {
- recv(newConnection, (char*)&msg_size, sizeof(int), NULL);
- char* msg = new char[msg_size + 1];
- msg[msg_size] = '\0';
- recv(newConnection, msg, msg_size, NULL);
- cout << msg << endl;
- delete[] msg;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement