📄 multiplex.c
字号:
/* MPICH-V2 Copyright (C) 2002, 2003 Groupe Cluster et Grid, LRI, Universite de Paris Sud This file is part of MPICH-V2. MPICH-V2 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. MPICH-V2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with MPICH-V2; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: multiplex.c,v 1.1.1.1 2004/01/30 15:49:38 lemarini Exp $*/#include<sys/types.h>#include<sys/socket.h>#include<sys/time.h>#include<netinet/in.h>#include<ctype.h>#include<netdb.h>#include<errno.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include <arpa/inet.h>#include <unistd.h>#define MTU 65536static int uread(int sock,char * buf,int size){ int n,rd=0; while(rd<size) { n=recv(sock, buf+rd, size-rd, 0); if(n<=0) { if((n<0) && ((errno == EINTR) || (errno == EAGAIN))) continue; return n; } rd+=n; } return rd;}typedef struct client { struct sockaddr_in connect_info; int fd; struct client *next;} client; int main(int argc,char **argv){ int soc; struct sockaddr_in server; struct sockaddr_in s_client; client *cl=NULL; int size_client; int taille; char buf[MTU]; int i; struct timeval tv; fd_set readset; int maxfd; client *nc; client *tmp; if (argc<2) { fprintf(stderr,"usage : %s port\n",argv[0]); exit(0); } /* creation de la socket */ if ((soc=socket(AF_INET,SOCK_STREAM,0))<0) { perror("socket"); exit(1); } /* remplissage de la structure de description de connexion */ bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_addr.s_addr=INADDR_ANY; server.sin_port=htons(atoi(argv[1])); /* binding de la socket */ if (bind(soc,(struct sockaddr*) &server,sizeof(server))<0) { perror("bind"); exit(1); } /* passage de la socket en mode listen */ if (listen(soc,32)<0) { perror("listen"); exit(1); } for(;;) { maxfd=0; FD_ZERO(&readset); tmp=cl; while(tmp) { if (tmp->fd>0) FD_SET(tmp->fd,&readset); if (tmp->fd>maxfd) maxfd=tmp->fd; tmp=tmp->next; } FD_SET(soc,&readset); if (soc>maxfd) maxfd=soc; maxfd++; select(maxfd,&readset,NULL,NULL,NULL); if (FD_ISSET(soc,&readset)) { nc=(client *)calloc(1,sizeof(client)); if ((nc->fd=accept(soc,(struct sockaddr *)&s_client,(socklen_t *)&size_client))<0) { fprintf(stderr,"accept : %d\n",errno); perror("accept"); exit(1); } gettimeofday(&tv,NULL); memcpy(&nc->connect_info,&s_client,size_client); printf("[%ld.%ld:%s:E]\n",tv.tv_sec,tv.tv_usec,inet_ntoa(nc->connect_info.sin_addr)); fflush(stdout); if (cl==NULL) cl=nc; else { nc->next=cl; cl=nc; } } tmp=cl; while(tmp) { if ((tmp->fd>0) && (FD_ISSET(tmp->fd,&readset))) { bzero(buf,MTU); if (uread(tmp->fd,buf,4)<=0) { perror("uread1"); gettimeofday(&tv,NULL); printf("[%ld.%ld:%s:D]\n",tv.tv_sec,tv.tv_usec,inet_ntoa(tmp->connect_info.sin_addr)); fflush(stdout); tmp->fd=-1; break; } taille=ntohl(*(int *)buf); if (taille>MTU) { fprintf(stderr,"MTU too little (requested size=%08x)\n",taille); printf("[%ld.%ld:%s:B]\n",tv.tv_sec,tv.tv_usec,inet_ntoa(tmp->connect_info.sin_addr)); fflush(stdout); close(tmp->fd); tmp->fd=-1; break; } bzero(buf,MTU); if (uread(tmp->fd,buf,taille)<=0) { perror("uread2"); gettimeofday(&tv,NULL); printf("[%ld.%ld:%s:B]\n",tv.tv_sec,tv.tv_usec,inet_ntoa(tmp->connect_info.sin_addr)); fflush(stdout); tmp->fd=-1; break; } for(i=0;i<taille;i++) { if (buf[i]=='\n') buf[i]=' '; } printf("[%ld.%ld:%s:M]%s\n",tv.tv_sec,tv.tv_usec,inet_ntoa(tmp->connect_info.sin_addr),buf); fflush(stdout); } tmp=tmp->next; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -