日期:2014-05-16 浏览次数:21105 次
typedef struct tagIn_Addr
{
unsigned long in_addr;
}IN_ADDR_T;
typedef struct tagSockaddr
{
unsigned short int sin_family;
unsigned short int sin_port;
IN_ADDR_T sin_addr;
unsigned char sin_zero[8];
}SOCK_ADDR_T;
SOCK_ADDR_T stSockAddr = {0};
char acReadBuff[256] = {0};
int nSinSize = sizeof(SOCK_ADDR_T);
while(1)
{
memset(&stSockAddr, 0x00, nSinSize);
recvfrom(s_nSockFd, acReadBuff, 256, 0, (struct sockaddr *)&stSockAddr, &nSinSize); //s_nSockFd:socket之前创建号的
printf("Get a new message:%s\n", acReadBuff);
printf("IP:%s\n", (char *)inet_ntoa(stSockAddr.sin_addr));
printf("Port:%d\n", htons(stSockAddr.sin_port));
printf("nSinSize = %d\n", nSinSize);
sendto(s_nSockFd, "Serv Recv data", 15, 0, (struct sockaddr *)&stSockAddr, nSinSize);
sleep(1);
}
printf("Port:%d\n", htons(stSockAddr.sin_port));//应该是ntohs()