简单实现TCP客户端与服务器之间的通信(软件:VS2013)

新建两个控制台程序,分别命名为TCPServer_demo,TCPClient_demo.

TCPServer_demo

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

int main(int argc, char* argv[])
{
    //初始化WSA windows自带的socket
    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) //将服务端套接字与上面的ip和端口绑定 
    {
        printf("bind error !");
    }

    //开始监听
    if (listen(slisten, 5) == SOCKET_ERROR)  //用listen() 监听前面绑定好的slisten套接字
    {
        printf("listen error !");
        return 0;
    }

    //循环接受数据
    SOCKET sClient;  //创建连接的套接字
    sockaddr_in remoteAddr;
    int nAddrlen = sizeof(remoteAddr); //用于接受客户端地址
    char revData[255]; //存储接受的数据
    while (1)
    {
        printf("等待连接...\n");
        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen); //和客户端 connect()对应
        if (sClient == INVALID_SOCKET)
        {
            printf("accept error !");
            continue;
        }
        char sendBuf[20] = { '\0' };
        printf("接受到一个连接:%s \r\n", inet_ntop(AF_INET, (void*)&remoteAddr.sin_addr, sendBuf, 16));

        //数据接收
        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;
}

TCPClient_demo

#include  
#include  
#include  
#include  
#include 
#include
using namespace std;
#pragma comment(lib, "ws2_32.lib")  
int main()
{
    //初始化WSA windows自带的socket
    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", (void*)&serAddr.sin_addr.S_un.S_addr);
        if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) //与指定IP地址和端口的服务端连接
        {
            printf("connect error !");
            closesocket(sclient);
            return 0;
        }

        //string data;
        //cin >> data;
        //const char * sendData;
        //sendData = data.c_str(); //string转const char*
     //   char * sendData1 = "你好,TCP服务端,我是客户端\n";  
        //send(sclient, sendData1, strlen(sendData1), 0);

        printf("输入你想传输的英文: \n");
        string data;
        cin >> data;
        const char * sendData2;
        sendData2 = data.c_str(); //string转const char*
        send(sclient, sendData2, strlen(sendData2), 0);


        char recData[255];
        int ret = recv(sclient, recData, 255, 0);
        if (ret>0){
            recData[ret] = 0x00;
            printf(recData);
        }
        closesocket(sclient);

    }
    WSACleanup();
    return 0;

}

 

你可能感兴趣的:(网络编程)