多进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 6666
#define IP "192.168.124.41"
#define errormsg(msg) do{\
fprintf(stderr,"line%d",__LINE__);\
perror(msg);\
}while(0)
void deal_cli_msg(int cfd,struct sockaddr_in cin);
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
//回收僵尸进程
if(signal(SIGCHLD,handler)==SIG_ERR)
{
errormsg(signal);
return -1;
}
//创建流失套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
errormsg("socket");
return -1;
}
printf("流式套接字创建成功 sfd=%d\n",sfd);
//允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
errormsg("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充服务器地址信息结构体,给bing函数用
struct sockaddr_in sin ;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//绑定服务器地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
errormsg("bind");
return -1;
}
printf("绑定服务器地址信息成功\n");
//套接字设置为监听状态
if(listen(sfd,128)<0)
{
errormsg("listen");
return -1;
}
printf("监听状态设置成功\n");
//获取客户端信息
//
pid_t pid;
while(1)
{
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int cfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(cfd<0)
{
errormsg("accept");
return -1;
}
printf("[%s:%d] cfd=%d客户端链接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
pid=fork();
if(0==pid)
{
deal_cli_msg(cfd,cin);
close(sfd);
exit(0);
}
}
if(close(sfd)<0)
{
errormsg("close");
return -1;
}
return 0;
}
void deal_cli_msg(int cfd,struct sockaddr_in cin)
{
char buf="";
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
errormsg("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d] cfd=%d客户端关闭\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
break;
}
printf("[%s:%d] cfd=%d\t%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd,buf);
//发送
strcat(buf,"*_*");
if(send(cfd,buf,sizeof(buf),0)<0)
{
errormsg("send");
return -1;
}
printf("发送成功\n");
}
if(close(cfd)<0)
{
errormsg("close");
return -1;
}
return 0;
}
多线程
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 6666
#define IP "192.168.124.41"
#define errormsg(msg) do{\
fprintf(stderr,"line%d",__LINE__);\
perror(msg);\
}while(0)
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void deal_cli_msg(int cfd,struct sockaddr_in cin);
int main(int argc, const char *argv[])
{
//回收僵尸进程
if(signal(SIGCHLD,handler)==SIG_ERR)
{
errormsg(signal);
return -1;
}
//创建流失套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
errormsg("socket");
return -1;
}
printf("流式套接字创建成功 sfd=%d\n",sfd);
//允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
errormsg("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充服务器地址信息结构体,给bing函数用
struct sockaddr_in sin ;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//绑定服务器地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
errormsg("bind");
return -1;
}
printf("绑定服务器地址信息成功\n");
//套接字设置为监听状态
if(listen(sfd,128)<0)
{
errormsg("listen");
return -1;
}
printf("监听状态设置成功\n");
//获取客户端信息
//
pthread_t tid;
struct msg cliinfo;
while(1)
{
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int cfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(cfd<0)
{
errormsg("accept");
return -1;
}
printf("[%s:%d] cfd=%d客户端链接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
cliinfo.newfd = cfd;
cliinfo.cin = cin;
if(pthread_create(&tid,NULL,deal_cli_msg,(void*)&cliinfo)!=0)
{
break;
}
pthread_detach(tid);
}
if(close(sfd)<0)
{
errormsg("close");
return -1;
}
return 0;
}
void deal_cli_msg(void* arg)
{
int cfd=((struct msg*)arg)->newfd;
struct sockaddr_in cin = ((struct msg*)arg)->cin;
char buf="";
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
errormsg("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d] cfd=%d客户端关闭\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
break;
}
printf("[%s:%d] cfd=%d\t%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd,buf);
//发送
strcat(buf,"*_*");
if(send(cfd,buf,sizeof(buf),0)<0)
{
errormsg("send");
return -1;
}
printf("发送成功\n");
}
if(close(cfd)<0)
{
errormsg("close");
return -1;
}
pthread_exit(NULL);
return 0;
}