主题:[讨论]求助:关于网络编程的小问题
我最近在学计算机网络这门课,有一个试验是用socket编程实现ping功能,我编了一个小程序,编译通过了,但是链接发生错误:
unresolved external symbol __imp__closesocket@4
哪位高手帮小弟指点一下,小弟感谢不尽!!!
附:客户端源代码如下(有兴趣的帮忙看一下):
#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#include <winsock2.h>
#define SERVER_PORT 5555
#define TIME_OUT 2000
static char str_default_servip[] = "0.0.0.0";
int main()
{
char *str_servip=str_default_servip;
cout<<"Please enter the Server IP address:"<<endl;
cin>>str_servip;
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
char buf[512];
int seradd_size=0;
int nbytes;
int rcv_time=0;
int sen_num=0;
int rcv_num=0;
unsigned long ltime = 0;
wVersionRequested = MAKEWORD( 2, 2 );
if ( (WSAStartup( wVersionRequested, &wsaData )) != 0 )
{
printf("could not find a usable WinSock DLL.\n");
return 1;
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket");
exit(1);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family =AF_INET;
servaddr.sin_port =htons(SERVER_PORT);
servaddr.sin_addr.s_addr=inet_addr(str_servip);
seradd_size=sizeof(servaddr); //大小是否与上面的一样
cout<<endl<<"Pinging "<<str_servip<<": "<<endl<<endl;
while(sen_num<=3)
{
nbytes=sendto(sockfd,"hello!",20,0,(struct sockaddr*)&servaddr,sizeof(servaddr));
sen_num++;
ltime=GetTickCount();
if(nbytes<0)
{
perror("sendto error!");
return 1;
}
nbytes=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&servaddr,&seradd_size);
if(nbytes<0)
{
cout<<"destination host unreachable."<<endl;
continue;
}
else
{
rcv_num++;
cout<<"Reply from host: "<<str_servip<<" RTT = "<<GetTickCount()-ltime<<" ms"<<endl;
}
}
cout<<endl;
cout<<"Ping statistics for "<<str_servip<<":"<<endl;
cout<<" Packets: Sent = "<<sen_num<<", Received = "<<rcv_num<<", Lost = "<<sen_num-rcv_num<<endl<<endl;
closesocket(sockfd);
return 1;
}
unresolved external symbol __imp__closesocket@4
哪位高手帮小弟指点一下,小弟感谢不尽!!!
附:客户端源代码如下(有兴趣的帮忙看一下):
#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#include <winsock2.h>
#define SERVER_PORT 5555
#define TIME_OUT 2000
static char str_default_servip[] = "0.0.0.0";
int main()
{
char *str_servip=str_default_servip;
cout<<"Please enter the Server IP address:"<<endl;
cin>>str_servip;
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
char buf[512];
int seradd_size=0;
int nbytes;
int rcv_time=0;
int sen_num=0;
int rcv_num=0;
unsigned long ltime = 0;
wVersionRequested = MAKEWORD( 2, 2 );
if ( (WSAStartup( wVersionRequested, &wsaData )) != 0 )
{
printf("could not find a usable WinSock DLL.\n");
return 1;
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket");
exit(1);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family =AF_INET;
servaddr.sin_port =htons(SERVER_PORT);
servaddr.sin_addr.s_addr=inet_addr(str_servip);
seradd_size=sizeof(servaddr); //大小是否与上面的一样
cout<<endl<<"Pinging "<<str_servip<<": "<<endl<<endl;
while(sen_num<=3)
{
nbytes=sendto(sockfd,"hello!",20,0,(struct sockaddr*)&servaddr,sizeof(servaddr));
sen_num++;
ltime=GetTickCount();
if(nbytes<0)
{
perror("sendto error!");
return 1;
}
nbytes=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&servaddr,&seradd_size);
if(nbytes<0)
{
cout<<"destination host unreachable."<<endl;
continue;
}
else
{
rcv_num++;
cout<<"Reply from host: "<<str_servip<<" RTT = "<<GetTickCount()-ltime<<" ms"<<endl;
}
}
cout<<endl;
cout<<"Ping statistics for "<<str_servip<<":"<<endl;
cout<<" Packets: Sent = "<<sen_num<<", Received = "<<rcv_num<<", Lost = "<<sen_num-rcv_num<<endl<<endl;
closesocket(sockfd);
return 1;
}