一、服务器程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#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
", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
exit(0);
}
二、显示客户IP地址和端口号的服务器程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#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
", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
exit(0);
}
三、并发服务器程序
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#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
", ctime(&ticks));
write(connfd, buff, strlen(buff));
close(connfd);
}
close(connfd);
}
exit(0);
}
四、客户端程序
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#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 <IPaddress>
");
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);
}