网络通信TCP的例子

服务器:

#include <WinSock2.h>  
#include <stdio.h>  
#include <stdlib.h>  

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

void main()
{
    WSADATA wsaData;
    int port = 5099;

    char buf[] = "Server: hello, I am a server.....";

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("Failed to load Winsock");
        return;
    }

 
    SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); //创建用于监听的套接字

    SOCKADDR_IN addrSrv;
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(port); //1024以上的端口号  
    addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

    int retVal = bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN));  //将端口ip和套接字绑定起来
    if (retVal == SOCKET_ERROR){
        printf("Failed bind:%d\n", WSAGetLastError());
        return;
    }

    //给外部暴露10个端口(套接字)
    if (listen(sockSrv, 10) == SOCKET_ERROR){         
        printf("Listen failed:%d", WSAGetLastError());
        return;
    }
    SOCKET sockConn;
    SOCKADDR_IN addrClient;
    int len = sizeof(SOCKADDR);
    bool ax = true;
    while (ax)
    {
        //等待客户请求到来(一直在监听这个端口看有没有人链接,如果有链接 那么判断是不是符合的)    
        sockConn = accept(sockSrv, (SOCKADDR *)&addrClient, &len);
        if (sockConn == SOCKET_ERROR) {
            printf("Accept failed:%d", WSAGetLastError());
            break;
        }

        printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr));

        //发送数据  
        int iSend = send(sockConn, buf, sizeof(buf), 0);
        ax = false;
        if (iSend == SOCKET_ERROR) {
            printf("send failed");
            break;
        }

    }
        while (1)
        {
        char recvBuf[100];
        memset(recvBuf, 0, sizeof(recvBuf));
        //      //接收数据  
        recv(sockConn, recvBuf, sizeof(recvBuf), 0);
        printf("%s\n", recvBuf);

        closesocket(sockConn);
    }

    closesocket(sockSrv);
    WSACleanup();
    
    system("pause");
}

客户端:

#include <WinSock2.h>  
#include <stdio.h>  
#include <iostream>
#pragma comment(lib, "ws2_32.lib")  
using namespace std;
void main()
{
    //加载套接字  
    WSADATA wsaData;
    char buff[1024];
    memset(buff, 0, sizeof(buff));

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("Failed to load Winsock");
        return;
    }

    SOCKADDR_IN addrSrv;
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(5099);
    addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

    //创建套接字  
    SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
    if (SOCKET_ERROR == sockClient){
        printf("Socket() error:%d", WSAGetLastError());
        return;
    }

    //向服务器发出连接请求  
    int ret = connect(sockClient, (struct  sockaddr*)&addrSrv, sizeof(addrSrv));
    if (ret == INVALID_SOCKET){
        printf("Connect failed:%d", WSAGetLastError());
        return;
    }
    else
    {
        //接收数据  
        recv(sockClient, buff, sizeof(buff), 0);
        printf("%s\n", buff);
    }

    //发送数据  
    char buff1[] = "hello, this is a Client....";
    send(sockClient, buff1, sizeof(buff1), 0);

    while (true)
    {
        cin >> buff1;
        send(sockClient, buff1, sizeof(buff1), 0);
    }
    //cin>> buff1;
    //send(sockClient, buff1, sizeof(buff1), 0);
    //关闭套接字  
    closesocket(sockClient);
    WSACleanup();

    system("pause"); // 程序运行到此处停止向下继续运行

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力攀爬的菜鸟_为自己努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值