SOCKET 通信

TCP

服务端

#include<iostream>
#include<winsock.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;

void initialization() {
    //初始化套接字库
    WORD w_req = MAKEWORD(2, 2);//版本号
    WSADATA wsadata;
    /*WSAStartup()程序使用的socket版本、操作系统但会的支持的socket版本,成功时返回0*/
    int err = WSAStartup(w_req, &wsadata);
    if (err != 0) {
        cout << "初始化套接字库失败!" << endl;
    }
    else {
        cout << "初始化套接字库成功!" << endl;
    }
    //检测版本号
    if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
        cout << "套接字库版本号不符!" << endl;
        WSACleanup();
    }
    else {
        cout << "套接字库版本正确!" << endl;
    }
}
int main() {
    //定义长度变量
    int send_len = 0, recv_len = 0;
    int len = 0;
    //定义发送缓冲区和接受缓冲区
    char send_buf[100], recv_buf[100];
    //定义服务端套接字,接受请求套接字
    SOCKET s_server, s_accept;
    //服务端地址客户端地址
    SOCKADDR_IN server_addr, accept_addr;
    initialization();
    //填充服务端信息
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(1234);
    //创建套接字
    s_server = socket(AF_INET, SOCK_STREAM, 0);
    if (bind(s_server, (SOCKADDR*)& server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
        cout << "套接字绑定失败!" << endl;
        WSACleanup();
    }
    else {
        cout << "套接字绑定成功!" << endl;
    }
    while (1) {
        //设置套接字为监听状态
        if (listen(s_server, SOMAXCONN) < 0) {
            cout << "设置监听状态失败!" << endl;
            WSACleanup();
        }
        else {
            cout << "设置监听状态成功!" << endl;
        }
        cout << "服务端正在监听连接,请稍候...." << endl;
        //接受连接请求
        len = sizeof(SOCKADDR);
        s_accept = accept(s_server, (SOCKADDR*)& accept_addr, &len);
        if (s_accept == SOCKET_ERROR) {
            cout << "连接失败!" << endl;
            WSACleanup();
            return 0;
        }
        cout << "连接建立,准备接受数据" << endl;
        //接收数据
        recv_len = recv(s_accept, recv_buf, 100, 0);
        if (recv_len < 0) {
            cout << "接受失败!" << endl;
            break;
        }
        else {
            cout << "客户端信息:" << recv_buf << endl;
        }
        /*cout << "请输入回复信息:";
        cin >> send_buf;
        send_len = send(s_accept, send_buf, 100, 0);
        if (send_len < 0) {
            cout << "发送失败!" << endl;
            break;
        }*/
    }
    //关闭套接字
    closesocket(s_server);
    closesocket(s_accept);
    //释放DLL资源
    WSACleanup();
    return 0;
}
View Code

客户端

#include <iostream>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;

void initialization() {
    WORD w_req = MAKEWORD(2, 2);
    WSADATA wsadata;
    int err = WSAStartup(w_req, &wsadata);
    if (err != 0) {
        cout << "初始化套接字库失败!" << endl;
    }
    else {
        cout << "初始化套接字库成功!" << endl;
    }
    if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
        cout << "套接字库版本号不符!" << endl;
        WSACleanup();
    }
    else {
        cout << "套接字库版本正确!" << endl;
    }
}

int main()
{
    int send_len, recv_len;
    char send_buf[100], recv_buf[100];
    SOCKET s_server;
    SOCKADDR_IN server_addr;
    initialization();
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(1234);

    s_server = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(s_server, (SOCKADDR*)& server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
        cout << "服务器连接失败!" << endl;
        WSACleanup();
    }
    else {
        cout << "服务器连接成功!" << endl;
    }

    while (1) {
        cout << "请输入发送信息:";
        cin >> send_buf;
        send_len = send(s_server, send_buf, 100, 0);
        if (send_len < 0) {
            cout << "发送失败!" << endl;
            break;
        }
        /*recv_len = recv(s_server, recv_buf, 100, 0);
        if (recv_len < 0) {
            cout << "接受失败!" << endl;
            break;
        }
        else {
            cout << "服务端信息:" << recv_buf << endl;
        }*/
    }
    //关闭套接字
    closesocket(s_server);
    //释放DLL资源
    WSACleanup();
    return 0;
}
View Code

参考链接:

【1】https://blog.csdn.net/qq_27923041/article/details/83857964

【2】关于socket函数参数的说明

posted @ 2019-11-17 20:52  啦啦啦天啦噜  阅读(202)  评论(0编辑  收藏  举报