socket__01

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dong1528313271/article/details/80356549

小结下windows的socket的使用流程

1.服务端:
我们的服务端主要功能是等待用户连接,给连接用户提供服务
所以我们必须形成一个互联网可见的节点(整个internet的唯一节点:ip+port)
所以在服务端我们必须明确生成一个节点(必须为service绑定port)


2.客户端
在客户端需要的时候向服务端发起连接,请求服务
所以客户端是可不连接可连接的,发起连接是可有本机系统为程序分配端口 (connect),即可以不显示bind,绑定端口


流程如下:

1.初始化SOCKET版本信息
2.定义socket
3.服务端bind port+ip形成Internet上的唯一节点        【客户端可绑定可不绑定】
4.服务端listen监听+accept       客户端connect
5.服务端客户端交互
service:
#include<iostream>
#include<windows.h>
#include<string>
#include<stdlib.h>

#pragma comment(lib,"ws2_32.lib")
int main()
{
    //初始化socket版本号
    WSADATA wsa;
    WSAStartup(MAKEWORD(2,2),&wsa);

    //创建socket
    SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
    if (sock < 0)
    {
        std::cout << "create sock failed" << std::endl;
        return -1;
    }

    //绑定ip+port
    sockaddr_in addr;
    addr.sin_addr.S_un.S_addr = INADDR_ANY;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);

    bind(sock,(sockaddr*)&addr,sizeof(addr));

    //监听
    if (0 != listen(sock, 10))
    {
        std::cout << "listen sock failed" << std::endl;
        closesocket(sock);
        return -2;
    }
    //accept:
    sockaddr_in saddr;
    int len = sizeof(saddr);
    std::cout << "wait for connect..." << std::endl;
    SOCKET client = accept(sock,(sockaddr*)&saddr,&len);
    std::cout << "ip address:" << inet_ntoa(saddr.sin_addr) << "\nport:" << ntohs(saddr.sin_port) << std::endl;

    //交互
    char buf[1024];
    send(client,"connected.....",15,0);

    while (true)
    {
        int revlen = recv(client,buf,1024-1,0);
        buf[revlen] = 0x00;


        if (revlen <= 0 || strcmp("quit", buf) == 0)
        {
            std::cout << "qiut successful" << std::endl;
            send(client,"qiut ok",9,0);
            break;
        }
        std::cout << buf << std::endl;
        send(client,"recived",8,0);
    }
    closesocket(client);
    closesocket(sock);
    return 0;
}

//client:

#include<iostream>
#include<windows.h>
#include<string>
#include<stdlib.h>

#pragma comment(lib,"ws2_32.lib")
int main()
{
    //初始化socket版本信息
    WSADATA wsa;
    WSAStartup(MAKEWORD(2, 2), &wsa);

    //创建socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        std::cout << "create sock failed" << std::endl;
        return -1;
    }
    //申请连接
    sockaddr_in addr;
    addr.sin_addr.S_un.S_addr = inet_addr("172.17.1.9");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8888);

    //用try——catch模型抛出异常
    try{
        if (SOCKET_ERROR == connect(sock, (sockaddr*)&addr, sizeof(addr)))
            throw WSAGetLastError();
    }
    catch (int e)
    {
        std::cout << "ERROE CODE: " << e << std::endl;
        closesocket(sock);
        return 0;
    }



    //交互
    char buf[1024];
    while (true)
    {
        std::cin.getline(buf,1024-1,'\n');
        buf[std::cin.gcount()] = 0;
        send(sock, buf, strlen(buf), 0);
        int len = recv(sock,buf,1024-1,0);
        buf[len] = 0;
        std::cout << buf << std::endl;
        buf[0] = 0;
    }
    closesocket(sock);
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页