使用tcp_connect和tcp_listen的时间获取函数

来源:互联网 发布:淘宝店铺装修素材 编辑:程序博客网 时间:2024/06/06 00:00

tcp_connect.c 函数源码:(lib/tcp_connect.c)

/* include tcp_connect */

#include "unp.h"

int tcp_connect(const char *host, const char *serv)

{

intsockfd, n;

struct addrinfohints, *res, *ressave;

bzero(&hints,sizeof(struct addrinfo));

hints.ai_family = AF_UNSPEC;

hints.ai_socktype = SOCK_STREAM;


if ( (n = getaddrinfo(host, serv, &hints, &res)) !=0)

err_quit("tcp_connect error for %s, %s: %s",

host, serv, gai_strerror(n));

ressave = res;

do {

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (sockfd <0)

continue;/* ignore this one */

if (connect(sockfd, res->ai_addr, res->ai_addrlen) ==0)

break;/* success */

Close(sockfd);/* ignore this one */

}while ( (res = res->ai_next) !=NULL);


if (res ==NULL)/* errno set from final connect() */

err_sys("tcp_connect error for %s, %s", host, serv);

freeaddrinfo(ressave);

return(sockfd);

}

/* end tcp_connect */


/*

 * We place the wrapper function here, not in wraplib.c, because some

 * XTI programs need to include wraplib.c, and it also defines

 * a Tcp_connect() function.

 */

int Tcp_connect(const char *host, const char *serv)

{

return(tcp_connect(host, serv));

}



daytimetcpcli.c客户端代码:(names/daytimetcpcli.c)

#include "unp.h"

int main(int argc, char **argv)

{

intsockfd, n;

charrecvline[MAXLINE +1];

socklen_tlen;

struct sockaddr_storagess;


if (argc !=3)

err_quit("usage: daytimetcpcli <hostname/IPaddress> <service/port#>");


sockfd = Tcp_connect(argv[1], argv[2]);


len =sizeof(ss);

Getpeername(sockfd, (SA *)&ss, &len);

printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len));


while ( (n = Read(sockfd, recvline, MAXLINE)) >0) {

recvline[n] = 0;/* null terminate */

Fputs(recvline, stdout);

}

exit(0);

}



tcp_listen.c函数代码:(lib/tcp_listen.c)

/* include tcp_listen */

#include "unp.h"

int tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)

{

intlistenfd, n;

constinton = 1;

struct addrinfohints, *res, *ressave;


bzero(&hints,sizeof(struct addrinfo));

hints.ai_flags = AI_PASSIVE;

hints.ai_family = AF_UNSPEC;

hints.ai_socktype = SOCK_STREAM;


if ( (n = getaddrinfo(host, serv, &hints, &res)) !=0)

err_quit("tcp_listen error for %s, %s: %s",

host, serv, gai_strerror(n));

ressave = res;

do {

listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (listenfd <0)

continue;/* error, try next one */

Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on));

if (bind(listenfd, res->ai_addr, res->ai_addrlen) ==0)

break;/* success */

Close(listenfd);/* bind error, close and try next one */

}while ( (res = res->ai_next) !=NULL);


if (res ==NULL)/* errno from final socket() or bind() */

err_sys("tcp_listen error for %s, %s", host, serv);

Listen(listenfd, LISTENQ);

if (addrlenp)

*addrlenp = res->ai_addrlen;/* return size of protocol address */

freeaddrinfo(ressave);

return(listenfd);

}

/* end tcp_listen */


/*

 * We place the wrapper function here, not in wraplib.c, because some

 * XTI programs need to include wraplib.c, and it also defines

 * a Tcp_listen() function.

 */

int Tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)

{

return(tcp_listen(host, serv, addrlenp));

}



daytimetcpsrv1.c源码:(names/daytimetcpsrv1.c)

#include "unp.h"

#include <time.h>

int main(int argc, char **argv)

{

intlistenfd, connfd;

socklen_tlen;

charbuff[MAXLINE];

time_tticks;

struct sockaddr_storagecliaddr;


if (argc !=2)

err_quit("usage: daytimetcpsrv1 <service or port#>");


listenfd = Tcp_listen(NULL, argv[1],NULL);


for ( ; ; ) {

len =sizeof(cliaddr);

connfd = Accept(listenfd, (SA *)&cliaddr, &len);

printf("connection from %s\n", Sock_ntop((SA *)&cliaddr, len));


ticks = time(NULL);

snprintf(buff,sizeof(buff),"%.24s\r\n", ctime(&ticks));

Write(connfd, buff, strlen(buff));


Close(connfd);

}

}



运行效果:
服务器:

➜  names ./daytimetcpsrv1 1399

connection from 127.0.0.1:64225


客户端:

➜  names ./daytimetcpcli 127.0.0.1 1399

connected to 127.0.0.1

Tue May  5 09:43:43 2015





0 0