#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 <signal.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
typedef void (*sighandler_t)(int);
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int deal_cli_msg(int newfd,struct sockaddr_in cin);
#define PORT 7777
#define IP "192.168.8.171"
int main(int argc, const char *argv[])
{
//捕获17号信号,回收僵尸进程
sighandler_t s = signal(17,handler);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
printf("捕获成功\n");
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//如果不加这段代码,则服务器异常退出后,会导致端口号在30s~3min内释放不出来
//当程序结束后,想要让其他进程能够快速复用端口号,就可以使用下面的代码
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充地址信息结构体,真是的地址信息结构体根据地址族制定
//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); //本机IP,ifconfig查看;
//注意,切换网络环境后,IP地址是会改变的
//将地址信息结构体绑定到套接字上
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
//将套接字设置为被动监听状态
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
struct sockaddr_in cin; //存储连接成功的客户端信息
socklen_t addrlen = sizeof(cin);
int newfd = 0;
pid_t pid = 0;
while(1)
{
//accept从队列头中获取一个客户端信息
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(pid > 0)
{
close(newfd);
}
else if(0 == pid)
{
close(sfd);
deal_cli_msg(newfd,cin);
close(newfd);
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
}
return 0;
}
int deal_cli_msg(int newfd,struct sockaddr_in cin)
{
ssize_t res = 0;
char buf[128] = "";
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("sen");
return -1;
}
printf("send success\n");
}
}
#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, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
void* deal_cli_msg(void* arg);
#define PORT 7777
#define IP "192.168.8.171"
struct msg
{
int newfd;
struct sockaddr_in cin;
};
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//如果不加这段代码,则服务器异常退出后,会导致端口号在30s~3min内释放不出来
//当程序结束后,想要让其他进程能够快速复用端口号,就可以使用下面的代码
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充地址信息结构体,真是的地址信息结构体根据地址族制定
//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); //本机IP,ifconfig查看;
//注意,切换网络环境后,IP地址是会改变的
//将地址信息结构体绑定到套接字上
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
//将套接字设置为被动监听状态
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
struct sockaddr_in cin; //存储连接成功的客户端信息
socklen_t addrlen = sizeof(cin);
int newfd = 0;
pthread_t tid;
struct msg info;
while(1)
{
//accept从队列头中获取一个客户端信息
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,deal_cli_msg,(void*)&info)<0)
{
ERR_MSG("pthread_create");
return -1;
}
// pthread_detach(tid);
}
close(sfd);
return 0;
}
void* deal_cli_msg(void* arg)
{
pthread_detach(pthread_self());//分离线程
int newfd = ((struct msg*)arg)->newfd;
struct sockaddr_in cin = ((struct msg*)arg)->cin;
ssize_t res = 0;
char buf[128] = "";
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("sen");
break;
}
printf("send success\n");
}
close(newfd);
pthread_exit(NULL);
}