萌新求助(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;
}
有几个地方不知道该填什么地址,还有上面的几个错误。希望有大佬指点。