进程
#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 <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
//打印错误信息
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
void forconun(int newfd,struct sockaddr_in cin);
#define port 8887
#define ip "192.168.31.229"
typedef void (*sighandler_t)(int);
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;
}
printf("create socket success\n");
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充服务器地址结构体
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("bind success\n");
//将套接字设置监听
if(listen(sfd,10)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;//定义客户端结构体
socklen_t addrlen = sizeof(cin);
pid_t pid=0;
int newfd=0;
while(1)
{
//父进程获取nwefd
newfd =accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
//打印从网络转换回来的ip,port和新的描述符
printf("[ip:%s port:%d] newfd=%d\n",inet_ntoa(\
cin.sin_addr),ntohs(cin.sin_port),newfd);
pid=fork();
//子进程用来交互
if(0==pid)
{
close(sfd);//不需要原套接字
forconun(newfd,cin);
exit(0);
}
close(newfd);//父进程不需要newfd,关掉之后每次连接newfd都是4
}
return 0;
}
void forconun(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;
}
else if(0==res)
{
printf("[ip:%s port:%d]newfd=%d客户端退出\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),\
newfd);
break;
}
printf("[ip:%s port:%d]newfd=%d 说:%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),\
newfd,buf);
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return;
}
printf("send success\n");
}
}
线程
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
printf("line = %d\n", __LINE__);\
perror(msg); \
}while(0)
#define PORT 8887
#define IP "192.168.31.229" //ifconfig查找到本机IP
//需要传入到分支线程的数据结构体
typedef struct climsg
{
struct sockaddr_in cin;
int newfd;
}climsg;
void* forconun(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;
}
printf("socket create success\n");
//允许端口快速重用
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
//绑定ip和端口
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)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//listen 设置被动监听状态
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;
pthread_t tid;
climsg info;
while(1)
{
//主线程用来连接
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[ip:%s port:%d] newfd=%d\n",inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),newfd);
//
info.cin = cin;
info.newfd = newfd;
//连接成功后,创建分支线程做交互
if(pthread_create(&tid,NULL,forconun,(void*)&info)<0)
{
ERR_MSG("pthread_create");
return -1;
}
}
close(sfd);
return 0;
}
void* forconun(void* arg)
{
//线程分离
pthread_detach(pthread_self());
climsg info = *(climsg*)arg;
struct sockaddr_in cin = info.cin;
int newfd = info.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");
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] fd=%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);
}