| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1294 人关注过本帖
标题:[求助]eclipse下c++编的socket
取消只看楼主 加入收藏
beerboy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-5-20
收藏
 问题点数:0 回复次数:1 
[求助]eclipse下c++编的socket

哪位大侠在eclipse下编过c++,我编的一个socket传输,但是出错,不太理解。请指点一下,谢谢!!!
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <winsock2.h>
#include <winsock.h>
#define SERVPORT 3333 /*服务器监听端口号 */
#define BACKLOG 10 /* 最大同时连接请求数 */
int main()
{
int sockfd,client_fd,sin_size; /*sock_fd:监听socket;client_fd:数据传输socket */
struct sockaddr_in my_addr; /* 本机地址信息 */
struct sockaddr_in remote_addr; /* 客户端地址信息 */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket创建出错!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero),8,0);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind出错!");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen出错!");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr,&sin_size)) == -1)
{
perror("accept出错");
continue;
}
printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));

if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1)
{ perror("send出错!");
closesocket(client_fd);
exit(0);
}
closesocket(client_fd);
}

}

出错的信息:
make -k all
g++ server.cpp -g -o run
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0xae): In function `main':
e:/eclipse/workspace/wsock/server.cpp:14: undefined reference to `socket@12'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0xe9):e:/eclipse/workspace/wsock/server.cpp:20: undefined reference to `htons@4'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x11e):e:/eclipse/workspace/wsock/server.cpp:23: undefined reference to `bind@12'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x14d):e:/eclipse/workspace/wsock/server.cpp:28: undefined reference to `listen@8'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x18a):e:/eclipse/workspace/wsock/server.cpp:36: undefined reference to `accept@12'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x1b9):e:/eclipse/workspace/wsock/server.cpp:41: undefined reference to `inet_ntoa@4'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x1db):e:/eclipse/workspace/wsock/server.cpp:43: undefined reference to `send@16'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x1fb):e:/eclipse/workspace/wsock/server.cpp:45: undefined reference to `closesocket@4'
C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp/cccDaaaa.o(.text+0x213):e:/eclipse/workspace/wsock/server.cpp:48: undefined reference to `closesocket@4'
make: *** [all] Error 1

搜索更多相关主题的帖子: eclipse socket 数据传输 include 服务器 
2006-05-20 14:58
beerboy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-5-20
收藏
得分:0 
上面的是服务器的代码
2006-05-20 15:05
快速回复:[求助]eclipse下c++编的socket
数据加载中...
 
   



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

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