| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1827 人关注过本帖
标题:ld returned 1 exit status这个错误怎么解
只看楼主 加入收藏
捕鱼儿海
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-9-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
ld returned 1 exit status这个错误怎么解
#include<sys/types.h>
//#include<sys/socket.h>
//#include<netinet/in.h>
//#include<netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
 
#define PORTNUMBER 333

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

#define QueueLen 6

int visits = 0;

int  main(int argc, char * argv[])
{
    WORD version;
    version = MAKEWORD(1, 1);
    WSADATA wsa;
    if(0 != WSAStartup(version, &wsa)){ //初始化socket()
        printf("WSAStartup error!\n");
        exit(0);
    }
//    struct hostent * ptrh;
    struct sockaddr_in servaddr;
    struct sockaddr_in clientaddr;
    int listenfd;
    int clientfd;
    int port;
    int alen;
   
    char buf[1000];
    memset(&servaddr, 0, sizeof(servaddr)); //初始化servaddr为0

    servaddr.sin_family = AF_INET;//主机地址标识的数据结构,使用TCP/IP协议
    servaddr.sin_addr.s_addr = INADDR_ANY;//32位的IP地址,网络字节顺序
    if(argc > 1){
        port = atoi(argv[1]);
    } else {
        port = PORTNUMBER;
    }
    if(port > 0){
        servaddr.sin_port = htons((unsigned short) port); //把端口转换成网络序
    } else {
        fprintf(stderr, "bad port number%d\n", argv[1]);
        exit(0);
    }
   
    listenfd =  socket(AF_INET, SOCK_STREAM, IPPROTO_IP ); //注册一个socket description
    DWORD error = GetLastError();
    if(listenfd < 0){
        fprintf(stderr, "socket createion failed!\n");
        exit(0);
    }
    if(bind(listenfd, (struct sockaddr *) & servaddr, sizeof(servaddr)) < 0){//绑定到端口
        fprintf(stderr, "bind failed!\n");
        exit(0);
    }
    if(listen(listenfd, QueueLen) < 0){//开始监听端口
        fprintf(stderr, "listen failed!\n");
        exit(0);
    }
    while(1){//主循环
        alen = sizeof(clientaddr);
        if((clientfd = accept(listenfd, (struct sockaddr*)&clientaddr, &alen)) < 0){//接受客户端的连接
            fprintf(stderr, "accept failed!\n");
            exit(0);
        }
        printf("连接成功!!!!");
        visits++;
        sprintf(buf, "This server has been connected %d time\n", visits);//把需要发送的数据放到缓冲区里面
        send(clientfd, buf, strlen(buf), 0);//发送数据到客户端
        closesocket(clientfd);
    }
    return 0;
}
搜索更多相关主题的帖子: exit include port int fprintf 
2018-09-28 20:26
捕鱼儿海
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-9-28
收藏
得分:0 
不知道是不是用visiual c++就可以运行啦,我用的dev。。QAQ
2018-09-28 20:31
no1xijin
Rank: 9Rank: 9Rank: 9
来 自:江西
等 级:贵宾
威 望:20
帖 子:211
专家分:1207
注 册:2015-7-8
收藏
得分:10 
visiual c++   可以编译运行

126邮箱联系方式:no1xijin@126. com
2018-09-29 08:56
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:10 
ld returned 1 exit status
你为什么不肯贴出编译器告诉你的具体错误?比如

#pragma comment(lib,"ws2_32.lib")
warning: ignoring #pragma comment
undefined reference to `_imp__WSAStartup@8'
undefined reference to `_imp__htons@4'
undefined reference to `_imp__socket@12'
undefined reference to `_imp__bind@12'
undefined reference to `_imp__listen@8'
undefined reference to `_imp__accept@12'
undefined reference to `_imp__send@16'
undefined reference to `_imp__closesocket@4'

fprintf(stderr, "bad port number%d\n", argv[1]);
warning: format '%d' expects argument of type 'int', but argument 3 has type 'char *' [-Wformat=]

DWORD error = GetLastError();
warning: unused variable 'error'


2018-09-29 08:56
快速回复:ld returned 1 exit status这个错误怎么解
数据加载中...
 
   



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

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