Created
February 3, 2015 17:25
-
-
Save flouthoc/09dd0a701f9fa5e1a12f to your computer and use it in GitHub Desktop.
A simple Daytime Server in C from TextBook example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
#include <time.h> | |
#define MAXLINE 1024 | |
int main( int argc, char **argv){ | |
int listenfd; | |
int 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(3490); | |
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); | |
listen(listenfd, 10); | |
while(1){ | |
len = sizeof(cliaddr); | |
connfd = accept(listenfd, (struct sockaddr *) &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); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment