实例一:不支持并发,单服务器---单客户端

/*************************************************************************
> File Name: ser01.c
> Author:
> Mail:
> Created Time: 2015年11月22日 星期日 10时22分17秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h> int main()
{
//int socket(int domain, int type, int protocol);
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8001);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr)); listen(sockfd, 128); struct sockaddr cliaddr;
socklen_t clilen;
int connfd;
connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen); char recvbuf[2014] = {0};
int recvcount = 0; while(1){
recvcount = read(connfd, recvbuf,1024);
if (recvcount == 0){
printf("read is over.\n");
printf("the client is over.\n");
exit(0);
}
else if (recvcount == -1){
perror("read error.\n");
exit(EXIT_FAILURE);
}
fputs(recvbuf, stdout);
write(connfd, recvbuf, strlen(recvbuf));
memset(recvbuf, 0, 1024);
} exit(0);
}
/*************************************************************************
> File Name: cli01.c
> Author:
> Mail:
> Created Time: 2015年11月22日 星期日 11时56分05秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h> int main()
{ int sockfd, ret;
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8001);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockfd = socket(AF_INET, SOCK_STREAM, 0); ret = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
if(ret == -1){
perror("bind error.\n");
exit(EXIT_FAILURE);
} char recvbuf[1024] = {0};
char sendbuff[1024] = {0}; while(fgets(sendbuff, 1024, stdin) != NULL){
write(sockfd, sendbuff, strlen(sendbuff));
read(sockfd, recvbuf, 1024);
fputs(recvbuf, stdout);
memset(recvbuf, 0, 1024);
memset(sendbuff, 0, 1024);
} exit(0);
}

server:

client:

实例二:支持多并发,单服务器----多客户端

/*************************************************************************
> File Name: ser02.c
> Author:
> Mail:
> Created Time: 2015年11月22日 星期日 10时22分17秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h> int main()
{
//int socket(int domain, int type, int protocol);
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8001);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr)); listen(sockfd, 128); struct sockaddr_in cliaddr;
socklen_t clilen; int connfd;
char recvbuf[2014] = {0};
int recvcount = 0; while(1){
connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);
if(connfd == -1){
perror("accept error.\n");
exit(EXIT_FAILURE);
}
pid_t pid = fork(); if(pid == 0){
while(1){
recvcount = read(connfd, recvbuf,1024);
if (recvcount == 0){
printf("%s:%d is over.\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
exit(0);
}
else if (recvcount == -1){
perror("read error.\n");
exit(EXIT_FAILURE);
}
printf("%d receive from %s:%d - ", getpid(), inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
fputs(recvbuf, stdout);
write(connfd, recvbuf, strlen(recvbuf));
memset(recvbuf, 0, 1024);
}
}
else if(pid > 0){ }
else if(pid < 0){
perror("fork error.\n");
exit(EXIT_FAILURE);
}
} exit(0);
}
/*************************************************************************
> File Name: cli02.c
> Author:
> Mail:
> Created Time: 2015年11月22日 星期日 11时56分05秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h> int main()
{ int sockfd, ret;
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8001);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockfd = socket(AF_INET, SOCK_STREAM, 0); ret = connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr));
if(ret == -1){
perror("bind error.\n");
exit(EXIT_FAILURE);
} char recvbuf[1024] = {0};
char sendbuff[1024] = {0}; while(fgets(sendbuff, 1024, stdin) != NULL){
write(sockfd, sendbuff, strlen(sendbuff));
read(sockfd, recvbuf, 1024);
printf("receive from %s:%d - ", inet_ntoa(seraddr.sin_addr), ntohs(seraddr.sin_port));
fputs(recvbuf, stdout);
memset(recvbuf, 0, 1024);
memset(sendbuff, 0, 1024);
} exit(0);
}

server:

client1:

client2:

client3:

server(此时三个客户端退出):

linux支持并发的服务器回射程序实例的更多相关文章

  1. unp TCP 客户端服务器回射程序中对SIGCHLD信号的处理

    第五章中,有一个例子模拟客户端并发的终止TCP连接,服务器捕捉并处理SIGCHLD信号并调用waitpid函数防止僵死进程的出现.信号处理函数中核心的一句是: , &statloc, WNOH ...

  2. Socket 入门- 客户端回射程序

    结果输出:------------------------------------------------------客户端:xx@xxxxxx:~/Public/C$ ./postBackCli.o ...

  3. TCP简单回射程序

    一.程序功能 (1)客户从标准输入读入一行文本行,并写给服务器: (2)服务器从网络输入读入这行文本,并回射给客户: (3)客户从网络输入读入这行回射文本,并显示在标准输出上 二.服务器程序 #inc ...

  4. 简单回射程序之处理accept返回EINTR错误的服务器程序版本

    #include <stdio.h> #include <stdlib.h> #include <time.h> #include <errno.h> ...

  5. python实现支持并发、断点续传的Ftp程序

    一.要求 1.用户md5认证 2.支持多用户同时登陆(并发) 3.进入用户的命令行模式,支持cd切换目录,ls查看目录子文件 4.执行命令(ipconfig) 5.传输文件: a.支持断点续传 b.传 ...

  6. 第五十六节,python实现支持并发、断点续传的Ftp程序

    一.要求 1.用户md5认证 2.支持多用户同时登陆(并发) 3.进入用户的命令行模式,支持cd切换目录,ls查看目录子文件 4.执行命令(ipconfig) 5.传输文件: a.支持断点续传 b.传 ...

  7. unix域数据报回射程序(不完整)

    一.服务器程序 int main(int argc, char **argv) { int sockfd; struct sockaddr_un servaddr, cliaddr; sockfd = ...

  8. unix域字节流回射程序

    一.服务端程序 #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <uni ...

  9. 第十篇:基于TCP的一对回射客户/服务器程序及其运行过程分析( 上 )

    前言 本文将讲解一对经典的客户/服务器回射程序,感受网络编程的大致框架( 该程序稍作改装即可演变成各种提供其他服务的程序 ):同时,还将对其运行过程加以分析,观察程序背后协议的执行细节,学习调试网络程 ...

随机推荐

  1. swoole组件----mysql查询,插入数据

    注意!任何swoole函数都应该包含在go(function(){}) 经典查询方法query() go(function (){ $swoole_mysql = new Swoole\Corouti ...

  2. Fibonacci数性质

    Fibonacci数性质 0.\(F_{n-1}+F_{n-2}=F_{n} ,特殊的 F_{0}=1,F_{1}=1\) 上述式子为定义式 1.\(F_{0}+F_{1}+...+F_{n}=F_{ ...

  3. 【题解】P3069 [USACO13JAN]牛的阵容Cow Lineup-C++

    题目传送门 思路这道题目可以通过尺取法来完成 (我才不管什么必须用队列)什么是尺取法呢?顾名思义,像尺子一样取一段,借用挑战书上面的话说,尺取法通常是对数组保存一对下标,即所选取的区间的左右端点,然后 ...

  4. Asia-Jakarata 2018

    目录 Contest Info Solutions Problem A. Edit Distance Problem C. Smart Thief Problem D.Icy Land Problem ...

  5. MongoDB-查询关键字/排序等

    查询关键字 并列查询$and # 条件都成立才可以查询到结果 db.stutent.find({$and:[{name:"小漩涡"},{age:30}]}) 或查询$or # 有一 ...

  6. codeforces#1234F. Yet Another Substring Reverse(子集dp)

    题目链接: https://codeforces.com/contest/1234/problem/F 题意: 给出一个只包含前20个小写字母的字符串,一次操作可以让一段字符颠倒顺序 最多一次这样的操 ...

  7. springboot 出现 org.hibernate.LazyInitializationException: could not initialize proxy

    org.hibernate.LazyInitializationException: could not initialize proxy [com.example.shop.dataobject.U ...

  8. CF1208D

    CF1208D 题意: 给你一个数组,要求支持单点修改和单点查询 解法: 直接线段树搞一搞就没了. CODE: #include<iostream> #include<cstdio& ...

  9. ICEM-extrude功能画圆柱绕流网格【转载】

    转载自:http://blog.csdn.net/lgw19910426/article/details/26401517 首先画网格大体顺序为点-->线-->面-->单元体. 第一 ...

  10. CentOS 安装 MySQL PDO 扩展

    yum install php-pdo_mysql sudo service php-fpm restart