1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include "unp.h" #include <time.h>
int main(int argc, char **argv) { int listenfd, connfd; socklen_t len; struct sockaddr_in servaddr, cliaddr; char buff[MAXLINE]; time_t ticks; char buf[MAXLINE];
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
len = sizeof(servaddr); getsockname(listenfd, (SA *)&servaddr, &len); printf("Local addr: %s\n", Sock_ntop((SA *) &servaddr, len));
Listen(listenfd, LISTENQ);
len = sizeof(servaddr); getsockname(listenfd, (SA *)&servaddr, &len); printf("Local addr: %s\n", Sock_ntop((SA *) &servaddr, len));
for ( ; ; ) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &len); printf("connection from %s, port %d\n", Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port));
ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff));
Close(connfd); } }
|