#include<th.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 5555
#define IP "192.168.114.54"
void handler(int signum)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
int main(int argc, const char *argv[])
{
if(signal(SIGCHLD,handler) == SIG_ERR)
{
ERR_MSG("signal error");
return -1;
}
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket error");
return -1;
}
printf("socket success\n");
//允许端口被快速复用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt error");
return -1;
}
printf("允许端口被快速复用成功\n");
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 error");
return -1;
}
printf("bind success\n");
//将发送方变成被监听状态
if(listen(sfd,128) < 0)
{
ERR_MSG("listen error");
return -1;
}
printf("listen success\n");
int newfd = -1;
pid_t pid;
struct sockaddr_in cin; //存储发送方的地址信息
socklen_t addrlen = sizeof(cin);
while(1)
{
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept error");
return -1;
}
printf("[%s:%d] newfd = %d\n",\
inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),\
newfd);
pid = fork();
if(pid == 0)
{
close(sfd);
char buf[213] = "";
ssize_t res;
while(1)
{
//接收数据
bzero(buf,sizeof(buf));
if(res = recv(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("recv error");
return -1;
}
else if(res == 0)
{
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 error");
return -1;
}
printf("发送成功\n");
}
close(newfd);
exit(EXIT_SUCCESS);
}
close(newfd);
}
close(sfd);
//关闭文件描述符
if(close(sfd) < 0)
{
ERR_MSG("close error");
return -1;
}
return 0;
}
#include<th.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 7777 //端口号,范围1024~49151
#define IP "192.168.114.54" //本机IP
struct climsg
{
int newfd;
struct sockaddr_in cin;
};
void* task(void * arg);
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d\n",sfd);
//允许端口快速被复用成功
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt error");
return -1;
}
printf("允许端口快速被复用成功\n");
//填充地址信息结构体给bind函数绑定
//真实的地址信息结构体根据地址族指定 AF_INET: man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机IP
//绑定服务器的地址信息 必须绑定 bind
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind error");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态 listen
if(listen(sfd,128) < 0)
{
ERR_MSG("listen error");
return -1;
}
printf("listen success\n");
//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
int newfd = -1;
struct sockaddr_in cin; //存储客户端的地址信息
socklen_t addrlen = sizeof(cin); //真实的地址信息结构体的大小
pthread_t tid;
struct climsg info;
while(1)
{
if((newfd = accept(sfd,(struct sockaddr * )&cin, &addrlen)) < 0)
{
ERR_MSG("accept error");
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,task,(void *)&info) != 0)
{
fprintf(stderr,"pthread_create failed __%d__\n",__LINE__);
return -1;
}
pthread_detach(tid);
}
//关闭文件描述符
close(newfd);
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
void * task(void *arg)
{
int newfd = ((struct climsg*)arg) -> newfd;
struct sockaddr_in cin = ((struct climsg*)arg) -> cin;
char buf[234] = "";
ssize_t res = 0;
while(1)
{
//清空字符串
bzero(buf,sizeof(buf)); //或者用memset
//接收
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv error");
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 error");
break;
}
printf("send success\n");
}
close(newfd);
pthread_exit(NULL);
}