TCP 시간 캡처

12920 단어
1. 서버 프로그램
#include 
#include 
#include 
#include 
#include <string.h>
#include in.h>
#include 
#include 

#define MAXLINE 4096
#define LISTENQ 1024
#define SA struct sockaddr

int main(int argc, char *argv[]) {
    int listenfd, connfd;
    struct sockaddr_in servaddr;
    char buff[MAXLINE];
    time_t ticks;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(9999);

    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    for ( ; ; ) {
        connfd = accept(listenfd, (SA *)NULL, NULL);

        ticks = time(NULL);
        snprintf(buff, sizeof(buff), "%.24s\r
", ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } exit(0); }

2. 고객의 IP 주소와 포트 번호를 표시하는 서버 프로그램
#include 
#include 
#include 
#include 
#include <string.h>
#include in.h>
#include 
#include 
#include 

#define MAXLINE 4096
#define LISTENQ 1024
#define SA struct sockaddr

int main(int argc, char *argv[]) {
    int listenfd, connfd;
    socklen_t len;
    struct sockaddr_in servaddr, cliaddr;
    char buff[MAXLINE];
    time_t ticks;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(9999);

    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    for ( ; ; ) {
        len = sizeof(cliaddr);
        connfd = accept(listenfd, (SA *)&cliaddr, &len);
        printf("connection from %s, port %d
", inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)), ntohs(cliaddr.sin_port)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r
", ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } exit(0); }

3. 동시 서버 프로그램
#include 
#include 
#include 
#include 
#include <string.h>
#include in.h>
#include 
#include 

#define MAXLINE 4096
#define LISTENQ 1024
#define SA struct sockaddr

int main(int argc, char *argv[]) {
    int listenfd, connfd;
    pid_t pid;
    struct sockaddr_in servaddr;
    char buff[MAXLINE];
    time_t ticks;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(9999);

    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    for ( ; ; ) {
        connfd = accept(listenfd, (SA *)NULL, NULL);

        if ( (pid = fork()) == 0) {
            close(listenfd);

            ticks = time(NULL);
            snprintf(buff, sizeof(buff), "%.24s\r
", ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } close(connfd); } exit(0); }

4. 클라이언트 프로그램
#include 
#include 
#include 
#include in.h>
#include <string.h>
#include 
#include 

#define SA struct sockaddr
#define MAXLINE 4096

int main(int argc, char *argv[]) {
    int sockfd, n;
    char recvline[MAXLINE + 1];
    struct sockaddr_in servaddr;
    
    if (argc != 2) {
        printf("usage: ./daytimetcpcli 
"); return 0; } if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 0; } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(9999); if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) { perror("inet_pton"); return 0; } if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) { perror("connect"); } while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; if (fputs(recvline, stdout) == EOF) { perror("fputs"); } } if (n < 0) { perror("read"); } exit(0); }

 
 
전재 대상:https://www.cnblogs.com/soldierback/p/10669743.html

좋은 웹페이지 즐겨찾기