标题:萌新求助(TCP聊天)
只看楼主
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
结帖率:33.33%
已结贴  问题点数:20 回复次数:5 
萌新求助(TCP聊天)
按教材上编了一个TCP聊天程序,但显示
E:\CPP\tx\main.c|27|undefined reference to `__imp_WSAStartup'|
E:\CPP\tx\main.c|35|undefined reference to `__imp_WSACleanup'|
E:\CPP\tx\main.c|39|undefined reference to `__imp_htonl'|
E:\CPP\tx\main.c|40|undefined reference to `__imp_htons'|
E:\CPP\tx\main.c|41|undefined reference to `__imp_socket'|
E:\CPP\tx\main.c|44|undefined reference to `__imp_bind'|
E:\CPP\tx\main.c|49|undefined reference to `__imp_listen'|
E:\CPP\tx\main.c|56|undefined reference to `__imp_accept'|
E:\CPP\tx\main.c|64|undefined reference to `__imp_recv'|
E:\CPP\tx\main.c|83|undefined reference to `__imp_closesocket'|
E:\CPP\tx\main.c|84|undefined reference to `__imp_closesocket'|
E:\CPP\tx\main.c|85|undefined reference to `__imp_WSACleanup'|
E:\CPP\tx\main.c|77|undefined reference to `__imp_send'|
||error: ld returned 1 exit status|
服务端的代码:
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
int main()
{
    char Sendbuf[100];
    char Receivebuf[100];
    int SendLen;
    int ReceiveLen;
    int length;

    SOCKET socket_server;
    SOCKET socket_receive;

    SOCKADDR_IN Server_add;
    SOCKADDR_IN Client_add;

    WORD wVersionReuested;
    WSADATA wsaData;
    int error;




    wVersionReuested=MAKEWORD(2,2);

    error=WSAStartup(wVersionReuested,&wsaData);
    if(error!=0)
    {
        printf("加载套接字失败!");
    }
    if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)

    {
        WSACleanup();
        return 0;
    }
    Server_add.sin_family=AF_INET;
    Server_add.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    Server_add.sin_port=htons(5000);
    socket_server=socket(AF_INET,SOCK_STREAM,0);


    if(bind(socket_server,(SOCKADDR*)&Server_add,sizeof(SOCKADDR))==SOCKET_ERROR)
    {
        printf("绑定失败\n");
    }

    if(listen(socket_server,5)<0)
    {
        printf("监听失败\n");
    }

    length=sizeof(SOCKADDR);

    socket_receive=accept(socket_server,(SOCKADDR*)&Client_add,&length);
    if(socket_receive==SOCKET_ERROR)
    {
        printf("连接失败\n");
    }

    while(1)
    {
        ReceiveLen=recv(socket_receive,Receivebuf,100,0);
        if(ReceiveLen<0)
        {
            printf("接收失败/n");
            printf("程序退出\n");
            break;
        }
        else
        {
            printf("client say:%s\n",Receivebuf);
        }
        printf("please enter the massage:");
        scanf("%s",Sendbuf);
        SendLen=send(socket_receive,Sendbuf,100,0);
        if(SendLen<0)
        {
            printf("发送失败\n");
        }
    }
    closesocket(socket_receive);
    closesocket(socket_server);
    WSACleanup();
    return 0;
}
客户端的代码:
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
int main()
{
    char Sendbuf[100];
    char Receivebuf[100];
    int SendLen;
    int ReceiveLen;
    int length;

    SOCKET socket_server;
    SOCKET socket_receive;

    SOCKADDR_IN Server_add;
    SOCKADDR_IN Client_add;

    WORD wVersionReuested;
    WSADATA wsaData;
    int error;




    wVersionReuested=MAKEWORD(2,2);

    error=WSAStartup(wVersionReuested,&wsaData);
    if(error!=0)
    {
        printf("加载套接字失败!");
    }
    if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)

    {
        WSACleanup();
        return 0;
    }
    Server_add.sin_family=AF_INET;
    Server_add.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    Server_add.sin_port=htons(5000);
    socket_server=socket(AF_INET,SOCK_STREAM,0);


    if(bind(socket_server,(SOCKADDR*)&Server_add,sizeof(SOCKADDR))==SOCKET_ERROR)
    {
        printf("绑定失败\n");
    }

    if(listen(socket_server,5)<0)
    {
        printf("监听失败\n");
    }

    length=sizeof(SOCKADDR);

    socket_receive=accept(socket_server,(SOCKADDR*)&Client_add,&length);
    if(socket_receive==SOCKET_ERROR)
    {
        printf("连接失败\n");
    }

    while(1)
    {
        ReceiveLen=recv(socket_receive,Receivebuf,100,0);
        if(ReceiveLen<0)
        {
            printf("接收失败/n");
            printf("程序退出\n");
            break;
        }
        else
        {
            printf("client say:%s\n",Receivebuf);
        }
        printf("please enter the massage:");
        scanf("%s",Sendbuf);
        SendLen=send(socket_receive,Sendbuf,100,0);
        if(SendLen<0)
        {
            printf("发送失败\n");
        }
    }
    closesocket(socket_receive);
    closesocket(socket_server);
    WSACleanup();
    return 0;
}
有几个地方不知道该填什么地址,还有上面的几个错误。希望有大佬指点。


搜索更多相关主题的帖子: CPP undefined reference main printf 
2021-09-14 18:58
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
lack of ws2_32.lib
https://
https://bbs.


2021-09-14 19:09
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
https://docs.
https://docs.

good luck!
2021-09-14 19:49
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
Server_add.sin_family = AF_INET;
    Server_add.sin_addr.S_un.S_addr = htonl("192.168.1.10");
    Server_add.sin_port = htons(5000);
    socket_server = socket(AF_INET, SOCK_STREAM, 0);
请问,这段代码里的主机ip应不应该这么填


[此贴子已经被作者于2021-9-14 21:15编辑过]

2021-09-14 21:13
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
无线局域网适配器 WLAN:

   连接特定的 DNS 后缀 . . . . . . . :
   IPv6 地址 . . . . . . . . . . . . : 2409:8a1e:c918:3bb0:f098:aea1:90ea:f133
   临时 IPv6 地址. . . . . . . . . . : 2409:8a1e:c918:3bb0:f86e:152b:2a80:3374
   本地链接 IPv6 地址. . . . . . . . : fe80::f098:aea1:90ea:f133%14
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.10
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : fe80::1%14
                                       192.168.1.1

以上是我电脑上输入ipconfig得到的信息。
2021-09-14 21:19
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:20 
https://
https://

[此贴子已经被作者于2021-9-14 21:36编辑过]

2021-09-14 21:32
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
但还是提示找不到ws2_32.lib
2021-09-21 20:14
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
电脑里有文件,但提示ld.exe||cannot find -lws2_32.lib|
2021-09-21 20:15
bigbigeric
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2021-9-11
得分:0 
用的是codelocks
大佬能在指导一下吗?
2021-09-21 20:17
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
得分:0 
回复 9楼 bigbigeric
https://blog.
https://bbs.

[此贴子已经被作者于2021-9-21 20:25编辑过]

2021-09-21 20:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506843-1-1.html




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

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