*Send a UDP datagram to a daytime server on some other host,
*read the reply, and print the time and date on the server.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFSIZE 150
#define SERVER_ADDR "202.112.10.60"
#define SERVER_PORT 123
int
main()
{
struct sockaddr_in serv;
char buff[BUFFSIZE];
int sockfd, n;
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
printf("socket error");
bzero((char *) &serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(SERVER_ADDR);
serv.sin_port = htons(SERVER_PORT);
if (sendto(sockfd, buff, BUFFSIZE, 0,
(struct sockaddr *) &serv, sizeof(serv)) != BUFFSIZE)
printf("sendto error");
if((n = recvfrom(sockfd, buff, BUFFSIZE, 0,
(struct sockaddr *) NULL, (socklen_t *) NULL)) < 2)
printf("recvfrom error");
buff[n-2] = 0;
printf("%s\n", buff);
exit(0);
}[/code]
hml1006 于 2011-04-06 10:07:22发表:
没看过
ubuntulover 于 2011-04-03 22:41:31发表:
明天试试去