Ubuntu下c语言实现并发服务器简单socket编程实例
首先测试三台机子的网络互联,在这里测试通过。在Ubuntu下ping是不停的ping,和Windows下有所不同,可以停止Terminal来结束。
一、客户端运行的代码
daytimetcpcli.c
#include "unp.h"
int
main(int argc, char **argv)
{
int sockfd, n, counter = 0;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: a.out
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(9000); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");
while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
counter++;
recvline[n] = 0; /* null terminate */
if (fputs(recvline, stdout) == EOF)
err_sys("fputs error");
}
if (n < 0)
err_sys("read error");
printf("counter = %d/n", counter);
exit(0);
}
二、服务器运行的代码
并行服务器代码:time_server.c
include "unp.h"
void
gettimeproc(int sockfd)
{
int i,j;
time_t ticks;
char buff[MAXLINE];
char szAddr[INET_ADDRSTRLEN];
socklen_t clilen;
struct sockaddr_in cliaddr;
memset(buff, 0, MAXLINE);
clilen = sizeof(cliaddr);
if(getsockname(sockfd, (SA *)&cliaddr, &clilen) == -1)
{
Close(sockfd);
return;
}
inet_ntop(AF_INET, &(cliaddr.sin_addr), szAddr, INET_ADDRSTRLEN);
printf("%s /r/n", szAddr);
ticks = time(NULL);
memset(buff, 0, MAXLINE);
snprintf(buff, sizeof(buff), "%.24s/r/n", ctime(&ticks));
Write(sockfd, &buff, strlen(buff));
for(i = 65535; i > 0; i--)
{
for(j = 65535; j > 0; j--)
{
}
}
Close(sockfd);
}
int
main(int argc, char **argv)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
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(9000);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);
for ( ; ; ) {
clilen = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
if ( (childpid = Fork()) == 0) { /* child process */
Close(listenfd); /* close listening socket */
gettimeproc(connfd); /* process the request */
exit(0);
}
Close(connfd); /* parent closes connected socket */
}
}
三、运行程序
先在服务器运行time_server.c(用GCC命令编译生成,不用make);再用客户机client 1和客户机client 2运行daytimetcpcli.c程序,用make来编译生成。
A.运行服务器结果如下:
B.客户机client 1结果如下:
C.客户机client 2结果如下:
这说明两台客户机可以同时访问服务器,实现了并发服务器的功能。