IT

timeserv와 timeclnt...

kio467 2014. 12. 14. 18:59

sigh......


뭐가 문제냐...




#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <unistd.h>


#define PORTNUM 9906

#define HOST "155.230.90.111"


main(int argc, char *argv[])

{

        struct sockaddr_in      servadd;

        struct hostent  *hp;

        int    sock_id, messlen;

        char   message[BUFSIZ], *id, recvMsg[BUFSIZ];


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

perror("socket");


        bzero(&servadd, sizeof(servadd));

//      hp = gethostbyname(HOST);

 //     bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);

        servadd.sin_addr.s_addr = inet_addr(HOST);

        servadd.sin_port = htons(PORTNUM);

        servadd.sin_family = AF_INET;

  if(connect(sock_id, (struct sockaddr *) &servadd, sizeof(servadd)) == -1){perror("connect");}


id = argv[1];

if(send(sock_id, id, sizeof(id),0) != sizeof(id)){perror("send");}


        messlen = read(sock_id, message, BUFSIZ);


while(1)

{

fgets(message, BUFSIZ, stdin);

if(!strncmp(message, "quit",4)){break;}


if(send(sock_id, message, sizeof(message), 0) != sizeof(message)) {perror("send");}


if((messlen=recv(sock_id, recvMsg, BUFSIZ, 0)) == -1) {perror("receive");}


recvMsg[messlen] = '\0';

printf("Recieved : %s \n", recvMsg);

}

        close(sock_id);

        return 0;

}




















#include <stdio.h>

#include <stdlib.h>

#include <strings.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

#include <time.h>


#define PORTNUM 9906

#define HOSTLEN 256

#define oops(m) {perror(m); exit(1);}


int main(int ac, char *av[])

{

struct sockaddr_in saddr;

struct sockaddr_in caddr;

struct hostent *hp;

char hostname[HOSTLEN],dirname[BUFSIZ], recvMsg[BUFSIZ], usrId[BUFSIZ];

int sock_id, sock_fd, idLen, recvLen;

FILE *sock_fp;

time_t thetime;


if ((sock_id = socket(PF_INET, SOCK_STREAM, 0)) == -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, 5)) != 0)

oops("listen");


while(1)

{

if((sock_fd=accept(sock_id, NULL, NULL))==-1)

oops("accept");


if((idLen = recv(sock_fd, usrId, BUFSIZ,0))==-1)

oops("receive");


printf("connection from %s\n",usrId);


sock_fp=fdopen(sock_fd,"w"); 


fprintf(sock_fp, "the time here is.. %s", ctime(time(NULL)));


while((recvLen = recv(sock_fd, recvMsg, BUFSIZ, 0)) > 0)

{

// recvMsg[recvLen] = '\0';


if(send(sock_fd, recvMsg, recvLen, 0) != recvLen){ perror("send"); }


printf("%s : %s", usrId, recvMsg);

fwrite(recvMsg, recvLen, 1,sock_fp);

}

   

if(recvLen <= 0) 

{

printf("%s disconnected\n", usrId);

}

fclose(sock_fp);

}

}