小弟最近编了一个基于udp的简单程序,功能很简单,就是有个客户端一个服务器端,客户端向发送信息,服务器返回信息,客户收到后计算出往返时间。有点类似ping的功能。我是实现了,但是有点小bug,就是我去ping一个未知ip地址时或者那台主机没开时,客户端程序就死在那里。请问要怎样处理?
客户端程序:
#include <stdio.h>
#include <string>
#include <iostream>
#include <winsock2.h>using namespace std;
#pragma comment ( lib, "ws2_32.lib" )#define SERV_PORT        3000
#define CLI_PORT         2090
#define BUF_SIZE  1024int main(int argc, char *argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr,cliaddr;
char *serv_ip=argv[1]; 
char buf[BUF_SIZE];
string sMessage;
int nbytes;
int nSend = 4;
int nRecv = 0;
unsigned long elapstime = 0; wVersionRequested = MAKEWORD( 2, 2 );
if ( (WSAStartup( wVersionRequested, &wsaData )) != 0 )
{
printf("初始化 Windows Socket DLL 失败!\n");
return 1;
} if(argc<2)
{
printf("在命令提示符下输入: ping <server IP address>\n");
exit(0);
} sockfd = socket(AF_INET, SOCK_DGRAM, 0);  // SOCK_DGRAM表示使用UDP, 0 表示下一层为 IP 层
memset(&cliaddr, 0, sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLI_PORT);
cliaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sockfd,(struct sockaddr *)&cliaddr,sizeof(struct sockaddr_in))<0)

printf("邦定插口失败!\n");
exit(2);
} memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr = inet_addr(serv_ip); int nSize = sizeof(servaddr);

while (nSend>0) 
{
nbytes = sendto(sockfd, "你好", 20, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
nSend--;
elapstime = GetTickCount();
if(nbytes < 0) 
{
    printf("数据发送错误\n");
return 1;
}
nbytes = recvfrom(sockfd,buf,BUF_SIZE,0,(struct sockaddr*)&cliaddr,&nSize);
if(nbytes<0)
{
cout<<"request time out"<<endl;
continue;
}
else
{
nRecv++;
cout<<"Reply from: "<<inet_ntoa(cliaddr.sin_addr);
cout<<"     "<<buf<<"    RTT = "<<GetTickCount()-elapstime<<" ms"<<endl;
}

}
closesocket(sockfd);
return 1;
}服务器端程序:
#include <stdio.h>
#include <winsock2.h>
#include <string>
#include <iostream>#pragma comment ( lib, "ws2_32.lib" )
using namespace std;#define SERV_PORT     3000
#define CLIENT_PORT     2090
#define BUF_SIZE 1024int main()
{
printf(" UDP pinger server\n");
WORD wVersionRequested;
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in clientaddr;
char buf[BUF_SIZE];
int nbytes =0;

wVersionRequested = MAKEWORD( 2, 2 );
if ( (WSAStartup( wVersionRequested, &wsaData )) != 0 )
{
printf("could not find a usable WinSock DLL.\n");
return 1;
} sockfd = socket(AF_INET, SOCK_DGRAM, 0);  // SOCK_DGRAM表示使用UDP, 0 表示下一层为 IP 层
memset(&clientaddr, 0, sizeof(clientaddr));
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(SERV_PORT);
clientaddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sockfd , (struct sockaddr*) &clientaddr, sizeof(clientaddr)) < 0) 
{
printf("服务器邦定端口失败\n"); 
exit(1);
}

int nSize = sizeof(clientaddr);
for(;;) 
{
nbytes = recvfrom(sockfd, buf, BUF_SIZE, 0, (struct sockaddr*)&clientaddr, &nSize);
if(nbytes < 0) 
{
printf("服务器接受数据失败\n"); 
continue;
}
printf("Ping from %s : %s\n",inet_ntoa(clientaddr.sin_addr),buf);
Sleep(27);
nbytes = sendto(sockfd, "你好!",20, 0, (struct sockaddr*)&clientaddr, nSize);
if( nbytes < 0 )
{
printf("服务器发送数据失败\n"); 
return 0;
}
}
closesocket(sockfd); return 1;
}