Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[18.97.9.175] |
|
Сообщ.
#1
,
|
|
|
Всем привет, решил записать небольшую серию уроков по WinSock в С++.
Урок #1. Написание простейшего клиент-серверного приложения (видео) Исходный код: Скрытый текст // // ClientC.cpp - код клиентской части // #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #pragma warning(disable: 4996) int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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 Connection = socket(AF_INET, SOCK_STREAM, NULL); if(connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { std::cout << "Error: failed connect to server.\n"; return 1; } std::cout << "Connected!\n"; char msg[256]; recv(Connection, msg, sizeof(msg), NULL); std::cout << msg << std::endl; system("pause"); return 0; } // // ServerS.cpp - код серверной части // #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #pragma warning(disable: 4996) int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr); if(newConnection == 0) { std::cout << "Error #2\n"; } else { std::cout << "Client Connected!\n"; char msg[256] = "Hello. It`s my first network program!"; send(newConnection, msg, sizeof(msg), NULL); } system("pause"); return 0; } |
Сообщ.
#2
,
|
|
|
Вышел 2 урок: Видео
Исходный код: #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #pragma warning(disable: 4996) SOCKET Connections[100]; int Counter = 0; void ClientHandler(int index) { char msg[256]; while(true) { recv(Connections[index], msg, sizeof(msg), NULL); for(int i = 0; i < Counter; i++) { if(i == index) { continue; } send(Connections[i], msg, sizeof(msg), NULL); } } } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; for(int i = 0; i < 100; i++) { newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr); if(newConnection == 0) { std::cout << "Error #2\n"; } else { std::cout << "Client Connected!\n"; char msg[256] = "Hello. It`s my first network program!"; send(newConnection, msg, sizeof(msg), NULL); Connections[i] = newConnection; Counter++; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, (LPVOID)(i), NULL, NULL); } } system("pause"); return 0; } #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #pragma warning(disable: 4996) SOCKET Connection; void ClientHandler() { char msg[256]; while(true) { recv(Connection, msg, sizeof(msg), NULL); std::cout << msg << std::endl; } } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; Connection = socket(AF_INET, SOCK_STREAM, NULL); if(connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { std::cout << "Error: failed connect to server.\n"; return 1; } std::cout << "Connected!\n"; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, NULL, NULL, NULL); char msg1[256]; while(true) { std::cin.getline(msg1, sizeof(msg1)); send(Connection, msg1, sizeof(msg1), NULL); Sleep(10); } system("pause"); return 0; } |
Сообщ.
#3
,
|
|
|
Вышел 3 урок: Видео
Исходный код: #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #include <string> #pragma warning(disable: 4996) SOCKET Connection; void ClientHandler() { int msg_size; while(true) { recv(Connection, (char*)&msg_size, sizeof(int), NULL); char *msg = new char[msg_size + 1]; msg[msg_size] = '\0'; recv(Connection, msg, msg_size, NULL); std::cout << msg << std::endl; delete[] msg; } } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; Connection = socket(AF_INET, SOCK_STREAM, NULL); if(connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { std::cout << "Error: failed connect to server.\n"; return 1; } std::cout << "Connected!\n"; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, NULL, NULL, NULL); std::string msg1; while(true) { std::getline(std::cin, msg1); int msg_size = msg1.size(); send(Connection, (char*)&msg_size, sizeof(int), NULL); send(Connection, msg1.c_str(), msg_size, NULL); Sleep(10); } system("pause"); return 0; } #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #pragma warning(disable: 4996) SOCKET Connections[100]; int Counter = 0; void ClientHandler(int index) { int msg_size; while(true) { recv(Connections[index], (char*)&msg_size, sizeof(int), NULL); char *msg = new char[msg_size + 1]; msg[msg_size] = '\0'; recv(Connections[index], msg, msg_size, NULL); for(int i = 0; i < Counter; i++) { if(i == index) { continue; } send(Connections[i], (char*)&msg_size, sizeof(int), NULL); send(Connections[i], msg, msg_size, NULL); } delete[] msg; } } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; for(int i = 0; i < 100; i++) { newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr); if(newConnection == 0) { std::cout << "Error #2\n"; } else { std::cout << "Client Connected!\n"; std::string msg = "Hello. It`s my first network program!"; int msg_size = msg.size(); send(newConnection, (char*)&msg_size, sizeof(int), NULL); send(newConnection, msg.c_str(), msg_size, NULL); Connections[i] = newConnection; Counter++; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, (LPVOID)(i), NULL, NULL); } } system("pause"); return 0; } |
Сообщ.
#4
,
|
|
|
Вышел 4 урок: Видео
Исходный код: #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #include <iostream> #include <string> #define _WINSOCK_DEPRECATED_NO_WARNINGS #pragma warning(disable: 4996) SOCKET Connection; enum Packet { P_ChatMessage, P_Test }; bool ProcessPacket(Packet packettype) { switch(packettype) { case P_ChatMessage: { int msg_size; recv(Connection, (char*)&msg_size, sizeof(int), NULL); char *msg = new char[msg_size + 1]; msg[msg_size] = '\0'; recv(Connection, msg, msg_size, NULL); std::cout << msg << std::endl; delete[] msg; break; } case P_Test: std::cout << "Test packet.\n"; break; default: std::cout << "Unrecognized packet: " << packettype << std::endl; break; } return true; } void ClientHandler() { Packet packettype; while(true) { recv(Connection, (char*)&packettype, sizeof(Packet), NULL); if(!ProcessPacket(packettype)) { break; } } closesocket(Connection); } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; Connection = socket(AF_INET, SOCK_STREAM, NULL); if(connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) { std::cout << "Error: failed connect to server.\n"; return 1; } std::cout << "Connected!\n"; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, NULL, NULL, NULL); std::string msg1; while(true) { std::getline(std::cin, msg1); int msg_size = msg1.size(); Packet packettype = P_ChatMessage; send(Connection, (char*)&packettype, sizeof(Packet), NULL); send(Connection, (char*)&msg_size, sizeof(int), NULL); send(Connection, msg1.c_str(), msg_size, NULL); Sleep(10); } system("pause"); return 0; } #include "stdafx.h" #pragma comment(lib, "ws2_32.lib") #include <winsock2.h> #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <iostream> #pragma warning(disable: 4996) SOCKET Connections[100]; int Counter = 0; enum Packet { P_ChatMessage, P_Test }; bool ProcessPacket(int index, Packet packettype) { switch(packettype) { case P_ChatMessage: { int msg_size; recv(Connections[index], (char*)&msg_size, sizeof(int), NULL); char *msg = new char[msg_size + 1]; msg[msg_size] = '\0'; recv(Connections[index], msg, msg_size, NULL); for(int i = 0; i < Counter; i++) { if(i == index) { continue; } Packet msgtype = P_ChatMessage; send(Connections[i], (char*)&msgtype, sizeof(Packet), NULL); send(Connections[i], (char*)&msg_size, sizeof(int), NULL); send(Connections[i], msg, msg_size, NULL); } delete[] msg; break; } default: std::cout << "Unrecognized packet: " << packettype << std::endl; break; } return true; } void ClientHandler(int index) { Packet packettype; while(true) { recv(Connections[index], (char*)&packettype, sizeof(Packet), NULL); if(!ProcessPacket(index, packettype)) { break; } } closesocket(Connections[index]); } int main(int argc, char* argv[]) { //WSAStartup WSAData wsaData; WORD DLLVersion = MAKEWORD(2, 1); if(WSAStartup(DLLVersion, &wsaData) != 0) { std::cout << "Error" << std::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; for(int i = 0; i < 100; i++) { newConnection = accept(sListen, (SOCKADDR*)&addr, &sizeofaddr); if(newConnection == 0) { std::cout << "Error #2\n"; } else { std::cout << "Client Connected!\n"; std::string msg = "Hello. It`s my first network program!"; int msg_size = msg.size(); Packet msgtype = P_ChatMessage; send(newConnection, (char*)&msgtype, sizeof(Packet), NULL); send(newConnection, (char*)&msg_size, sizeof(int), NULL); send(newConnection, msg.c_str(), msg_size, NULL); Connections[i] = newConnection; Counter++; CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandler, (LPVOID)(i), NULL, NULL); Packet testpacket = P_Test; send(newConnection, (char*)&testpacket, sizeof(Packet), NULL); } } system("pause"); return 0; } |