C++网络通信 Socket

举报
AI浩 发表于 2021/12/23 01:46:38 2021/12/23
【摘要】 目录 服务端: 客户端: 客户端第二种写法:不使用inet_pton函数 服务端: #include <iostream> #include <stdio.h>  #include <winsock2.h>  #include <Ws2tcpip.h>...

目录

服务端:

客户端:

客户端第二种写法:不使用inet_pton函数


服务端:

#include <iostream>

#include <stdio.h> 

#include <winsock2.h> 

#include <Ws2tcpip.h>

#pragma comment(lib,"ws2_32.lib"

#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

using namespace std;

int main(int argc, char* argv[])

{

    //初始化WSA 

    WORD sockVersion = MAKEWORD(2, 2);

    WSADATA wsaData;

    if (WSAStartup(sockVersion, &wsaData) != 0)

    {

        return 0;

    }

    //创建套接字 

    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (slisten == INVALID_SOCKET)

    {

        printf("socket error !");

        return 0;

    }

    //绑定IP和端口 

    sockaddr_in sin;

    sin.sin_family = AF_INET;

    sin.sin_port = htons(8888);

    sin.sin_addr.S_un.S_addr = INADDR_ANY;

    if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)

    {

        printf("bind error !");

    }

    //开始监听 

    if (listen(slisten, 5) == SOCKET_ERROR)

    {

        printf("listen error !");

        return 0;

    }

    //循环接收数据 

    SOCKET sClient;

    sockaddr_in remoteAddr;

    int nAddrlen = sizeof(remoteAddr);

    char revData[255];

    while (true)

    {

        printf("等待连接...\n");

        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);

        if (sClient == INVALID_SOCKET)

        {

            printf("accept error !");

            continue;

        }

        char ip[100];

        if (inet_ntop(AF_INET, &remoteAddr.sin_addr.S_un.S_addr, ip, 100) == NULL)

        {

            cout << "error" << endl;

            return -1;

        }

        cout << ip << endl; //宽字符记得用宽字符的方式进行输出

        //接收数据 

        int ret = recv(sClient, revData, 255, 0);

        if (ret > 0)

        {

            revData[ret] = 0x00;

            printf(revData);

        }

        //发送数据 

        const char * sendData = "你好,TCP客户端!\n";

        send(sClient, sendData, strlen(sendData), 0);

        closesocket(sClient);

    }

    closesocket(slisten);

    WSACleanup();

    return 0;

}

客户端:

#include<iostream>

#include<WINSOCK2.H>

#include<STDIO.H>

#include<cstring>

#include <Ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

using namespace std;

int main()

{

    WORD sockVersion = MAKEWORD(2, 2);

    WSADATA data;

    if (WSAStartup(sockVersion, &data) != 0)

    {

        return 0;

    }

    while (true) {

        SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (sclient == INVALID_SOCKET)

        {

            printf("invalid socket!");

            return 0;

        }

 

        sockaddr_in serAddr;

        serAddr.sin_family = AF_INET;

        serAddr.sin_port = htons(8888);

        inet_pton(AF_INET, "127.0.0.1", &serAddr.sin_addr.S_un.S_addr);

        if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)

        {  //连接失败

            printf("connect error !");

            closesocket(sclient);

            return 0;

        }

 

        char data[1024];

        cin >> data;

        const char * sendData;

        sendData = data;   //string转const char*

        //char * sendData = "你好,TCP服务端,我是客户端\n";

        send(sclient, sendData, strlen(sendData), 0);

        //send()用来将数据由指定的socket传给对方主机

        //int send(int s, const void * msg, int len, unsigned int flags)

        //s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0

        //成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error

        char recData[255];

        int ret = recv(sclient, recData, 255, 0);

        if (ret > 0) {

            recData[ret] = 0x00;

            printf(recData);

        }

        closesocket(sclient);

    }

    WSACleanup();

    return 0;

 

}

客户端第二种写法:不使用inet_pton函数

#include<iostream>

#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

#include<WINSOCK2.H>

#include<STDIO.H>

#include<cstring>

 

#pragma comment(lib, "ws2_32.lib")

 

using namespace std;

int main()

{

    WORD sockVersion = MAKEWORD(2, 2);

    WSADATA data;

    if (WSAStartup(sockVersion, &data) != 0)

    {

        return 0;

    }

    while (true) {

        SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (sclient == INVALID_SOCKET)

        {

            printf("invalid socket!");

            return 0;

        }

 

        sockaddr_in serAddr;

        serAddr.sin_family = AF_INET;

        serAddr.sin_port = htons(8888);

        serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

        if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)

        {  //连接失败

            printf("connect error !");

            closesocket(sclient);

            return 0;

        }

        char data[1024];

        cin >> data;

        const char * sendData;

        sendData = data;   //string转const char*

        //char * sendData = "你好,TCP服务端,我是客户端\n";

        send(sclient, sendData, strlen(sendData), 0);

        char recData[255];

        int ret = recv(sclient, recData, 255, 0);

        if (ret > 0) {

            recData[ret] = 0x00;

            printf(recData);

        }

        closesocket(sclient);

    }

    WSACleanup();

    return 0;

}

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/108062794

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。