一、TCP多进程并发
1.地址快速重用
先退出服务端,后退出客户端,则服务端会出现以下错误:
地址仍在使用中
解决方法:
/*地址快速重用*/
int flag=1,len= sizeof (int);
if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) {
perror("setsockopt");
exit(1);
}
2.多进程并发
1)fork函数
#include <stdio.h>
#include <wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
pid_t pid = fork();
if(pid < 0){
perror("fork");
exit(0);
}else if(pid == 0){
printf("This is child process.\n");
}else{
printf("This is father process.\n");
wait(NULL);
}
return 0;
}
2)多个客户端
代码演示
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#define BACKLOG 5
void ClinetHandle(int newfd);
int main(int argc, char *argv[])
{
int fd, newfd;
struct sockaddr_in addr, clint_addr;
socklen_t addrlen = sizeof(clint_addr);
pid_t pid;
if(argc < 3){
fprintf(stderr, "%s<addr><port>\n", argv[0]);
exit(0);
}
/*创建套接字*/
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0){
perror("socket");
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons( atoi(argv[2]) );
if ( inet_aton(argv[1], &addr.sin_addr) == 0) {
fprintf(stderr, "Invalid address\n");
exit(EXIT_FAILURE);
}
/*地址快速重用*/
int flag=1,len= sizeof (int);
if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) {
perror("setsockopt");
exit(1);
}
/*绑定通信结构体*/
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1){
perror("bind");
exit(0);
}
/*设置套接字为监听模式*/
if(listen(fd, BACKLOG) == -1){
perror("listen");
exit(0);
}
while(1){
/*接受客户端的连接请求,生成新的用于和客户端通信的套接字*/
newfd = accept(fd, (struct sockaddr *)&clint_addr, &addrlen);
if(newfd < 0){
perror("accept");
exit(0);
}
printf("addr:%s port:%d\n", inet_ntoa(clint_addr.sin_addr), ntohs(clint_addr.sin_port) );
if( (pid = fork() ) < 0){
perror("fork");
exit(0);
}else if(pid == 0){
close(fd);
ClinetHandle(newfd);
exit(0);
}
else
close(newfd);
}
close(fd);
return 0;
}
void ClinetHandle(int newfd){
int ret;
char buf[BUFSIZ] = {};
while(1){
//memset(buf, 0, BUFSIZ);
bzero(buf, BUFSIZ); //初始化
ret = read(newfd, buf, BUFSIZ);
if(ret < 0)
{
perror("read");
exit(0);
}
else if(ret == 0)
break;
else
printf("buf = %s\n", buf);
}
close(newfd);
}
二、僵尸进程处理
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <sys/wait.h>
#define BACKLOG 5
void SigHandle(int sig){
if(sig == SIGCHLD){
printf("client exited\n");
wait(NULL);
}
}
void ClinetHandle(int newfd);
int main(int argc, char *argv[])
{
int fd, newfd;
struct sockaddr_in addr, clint_addr;
socklen_t addrlen = sizeof(clint_addr);
#if 0
struct sigaction act;
act.sa_handler = SigHandle;
act.sa_flags = SA_RESTART;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
#else
signal(SIGCHLD, SigHandle);
#endif
pid_t pid;
if(argc < 3){
fprintf(stderr, "%s<addr><port>\n", argv[0]);
exit(0);
}
/*创建套接字*/
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0){
perror("socket");
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons( atoi(argv[2]) );
if ( inet_aton(argv[1], &addr.sin_addr) == 0) {
fprintf(stderr, "Invalid address\n");
exit(EXIT_FAILURE);
}
/*地址快速重用*/
int flag=1,len= sizeof (int);
if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) {
perror("setsockopt");
exit(1);
}
/*绑定通信结构体*/
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1){
perror("bind");
exit(0);
}
/*设置套接字为监听模式*/
if(listen(fd, BACKLOG) == -1){
perror("listen");
exit(0);
}
while(1){
/*接受客户端的连接请求,生成新的用于和客户端通信的套接字*/
newfd = accept(fd, (struct sockaddr *)&clint_addr, &addrlen);
if(newfd < 0){
perror("accept");
exit(0);
}
printf("addr:%s port:%d\n", inet_ntoa(clint_addr.sin_addr), ntohs(clint_addr.sin_port) );
if( (pid = fork() ) < 0){
perror("fork");
exit(0);
}else if(pid == 0){
close(fd);
ClinetHandle(newfd);
exit(0);
}
else
close(newfd);
}
close(fd);
return 0;
}
void ClinetHandle(int newfd){
int ret;
char buf[BUFSIZ] = {};
while(1){
//memset(buf, 0, BUFSIZ);
bzero(buf, BUFSIZ);
ret = read(newfd, buf, BUFSIZ);
if(ret < 0)
{
perror("read");
exit(0);
}
else if(ret == 0)
break;
else
printf("buf = %s\n", buf);
}
close(newfd);
}
三、TCP并发多线程
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <pthread.h>
#define BACKLOG 5
void *ClinetHandle(void *arg);
int main(int argc, char *argv[])
{
int fd, newfd;
struct sockaddr_in addr, clint_addr;
pthread_t tid;
socklen_t addrlen = sizeof(clint_addr);
if(argc < 3){
fprintf(stderr, "%s<addr><port>\n", argv[0]);
exit(0);
}
/*创建套接字*/
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0){
perror("socket");
exit(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons( atoi(argv[2]) );
if ( inet_aton(argv[1], &addr.sin_addr) == 0) {
fprintf(stderr, "Invalid address\n");
exit(EXIT_FAILURE);
}
/*地址快速重用*/
int flag=1,len= sizeof (int);
if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) {
perror("setsockopt");
exit(1);
}
/*绑定通信结构体*/
if(bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1){
perror("bind");
exit(0);
}
/*设置套接字为监听模式*/
if(listen(fd, BACKLOG) == -1){
perror("listen");
exit(0);
}
while(1){
/*接受客户端的连接请求,生成新的用于和客户端通信的套接字*/
newfd = accept(fd, (struct sockaddr *)&clint_addr, &addrlen);
if(newfd < 0){
perror("accept");
exit(0);
}
printf("addr:%s port:%d\n", inet_ntoa(clint_addr.sin_addr), ntohs(clint_addr.sin_port) );
pthread_create(&tid, NULL, ClinetHandle, &newfd); //创建线程
pthread_detach(tid); //设置为分离属性
}
close(fd);
return 0;
}
void *ClinetHandle(void *arg){
int ret;
char buf[BUFSIZ] = {};
int newfd = *(int *)arg; //传参,强转
while(1){
//memset(buf, 0, BUFSIZ);
bzero(buf, BUFSIZ);
ret = read(newfd, buf, BUFSIZ);
if(ret < 0)
{
perror("read");
exit(0);
}
else if(ret == 0)
break;
else
printf("buf = %s\n", buf);
}
printf("client exited\n");
close(newfd);
return NULL;
}
Makefile
CC=gcc
CFLAGS=-Wall
all:client serverserver:server.c
$(CC) $^ -Wall -o $@ -lpthreadclean:
rm client server