コード例 #1
0
void * listener(void * args) {
	int max,i,j, nsfd, usfd = -1; char buf[BUFSIZE];
	printf("Ticket Counter Started !\n"); struct timeval tv;
	fd_set rfds;
	while(1) {
		FD_ZERO(&rfds);
		max = -1;
		for(j=0; j<MAXC; j++) {
			FD_SET(sfds[j], &rfds);
			if(max < sfds[j])
				max = sfds[j];
		}
		tv.tv_sec = 1; tv.tv_usec = 0;
		int res = select(max+1, &rfds, NULL, NULL, &tv);
		if(res < 0) {
			printf("select() ");
			continue;
		}
		for(i=0; i<MAXC; i++) {
			if(FD_ISSET(sfds[i], &rfds)){
				nsfd = tcp_accept(sfds[i]);
				if(seatfull) {
					printf("seatfull\n");
					write(nsfd, "-1", 2);
					close(nsfd);
				} else {
					write(nsfd, "Which Multiplex (1,2,3) ? ", 28);
					read(nsfd, buf, BUFSIZE);
					int id = atoi(buf);
					printf("Requested for Multiplex %d\n", id);
					sprintf(buf, "%d", tkt_no++);
					write(nsfd, buf, strlen(buf));
					if(usfd < 0) {
						strcpy(buf, "/tmp/sockpath1");
						usfd = init_sockconnect(buf);
						if(usfd < 0) {
							perror("init_sockconnect() ");
						}
					}
					if(usfd > 0) {
						if(send_fd(usfd, nsfd) < 0) {
							perror("send_fd() ");
						}
					}
				}
			}
		}
	}
}
コード例 #2
0
int main(int argc, char const *argv[]) {
	pthread_t pid;
	pthread_mutex_init(&lock, NULL);
	int usfd, nusfd, sfd, n, _tmp_usfd = -1; char buf[BUFSIZE];
	usfd = init_sockbind("/tmp/sockpath1");
	if(usfd < 0) {
		perror("init_sockbind() ");
		exit(1);
	}
	pthread_create(&pid, NULL, &runner, NULL);
	nusfd = sock_accept(usfd);
	if(nusfd < 0) {
		perror("sock_accept() ");
		exit(1);
	}
	while(1) {
		int nsfd = recv_fd(nusfd);
		if(nsfd < 0) {
			perror("recv_fd() ");
			continue;
		}
		if(nsfd_cnt == MAXF) {
			if(_tmp_usfd < 0) {
				strcpy(buf, "/tmp/sockpath2");
				_tmp_usfd = init_sockconnect(buf);
				if(_tmp_usfd < 0) {
					perror("init_sockconnect() ");
				}
			}
			if(_tmp_usfd > 0) {
				if(send_fd(_tmp_usfd, nsfd) < 0) {
					perror("send_fd() ");
				}
			}
			printf("client forwarded !\n");
			continue;
		}
		// pthread_mutex_lock(&lock);
		write(nsfd, "C Multiplex", 11);
		nsfds[nsfd_cnt++] = nsfd;
		// pthread_mutex_unlock(&lock);
		printf("client added !\n");
	}
	return 0;
}
コード例 #3
0
ファイル: service.c プロジェクト: acemaster/cs302-CN
int main(int argc,char **argv)
{
	if(argc < 2) {
		printf("Usage: ./service <temperory_sock_path>\n");
		exit(1);
	}
	int usfd = init_sockconnect(argv[1]);
	if(usfd < 0){
		printf("sock() error\n");
		exit(1);
	}
	int fd,n; char buf[256];
	printf("Process 2 Started !!\n");
	int i=0;
	while(1) 
	{
		fd = recvfd(usfd);
		if(fd < 0) {
			perror("recv_fd() ");
			exit(1);
		}
		int pid=fork();
		if(pid == 0)
		{
			while(1)
			{
				bzero(buf,sizeof(buf));
				n=read(fd,buf,100);
				if(n > 0)
				{
					printf("Client:%d %s\n",i,buf);
				}
			}
		}
		else
		{
			close(fd);
			i++;
		}
	}
	return 0;
}