Socket编程
发布日期:2021-05-15 02:58:14 浏览次数:26 分类:精选文章

本文共 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���������������������������������

  • TCP/IP������������������������������������������������������������
  • UDP������
  • sock
  • ���������
  • ���������������
  • ���������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������
  • UDP���������������������
  • sendto���recvfrom���������������UDP������������������������������
  • ���������������������������bind������������������������������������IP������������������
  • ������������������������������������������������������������������������������������
  • 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
    上一篇:内网穿透,微信测试号验证
    下一篇:python 编写web 服务器软件

    发表评论

    最新留言

    第一次来,支持一个
    [***.219.124.196]2025年04月15日 14时33分35秒