多进程服务器
#include <myhead.h>
#define SERPORT 8888
#define SERIP "192.168.0.150"
#define BACKBLOG 10
typedef struct {
int newfd;//存储新的文件描述符
struct sockaddr_in cin;//存储客户端信息
}S;
void *fun1(void *s){
S MSG = *(S*)s;
int newfd = MSG.newfd;
struct sockaddr_in cin = MSG.cin;
char buff[1024];
while(1){
memset(buff,0,sizeof(buff));
int len = recv(newfd,buff,sizeof(buff),0);
printf("收到%s:%d的消息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buff);
if(len == 0){
printf("客户端退出\n");
break;
}
strcat(buff,"mhy");
send(newfd,buff,sizeof(buff),0);
printf("发送成功\n");
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//1.创建套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd == -1){
perror("socket");
return -1;
}
//端口号复用
int a = 8;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&a,sizeof(a))==-1){
perror("setsockopt");
return -1;
}
printf("端口号复用成功\n");
//2.绑定
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_port = htons(SERPORT),
.sin_addr.s_addr = inet_addr(SERIP)
};
if(bind(oldfd,(struct sockaddr*)&sin,sizeof(sin))==-1){
perror("bind");
return -1;
}
//3.监听
if(listen(oldfd,BACKBLOG)==-1){
perror("lieten");
return -1;
}
printf("监听成功\n");
struct sockaddr_in cin;
int cinlen = sizeof(cin);
S MSG;
while(1){
//accept接收客户端请求
int newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);
if(newfd == -1){
perror("accept");
return -1;
}
printf("%s:%d发来请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
MSG.newfd = newfd;
MSG.cin = cin;
//创建子线程与客户端通信
pthread_t tid;
if(pthread_create(&tid,NULL,fun1,&MSG)!=0){
perror("ptc");
return -1;
}
//回收子线程资源
pthread_detach(tid);
//关闭新的描述符
//close(newfd);
}
//关闭旧的文件描述符
close(oldfd);
return 0;
}
多线程服务器
#include <myhead.h>
#define SERPORT 8888
#define SERIP "192.168.0.150"
#define BACKBLOG 10
typedef struct {
int newfd;//存储新的文件描述符
struct sockaddr_in cin;//存储客户端信息
}S;
void *fun1(void *s){
S MSG = *(S*)s;
int newfd = MSG.newfd;
struct sockaddr_in cin = MSG.cin;
char buff[1024];
while(1){
memset(buff,0,sizeof(buff));
int len = recv(newfd,buff,sizeof(buff),0);
printf("收到%s:%d的消息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buff);
if(len == 0){
printf("客户端退出\n");
break;
}
strcat(buff,"mhy");
send(newfd,buff,sizeof(buff),0);
printf("发送成功\n");
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//1.创建套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd == -1){
perror("socket");
return -1;
}
//端口号复用
int a = 8;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&a,sizeof(a))==-1){
perror("setsockopt");
return -1;
}
printf("端口号复用成功\n");
//2.绑定
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_port = htons(SERPORT),
.sin_addr.s_addr = inet_addr(SERIP)
};
if(bind(oldfd,(struct sockaddr*)&sin,sizeof(sin))==-1){
perror("bind");
return -1;
}
//3.监听
if(listen(oldfd,BACKBLOG)==-1){
perror("lieten");
return -1;
}
printf("监听成功\n");
struct sockaddr_in cin;
int cinlen = sizeof(cin);
S MSG;
while(1){
//accept接收客户端请求
int newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);
if(newfd == -1){
perror("accept");
return -1;
}
printf("%s:%d发来请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
MSG.newfd = newfd;
MSG.cin = cin;
//创建子线程与客户端通信
pthread_t tid;
if(pthread_create(&tid,NULL,fun1,&MSG)!=0){
perror("ptc");
return -1;
}
//回收子线程资源
pthread_detach(tid);
//关闭新的描述符
//close(newfd);
}
//关闭旧的文件描述符
close(oldfd);
return 0;
}