| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1487 人关注过本帖
标题:C++ SOCKET编程求指教
只看楼主 加入收藏
小子pk了
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-4-6
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
C++ SOCKET编程求指教
我正在学习C++编程,根据教学视频学习了SOCKET编程,编译时遇到错误:LNK1104,无法打开文件"win2_32.lib"
操作系统:windows10
操作系统版本:1909(OS内部版本:18363.752)
程序代码:
#include <cstdio>  //<stdio.h>
#include <cstdlib>  //<stdlib>
#include <WinSock2.h>
#pragma comment(lib,"win2_32.lib")//加载 ws2_32.dll

void main(void)
{
    //1:初始化windows套间字
    WSAData wsaData;
    int ret;
    if ((ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)
    {
        printf("WSAStartup failed with error %d\n", ret);
        return;
    }
    //2:创建监听套件字
    SOCKET listeningSocket;
    if ((listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
    {
        printf("socket failed with error %d\n", WSAGetLastError());
        WSACleanup();
        return;
    }
    //3:把套件字和服务端地址绑定
    SOCKADDR_IN serverAddr;
    int port = 5150;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(port);
    serverAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    if (bind(listeningSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
    {
        printf("bind failed with error %d\n", WSAGetLastError());
        closesocket(listeningSocket);
        WSACleanup();
        return;
    }
    //4:开启监听
    if (listen(listeningSocket, 5) == SOCKET_ERROR)
    {
        printf("listen failed with error %d\n", WSAGetLastError());
        closesocket(listeningSocket);
        WSACleanup();
        return;
    }
    printf("We are waiting a connection  on port %d\n", port);
    printf("Listening(我正在监听......)\n");  // NOLINT(clang-diagnostic-invalid-source-encoding)
    //5:接收客户端的连接请求
    SOCKADDR_IN clientAddr;
    int clientAddrLen;
    SOCKET clientNewConnection;
    clientAddrLen = sizeof(SOCKADDR);
    if ((clientNewConnection = accept(listeningSocket, (sockaddr*)&clientAddr, &clientAddrLen)) == INVALID_SOCKET)
    {
        printf("accept failed with error %d\n", WSAGetLastError());
        closesocket(listeningSocket);
        WSACleanup();
        return;
    }
    //6:打印客户端套件字
    //printf("we successfully got a connection from %s:%d\n", inet_ntoa(clientAddr.sin_addr),ntohs(clientAddr.sin_port));
    closesocket(listeningSocket);
    //7:服务器接受客户端套件字
    printf("we are waiting to receive data \n");
    char dataBuffer[1024];
    if ((ret = recv(clientNewConnection, dataBuffer, sizeof(dataBuffer), 0)) == SOCKET_ERROR)
    {
        printf("receive failed with error %d\n", WSAGetLastError());
        closesocket(clientNewConnection);
        WSACleanup();
        return;
    }
    printf("we successfully received %d bytes. \n", ret);
    dataBuffer[ret] = '\0';
    printf("%s\n", dataBuffer);
    //8:关闭连接
    printf("we are now going to close the client connection.\n");
    closesocket(clientNewConnection);
    WSACleanup();
    system("pause");
}

搜索更多相关主题的帖子: printf SOCKET failed return with 
2020-04-06 17:59
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:5 
ws2_32.lib

https://zh.
2020-04-06 18:03
小子pk了
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-4-6
收藏
得分:0 
我也按照网上说的修改了链接地址,还是不管用。
2020-04-07 22:15
雪影辰风
Rank: 6Rank: 6
来 自:衡阳市
等 级:贵宾
威 望:22
帖 子:177
专家分:387
注 册:2019-6-17
收藏
得分:5 
你用的IDE?
我用的VS2019,把main类型设为int,return全部替换成return 0
再改成连接ws2_32.lib(就是二楼那个)是可以正常运行的,不行的话可能是IDE问题。。。
那……,有问题再说吧。。。
2020-04-07 22:50
快速回复:C++ SOCKET编程求指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023880 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved