Boost.Asioã§ç°¡åãªãã£ãã
ç°¡åãªãã£ããããã°ã©ã ãæ¸ãã¦ã¿ã¾ãããBoost.Asioã¨Winsock2ã®2éãæ¸ãã¦ã¿ã¾ãããã¨ã«ããç°¡åã«æ¸ããã¨ããã®ã§åæéä¿¡ãã¦ãã¾ãããªã®ã§ä¸æ¹çã«ã¡ãã»ã¼ã¸ãéããã¨ã¯ã§ãããå¿
ããµã¼ãå´ã¨ã¯ã©ã¤ã¢ã³ãå´ã交äºã«ã¡ãã»ã¼ã¸ãéä¿¡ããªããã°ãªãã¾ããã
ã©ã¡ããã"end"ã¨ããã¡ãã»ã¼ã¸ãéã£ããæ¥ç¶ãåããçµäºãã¾ãã
WinSockã§æ¸ãããã£ããããã°ã©ã
#include <winsock2.h> #include <iostream> #include <string> using namespace std; #pragma comment(lib, "WSock32.lib") int client(); int server(); int main() { char c; cout << "ã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãã¼ï¼c/s\n>"; cin >> c; if (c == 'c') { client(); } else if (c == 's') { server(); } } int client() { WSADATA wsadata;//ã½ã±ããæ å ±ãæ ¼ç´ããæ§é ä½ //WinSockã®åæå if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) { cout << "winsock startup failed.\n"; return 1; } //æå®ãããµã¼ãã¹ãããã¤ãã¸ã®TCPã½ã±ãããä½æããã const auto sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { cout << "couldn't open a socket.\n"; WSACleanup(); return 1; } //ãã¹ãæ å ±ãåå¾ cout << "ãµã¼ãã¼å\n>"; string server_name; cin >> server_name; auto host = gethostbyname(server_name.c_str()); if (host == nullptr) { auto addr = inet_addr(server_name.c_str()); host = gethostbyaddr(reinterpret_cast<char*>(&addr), 4, AF_INET); if (host == nullptr) { cout << server_name << " is not found.\n"; WSACleanup(); return 1; } } //IPv4ã®ã½ã±ããã¢ãã¬ã¹æ å ±ãè¨å®ãã sockaddr_in sockadd; memset(&sockadd, 0, sizeof(sockadd)); sockadd.sin_family = AF_INET; cout << "ãã¼ãçªå·\n>"; u_short port = 0; cin >> port; sockadd.sin_port = htons(port); sockadd.sin_addr = *(reinterpret_cast<LPIN_ADDR>(*(host->h_addr_list))); //æå®ããã½ã±ããã¸æ¥ç¶ãã if (connect(sock, reinterpret_cast<PSOCKADDR>(&sockadd), sizeof(sockadd)) != 0) { cout << "connect failure.\n"; closesocket(sock); WSACleanup(); return 1; } //ã¡ãã»ã¼ã¸ãéåä¿¡ string buffer; while (true) { cout << "éä¿¡\n>"; cin >> buffer; send(sock, buffer.c_str(), buffer.size(), 0); if (buffer == "end") { break; } cout << "ãµã¼ãã¼ããè¿äºãå¾ ã£ã¦ãã¾ã\n"; char buf[1024]; memset(buf, '\0', sizeof(buf)); const auto rec = recv(sock, buf, sizeof(buf)-1, 0); if (rec == SOCKET_ERROR) { cout << "ã¨ã©ã¼ã§ã\n"; break; } buf[rec] = '\0'; if (strcmp(buf, "end") == 0) { cout << "ãµã¼ãã¼ãæ¥ç¶ãåãã¾ãã\n"; break; } cout << "åä¿¡:" << buf << endl; } //ã½ã±ããã®éåä¿¡ãåæãã shutdown(sock, SD_BOTH); //ã½ã±ãããç ´æ£ãã closesocket(sock); //WinSockã®è§£æ¾ WSACleanup(); cout << "çµäºãã¾ã\n"; return 0; } int server() { WSADATA wsadata;//ã½ã±ããæ å ±ãæ ¼ç´ããæ§é ä½ //WinSockã®åæå if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) { cout << "winsock startup failed.\n"; return 1; } //æå®ãããµã¼ãã¹ãããã¤ãã¸ã®TCPã½ã±ãããä½æããã const auto listen_socket = socket(PF_INET, SOCK_STREAM, 0); if (listen_socket == INVALID_SOCKET) { cout << "couldn't open a socket.\n"; WSACleanup(); return 1; } //IPv4ã®ã½ã±ããã¢ãã¬ã¹æ å ±ãè¨å®ãã sockaddr_in sockadd; memset(&sockadd, 0, sizeof(sockadd)); sockadd.sin_family = AF_INET; cout << "ãã¼ãçªå·\n>"; u_short port = 0; cin >> port; sockadd.sin_port = htons(port); sockadd.sin_addr.s_addr = INADDR_ANY; //ã½ã±ããã«ååãã¤ãã if (bind(listen_socket, reinterpret_cast<sockaddr*>(&sockadd), sizeof(sockadd)) == SOCKET_ERROR) { cout << "bind failure.\n"; closesocket(listen_socket); WSACleanup(); return 1; } //æ¥ç¶å¾ ã¡ç¶æ ã«ãã if (listen(listen_socket, 0) == SOCKET_ERROR) { cout << "listen failure.\n"; closesocket(listen_socket); WSACleanup(); return 1; } //ã¯ã©ã¤ã¢ã³ãããã®æ¥ç¶ãåãå ¥ãã decltype(sockadd) from; int from_length = sizeof(from); SOCKET sock; sock = accept(listen_socket, reinterpret_cast<sockaddr*>(&from), &from_length); if (sock == INVALID_SOCKET) { cout << "accept failure.\n"; closesocket(listen_socket); WSACleanup(); return 1; } cout << inet_ntoa(from.sin_addr) << "ãæ¥ç¶ãã¦ãã¾ãã\n"; closesocket(listen_socket); //ã¡ãã»ã¼ã¸ãéåä¿¡ string buffer; while (true) { cout << "ã¯ã©ã¤ã¢ã³ãããã®åä¿¡ãå¾ ã£ã¦ãã¾ã\n>"; char buf[1024]; memset(buf, '\0', sizeof(buf)); const auto rec = recv(sock, buf, sizeof(buf)-1, 0); if (rec == SOCKET_ERROR) { cout << "ã¨ã©ã¼ã§ã\n"; break; } buf[rec] = '\0'; if (strcmp(buf, "end") == 0) { cout << "ã¯ã©ã¤ã¢ã³ããæ¥ç¶ãåãã¾ãã\n"; break; } cout << "åä¿¡:" << buf << endl; cout << "éä¿¡\n>"; cin >> buffer; send(sock, buffer.c_str(), buffer.size(), 0); if (buffer == "end") { break; } } //ã½ã±ããã®éåä¿¡ãåæãã shutdown(sock, SD_BOTH); //ã½ã±ãããç ´æ£ãã closesocket(sock); //WinSockã®è§£æ¾ WSACleanup(); cout << "çµäºãã¾ã\n"; return 0; }
Boost.Asioã§æ¸ãããã£ããããã°ã©ã
#include <boost/asio.hpp> #include <iostream> #include <string> using namespace std; namespace asio = boost::asio; namespace ip = asio::ip; int client(); int server(); int main() { char c; cout << "ã¯ã©ã¤ã¢ã³ãï¼ãµã¼ãã¼ï¼c/s\n>"; cin >> c; if (c == 'c') { client(); } else if (c == 's') { server(); } } int client() try { asio::io_service io_service; //TCPã½ã±ãããä½æãã ip::tcp::socket sock(io_service); //ãã¹ãæ å ±ãåå¾ cout << "ãµã¼ãã¼å\n>"; string server_name; cin >> server_name; cout << "ãã¼ãçªå·\n>"; u_short port = 0; cin >> port; sock.connect(ip::tcp::endpoint(ip::address::from_string(server_name), port)); //ã¡ãã»ã¼ã¸ãéåä¿¡ string buffer; while (true) { cout << "éä¿¡\n>"; cin >> buffer; asio::write(sock, asio::buffer(buffer)); if (buffer == "end") { break; } cout << "ãµã¼ãã¼ããè¿äºãå¾ ã£ã¦ãã¾ã\n"; asio::streambuf receive_buffer; boost::system::error_code error; asio::read(sock, receive_buffer, asio::transfer_at_least(1), error); if (error && error != asio::error::eof) { std::cout << "receive failed: " << error.message() << std::endl; } else if (asio::buffer_cast<const char*>(receive_buffer.data()) == string("end")) { cout << "ãµã¼ãã¼ãæ¥ç¶ãåãã¾ãã\n"; break; } cout << "åä¿¡:" << &receive_buffer << endl; } return 0; } catch (exception& e) { cout << e.what(); return 1; } int server() try { asio::io_service io_service; //TCPã½ã±ãããä½æãã ip::tcp::socket sock(io_service); //IPv4ã®ã½ã±ããã¢ãã¬ã¹æ å ±ãè¨å®ãã cout << "ãã¼ãçªå·\n>"; u_short port = 0; cin >> port; ip::tcp::acceptor acceptor(io_service, ip::tcp::endpoint(ip::tcp::v4(), port)); //ã¯ã©ã¤ã¢ã³ãããã®æ¥ç¶ãåãå ¥ãã acceptor.accept(sock); //ã¡ãã»ã¼ã¸ãéåä¿¡ string buffer; while (true) { cout << "ã¯ã©ã¤ã¢ã³ãããã®åä¿¡ãå¾ ã£ã¦ãã¾ã\n>"; asio::streambuf receive_buffer; boost::system::error_code error; asio::read(sock, receive_buffer, asio::transfer_at_least(1), error); if (error && error != asio::error::eof) { std::cout << "receive failed: " << error.message() << std::endl; } else if (asio::buffer_cast<const char*>(receive_buffer.data()) == string("end")) { cout << "ã¯ã©ã¤ã¢ã³ããæ¥ç¶ãåãã¾ãã\n"; break; } cout << "åä¿¡:" << &receive_buffer << endl; cout << "éä¿¡\n>"; cin >> buffer; asio::write(sock, asio::buffer(buffer)); if (buffer == "end") { break; } } return 0; } catch (exception& e) { cout << e.what(); return 1; }
次ãããã¯ã¼ã¯ããã°ã©ã ãæ¸ãæã¯éåæéä¿¡ã«ææ¦ãã¦ã¿ããã¨ããã¾ãã