C++ socket 学习--服务端与客户端通信

服务端

#include
#include
#include
#include<string>
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#include "Server.h"

using namespace std;

int Server::run()
{
    cout << "--服务器--" << endl;

    //初始化
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);// make word ,  

    //创建服务器的套接字
    SOCKET serviceSocket;
    serviceSocket = socket(AF_INET, SOCK_STREAM, 0);   //0--protocol   这里我们默认为零 

    if (SOCKET_ERROR == serviceSocket) {
        cout << "套接字创建失败" << endl;
    }
    else {
        cout << "套接字创建成功" << endl;
    }

    //绑定套接字    IP  端口号
    sockaddr_in socketAddr;
    memset(&socketAddr, 0, sizeof(socketAddr));//各位清零,防止乱码 
    socketAddr.sin_family = AF_INET;  //#define AF_INET 2    协议族
    inet_pton(AF_INET, "127.0.0.1", (void*)&socketAddr.sin_addr.S_un.S_addr);
    socketAddr.sin_port = htons(1234);
    int bRes = bind(serviceSocket, (SOCKADDR*)&socketAddr, sizeof(SOCKADDR));//绑定--注意强制类型转换
    if (SOCKET_ERROR == bRes) {
        cout << "绑定失败" << endl;
    }
    else {
        cout << "绑定成功" << endl;
    }

    //服务器监听
    int lLen = listen(serviceSocket, 5);//表示能监听多少个请求
    if (SOCKET_ERROR == lLen) {
        cout << "监听失败" << endl;
    }
    else {
        cout << "监听成功" << endl;
    }

    //接受请求
    sockaddr_in revClientAddr;
    SOCKET recvClientSocket = INVALID_SOCKET;  //初始化一个接受的客户端socket
    int _revSize = sizeof(sockaddr_in);
    recvClientSocket = accept(serviceSocket, (SOCKADDR*)&revClientAddr, &_revSize);
    if (INVALID_SOCKET == recvClientSocket) {
        cout << "服务端请求失败" << endl;
    }
    else {
        cout << "服务端接受请求成功" << endl;
    }

    //发送/接受 数据
    char recvBuf[1024] = {};
    int reLen = recv(recvClientSocket, recvBuf, 1024, 0);
    int sLen = send(recvClientSocket, recvBuf, reLen, 0);
    if (SOCKET_ERROR == reLen) {
        cout << "服务端发送数据失败" << endl;
    }
    else {
        cout << "服务端接收到数据: " << recvBuf << endl << endl;
    }

    //关闭socket
    closesocket(recvClientSocket);
    closesocket(serviceSocket);

    //终止
    WSACleanup();

    cout << "服务器停止" << endl;
    cin.get();
    return 0;
}

客户端

#include
#include
#include
#include<string>
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
#include "Client.h"

using namespace std;

int Client::run()
{
    cout << "--客户端--" << endl;

    //初始化
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);

    //创建套接字
    SOCKET clientSocket;
    clientSocket = socket(PF_INET, SOCK_STREAM, 0);
    if (SOCKET_ERROR == clientSocket) {
        cout << "套接字创建失败" << endl;
    }
    else {
        cout << "套接字创建成功" << endl;
    }

    //绑定套接字
    sockaddr_in socketAddr;
    memset(&socketAddr, 0, sizeof(socketAddr));//各位清零,防止乱码 
    socketAddr.sin_family = PF_INET;
    //socketAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//inet_addr("127.0.0.1");htonl(INADDR_ANY); //
    inet_pton(AF_INET, "127.0.0.1", (void*)&socketAddr.sin_addr.S_un.S_addr);
    socketAddr.sin_port = htons(1234);
    int cRes = connect(clientSocket, (SOCKADDR*)&socketAddr, sizeof(SOCKADDR));
    if (SOCKET_ERROR == cRes) {
        cout << "客户端:\t\t与服务器连接失败" << endl;
    }
    else {
        cout << "客户端:\t\t与服务器连接成功" << endl;
    }

    //发送请求
    char sendBuf[1024] = "from Client: hello service";
    send(clientSocket, sendBuf, strlen(sendBuf), 0);

    //发送/接受数据
    char recvBuf[1024] = {};
    recv(clientSocket, recvBuf, 1024, 0);
    cout << "客户端接受数据 :  " << recvBuf << endl << endl;

    //关闭socket
    closesocket(clientSocket);

    //终止
    WSACleanup();

    cout << "客户端退出" << endl;
    cin.get();
    return 0;
}

 

你可能感兴趣的:(C++ socket 学习--服务端与客户端通信)