
本文共 5276 字,大约阅读时间需要 17 分钟。
1.���IP���������IP������ 2.���IP������������������������������������������������������IP������������������������������ 3.IP��������������������������������������������������� 4.������������������2������16������������ 5.��������������������������������������������������������������������������������������������������������� 6.IP������+������������������������������������������������������������������ 7.��������������������������������������������������������������������������������� 8.������pid������������������������ 9.��������������������������������������� 10.���������+IP��������������������������������� 11.������������pid��������������������������������������������������������������������������������������������������������������������� 12.������������������������������������������������ 13.������������������������������������������������������������IP��������������������������������� 14.TCP��������������������������������������� 15.TCP��������������������������������������������������������� 16.TCP������������������������������������������������ 17.TCP������������������TCP��������������������������������������������������������������������������������� 18.TCP���������������TCP��������������������������������������������������� 19.TCP������������������������������������������������������������������������ 20.TCP���������������������������������������������������������������������������������������������
22.UDP��������������������������� 23.UDP������������������������������������������������������������ 24.UDP������������������������������������ 25.UDP��������������������������� 26.��������������������������������������������������������� 27.������������������������������������������ 28.������������������������������������������������������������������������������������������������ 29.������������������������������������������������������������������������ 30.sockaddr���������������������������������
class udpServer { private: string ip; int port; int sock; public: udpServer(string _ip = "127.0.1.1", int _port = 7080) : ip(_ip), port(_port) {} void initServer() { sock = socket(AF_INET, SOCK_DGRAM, 0); cout << "socket created" << endl; struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(port); local.sin_addr.in_addr = inet_addr(ip.c_str()); if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0) { cerr << "bind error" << endl; exit(1); } } void start() { while (true) { struct sockaddr_in endpoint; socklen_t len = sizeof(endpoint); char buff[88] = {0}; ssize_t size = recvfrom(sock, buff, sizeof(buff)-1, 0, (struct sockaddr *)&endpoint, &len); if (size > 0) { buff[size] = '\0'; cout << "Client message: " << buff << endl; } } } }; class udpClient { private: string ip; int port; int sock; public: udpClient(string _ip = "127.0.1.1", int _port = 7080) : ip(_ip), port(_port) {} void initClient() { sock = socket(AF_INET, SOCK_DGRAM, 0); cout << "socket created" << endl; } void start() { while (true) { struct sockaddr_in peer; peer.sin_family = AF_INET; peer.sin_port = htons(port); peer.sin_addr.s_addr = inet_addr(ip.c_str()); char buff[88] = {0}; cout << "please enter your message# "; string send_string; getline(cin, send_string); sendto(sock, send_string.c_str(), send_string.size(), 0, (struct sockaddr *)&peer, sizeof(peer)); buff[0] = '\0'; ssize_t size = recvfrom(sock, buff, sizeof(buff)-1, 0, nullptr, sizeof(buff)-1); if (size > 0) { buff[size] = '\0'; cout << "Serve: " << buff << endl; } } } }; #include#include #include #include #include #include #include #include #include
发表评论
最新留言
关于作者
