哪位大侠在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