读取数据后,程序停在这里,请诸位大侠帮忙解决(在线等待)
#include "stdafx.h"
#include "winsock.h"
#pragma comment(lib, "wsock32")
#include <string.h>
#ifdef WIN32
bool wasInited = false;
void WSAInit()
{
if (wasInited) return;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
wasInited = true;
}
#else
void WSAInit(){}
#endif
int main()
{
WSAInit();
char *buf = new char[256];
struct hostent *hp;
struct sockaddr_in sa;
int s, i,n = sizeof(struct sockaddr), on = 1, len = strlen(buf);
if ( (s = socket(AF_INET,SOCK_DGRAM,0)) < 0 ) return(1);
if ( setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof(on)) != 0)
return(1);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_BROADCAST);
sa.sin_port = htons(8000);
sendto(s, buf, len, 0, (const struct sockaddr *)&sa, n);
while(1)
{
if ((i = recvfrom(s, buf, 256, 0, (struct sockaddr *)&sa, &n)) == -1)
break;
//读取数据后,程序停在这里,请诸位大侠帮忙解决?
buf[i] = (char)0;
hp = gethostbyaddr((const char *)&sa.sin_addr, 4, AF_INET);
printf("%s:%s\n",hp->h_name ,buf);
}
return 0;
}