多进程
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<signal.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 3333
#define IP "192.168.8.187"
typedef void(*sighandler_t)(int);
int deal_cli_inter(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;
}
//创建流式套接字--》TCP
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("sfd=%d\n",sfd);
//允许端口快速被复用
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
ERR_MSG("setsockopt");
return -1;
}
printf("setsockopt success\n");
//填充地址信息结构体,真实地址信息结构体在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); //本机IP
//将IP地址和端口号绑定到指定套接字中
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("bind success__%d__\n",__LINE__);
//将套接字设置为被动监听状态,只负责监听是否有客户端连接成功
if(listen(sfd,10)<0){
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin; //存储客户端地址信息
socklen_t addrlen = sizeof(cin);
int newfd =0;
int cpid =0;
//功能:阻塞函数,阻塞等待客户端连接成功
//当客户端连接成功后,会从已完成连接的队列头中获取一个客户端信息
//并生成一个新的文件描述符
//注意:新生成的文件描述符是用于通信的文件描述符
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);
//创建一个子进程,负责交互
cpid = fork();
if(0==cpid){ //子进程
//关闭套接字文件描述符
close(sfd); //sfd对子进程没用
deal_cli_inter(newfd,cin); //与客户端交互代码
exit(0); //退出子进程
}
else if(cpid>0){ //父进程
close(newfd);
}
//关闭新的文件描述符
else{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
int deal_cli_inter(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){
printf("[%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("send success\n");
}
close(newfd);
return 0;
}
多线程
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 3333
#define IP "192.168.8.187"
struct info
{
int newfd;
struct sockaddr_in cin;
};
void* deal_cli_inter(void* arg);
int main(int argc, const char *argv[])
{
//创建刘式套接字--》TCP
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("sfd = %d\n",sfd);
//允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
ERR_MSG("setsockopt");
return -1;
}
printf("setsockopt success\n");
//填充信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//将IP地址和端口号绑定到指定套接字中
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = 0;
pthread_t tid;
struct info cliMsg;
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);
cliMsg.newfd = newfd;
cliMsg.cin = cin;
if(pthread_create(&tid,NULL,deal_cli_inter,&cliMsg)!=0){
fprintf(stderr,"line:%d pthread_create failed\n",__LINE__);
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
void* deal_cli_inter(void* arg)
{
int newfd = ((struct info*)arg)->newfd;
struct sockaddr_in cin = ((struct info*)arg)->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){
printf("[%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("send success\n");
}
close(newfd);
pthread_exit(NULL);
}