进程:
#include <myhead.h>
#define IP "192.168.250.100"
#define PORT 8888
void handler(int signo){
if(signo==SIGCHLD){
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1){
perror("socket error");
return -1;
}
//端口快速启用
int reuse=-1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){
perror("setsockopt error");
return -1;
}
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))==-1){
perror("bind error");
return -1;
}
if(listen(sfd,128)==-1){
perror("sfd error");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t socklen=sizeof(cin);
pid_t pid;
if(signal(SIGCHLD,handler)==SIG_ERR){
perror("siganl error");
return -1;
}
while(1){
int newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
pid=fork();
if(pid>0){
close(newfd);
}else if(pid==0){
close(sfd);
char buf[128];
while(1){
bzero(buf,sizeof(buf));
int rev=recv(newfd,buf,sizeof(buf),0);
if(rev<0){
perror("recv error");
return -1;
}else if(rev==0){
printf("客服端");
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
strcat(buf,"****");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
exit(EXIT_SUCCESS);
}else{
perror("fork error");
return -1;
}
}
close(sfd);
return 0;
}
线程:
#include <myhead.h>
#define IP "192.168.250.100"
#define PORT 8888
typedef struct msg{
int newfd;
struct sockaddr_in sin;
}msg_info;
void *task(void *arg){
int newfd=((struct msg*)arg)->newfd;
struct sockaddr_in sin=((struct msg*)arg)->sin;
char buf[128];
while(1){
bzero(buf,sizeof(buf));
int res=recv(newfd,buf,sizeof(buf),0);
if(res<0){
perror("recv error");
return NULL;
}else if(res==0){
printf("客户端已经下线\n");
return NULL;
}
strcat(buf,"****");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1){
perror("socket error");
return -1;
}
int reuse=-1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){
perror("setsockopt error");
return -1;
}
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))==-1){
perror("bind error");
return -1;
}
if(listen(sfd,128)==-1){
perror("listen error");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t socklen=sizeof(cin);
while(1){
int newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
pthread_t tid;
msg_info info={newfd,cin};
if(pthread_create(&tid,NULL,task,&info)!=0){
perror("pthread_create error");
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
select:
#include <myhead.h>
#define PORT 8888
#define IP "192.168.250.100"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1){
perror("socket error");
return -1;
}
int reuse=-1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){
perror("setsockopt error");
return -1;
}
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))==-1){
perror("bind error");
return -1;
}
if(listen(sfd,128)==-1){
perror("listen error");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t socklen=sizeof(cin);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
char buf[128];
int res=0;
int newfd=-1;
int maxfd=newfd;
struct sockaddr_in save[1024];
while(1){
tempfds=readfds;
res=select(sfd+1,&tempfds,NULL,NULL,NULL);
if(res==-1){
perror("select error");
return -1;
}else if(res==0){
printf("time out\n");
return -1;
}
for(int i=0;i<=maxfd;i++){
if(!FD_ISSET(i,&tempfds)){
continue;
}
if(i==sfd){
newfd=accept(i,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
FD_SET(newfd,&readfds);
if(newfd>maxfd){
maxfd=newfd;
}
save[newfd]=cin;
printf("newfd=%d\n",newfd);
}else if(i==0){
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
for(int j=4;j<=maxfd;j++){
send(j,buf,sizeof(buf),0);
}
}else{
bzero(buf,sizeof(buf));
int rev=recv(i,buf,sizeof(buf),0);
if(rev==0){
printf("客户端已经下线\n");
close(i);
FD_CLR(i,&readfds);
for(int j=0;j<=maxfd;j++){
if(FD_ISSET(j,&readfds)){
maxfd=j;
break;
}
continue;
}
}else if(rev<0){
perror("recv error");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(save[i].sin_addr),ntohs(save[i].sin_port),buf);
strcat(buf,"****");
send(i,buf,sizeof(buf),0);
}
}
}
close(sfd);
return 0;
}