본문 바로가기

IT

rlsd.c

// rlsd 할 때 ddemon 이랍니다. 


///////////////////아ㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏㅏ 코딩 덜 했네요............

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <strings.h>

#define PORTNUM 5084
#define HOSTLEN 256
#define oops(m) {perror(m); exit(1);}

int main(int ac, char *av[])
{
    struct sockaddr_in saddr;
    struct hostent *hp;
    char hostname[HOSTLEN];
    int sock_id, sock_fd;
    FILE *sock_fp;
    char *ctime();
    time_t thetime;

    sock_id = socket(PF_INET, SOCK_STREAM, 0);
    if (sock_id == -1)
        oops("socket");

    bzero((void*)&saddr, sizeof(saddr));

    gethostname(hostname, HOSTLEN);
    hp = gethostbyname(hostname);

    bcopy((void*)hp->h_addr, (void*)&saddr.sin_addr, hp->h_length);
    saddr.sin_port = htons(PORTNUM);
    saddr.sin_family = AF_INET;

    if(bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr))!=0)
        oops("bind");

    if(listen(sock_id, 1) != 0)
        oops("listen");

    while(1)
    {
        sock_fd=accept(sock_id, NULL, NULL);
        printf("wow i got a call\n");
        if(sock_fd==-1)
            oops("accept");

        sock_fp = fdopen(sock_fd,"w");
        if(sock_fp == NULL)
            oops("fdopen");

        thetime = time(NULL);

        fprintf(sock_fp, "the time here is.. ");
        fprintf(sock_fp, " %s", ctime(&thetime));
        fclose(sock_fp);
    }
}

'IT' 카테고리의 다른 글

socklib.c  (0) 2014.11.23
timeserv.c  (0) 2014.11.23
rls.c  (0) 2014.11.23
timeclnt.c  (0) 2014.11.20
timeserv.c  (0) 2014.11.19