TCP并发服务器
多进程并发服务器
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.31.114" //本机IP:ifconfig查看
typedef void(*sighandler_t)(int);
int rcv_cli(int newfd,struct sockaddr_in cin);
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
int main(int argc, const char *argv[])
{
//捕获17号信号,信号方式回收僵尸进程
sighandler_t s = signal(17,handler);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用设置成功\n");
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定:
//AF_INET --> man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //地址族必须指定为AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序:1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //ubuntu的本机IP:ifconfig可以查看
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd, 10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin ; //接收客户端的IP和端口
socklen_t addrlen = sizeof(cin);
int newfd;
pid_t pid;
while(1)
{
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd = %d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
//到当前位置说明有客户端连接成功
//创建子进程
pid = fork();
if(0 == pid)
{
close(sfd);
//子进程
rcv_cli(newfd,cin);
//交互完毕退出子进程
close(newfd);
exit(0);
}
else if(pid > 0)
{
//负责与客户端链接
close(newfd);
}
else
{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
//子进程交互函数
int rcv_cli(int newfd,struct sockaddr_in cin)
{
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//接收
res = recv(newfd, buf, sizeof(buf), 0);
if(res< 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
fprintf(stderr, "[%s:%d] newfd = %d客户端关闭\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd);
break;
}
printf("[%s:%d] newfd=%d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, buf);
//发送
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
return 0;
}
多线程并发服务器
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.31.114"
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void *callback(void *arg);
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用设置成功\n");
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定:
//AF_INET --> man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //地址族必须指定为AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序:1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //ubuntu的本机IP:ifconfig可以查看
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd, 10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin ; //接收客户端的IP和端口
socklen_t addrlen = sizeof(cin);
int newfd;
pthread_t tid;
struct msg info;
while(1)
{
//主线程只负责链接
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd = %d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
info.newfd = newfd;
info.cin = cin;
//客户端连接成功
//创建分支进程处理交互
if(pthread_create(&tid,NULL,callback,(void*)&info) != 0)
{
ERR_MSG("pthread_create");
return -1;
}
//分离线程,分离后线程自动回收资源
pthread_detach(tid);
}
close(sfd);
return 0;
}
//线程执行体
void *callback(void* arg)
{
struct msg info = *(struct msg*)arg;
int newfd = info.newfd;
struct sockaddr_in cin = info.cin;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//接收
res = recv(newfd, buf, sizeof(buf), 0);
if(res< 0)
{
ERR_MSG("recv");
break;
}
else if(0 == res)
{
fprintf(stderr, "[%s:%d] newfd = %d客户端关闭\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd);
break;
}
printf("[%s:%d] newfd=%d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port), newfd, buf);
//发送
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
break;
}
printf("发送成功\n");
}
close(newfd);
pthread_exit(NULL);
}
域套接字本地通信
流式域套接字—>TCP
服务器实例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//判断要绑定的套接字文件路径是否存在
if(access("./unix",F_OK) == 0)
{
//如果存在,删除套接字文件
if(unlink("./unix") < 0)
{
ERR_MSG("unlink");
return -1;
}
}
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定:
//AF_INET --> man 7 unix
struct sockaddr_un sin;
sin.sun_family = AF_UNIX; //地址族必须指定为AF_UNIX
strcpy(sin.sun_path,"./unix");
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd, 10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_un cin ; //接收客户端的IP和端口
socklen_t addrlen = sizeof(cin);
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
int newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd = %d\n", newfd);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//接收
res = recv(newfd, buf, sizeof(buf), 0);
if(res< 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
fprintf(stderr, "newfd = %d客户端关闭\n", newfd);
break;
}
printf("newfd=%d : %s\n", newfd, buf);
//发送
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
close(newfd);
close(sfd);
return 0;
}
流式域套接字—>TCP
客户端实例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//绑定客户端自身的IP地址和端口---->非必须绑定
//填充要连接的服务器的地址信息结构体
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./unix");
//连接服务器
if(connect(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128] = "";
ssize_t res ;
while(1)
{
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//发送
if(send(sfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
//接收
bzero(buf, sizeof(buf));
res = recv(sfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 ==res)
{
printf("服务器关闭\n");
break;
}
printf(":%s\n", buf);
}
close(sfd);
return 0;
}
报式域套接字—>UDP
服务器实例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#include <sys/ipc.h>
#include <netinet/in.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//判断要绑定的套接字文件路径是否存在
if(access("./unix1",F_OK) == 0)
{
//如果存在,删除套接字文件
if(unlink("./unix1") < 0)
{
ERR_MSG("unlink");
return -1;
}
}
//填充地址信息结构体中,实际结构体是由地址族决定:
//AF_INET --> man 7 unix
struct sockaddr_un sun;
sun.sun_family = AF_UNIX; //地址族必须指定为AF_UNIX
strcpy(sun.sun_path,"./unix1"); //套接字路径名,必须由bind函数生成
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_un cun ; //接收对端的地址信息结构体
socklen_t addrlen = sizeof(cun);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//接收
res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&cun,&addrlen);
if(res< 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s]:%s\n",cun.sun_path,buf);
//发送-->接收到谁的就发给谁
strcat(buf, "*_*");
if(sendto(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&cun,sizeof(cun)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
}
close(sfd);
return 0;
}
报式域套接字—>UDP
客户端实例
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//判断要绑定的套接字文件路径是否存在
if(access("./unix2", F_OK) == 0)
{
//如果存在,则需要将该套接字文件删除
if(unlink("./unix2") < 0)
{
ERR_MSG("unlink");
return -1;
}
}
//绑定客户端自身的地址信息结构体 --->必须绑定,因为系统不会所及分配套接字文件
//填充客户端自身的地址信息结构体;
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path, "./unix2");
if(bind(sfd, (struct sockaddr*)&cun, sizeof(cun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//因为要发送给服务器,所以要先填充服务器的地址信息结构体
//在sendto函数中使用。
//AF_UNIX --> man 7 unix
struct sockaddr_un sun;
sun.sun_family = AF_UNIX; //地址族必须指定为AF_UNIX
strcpy(sun.sun_path, "./unix1"); //套接字路径名
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_un rcv_addr; //对端的IP和端口
socklen_t addrlen = sizeof(rcv_addr);
while(1)
{
//发送数据包-->从终端获取
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//要发送给谁,则地址信息结构体填充的就是谁的IP和端口
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
//接收数据包
bzero(buf, sizeof(buf));
//res = recvfrom(sfd, buf, sizeof(buf), 0, NULL, NULL);
//rcv_addr中会存储数据包是从哪里发送过来的
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcv_addr, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s]:%s\n", rcv_addr.sun_path, buf);
}
close(sfd);
return 0;
}