static void do_echo(FILE *fp, int sockfd)
{
	int maxfdp1, stdineof, n;
	fd_set rset;
	char buf[MAXLINE];
	stdineof = 0;
	int stdoutfd = fileno(stdout);
	for ( ; ; ) {
		FD_ZERO(&rset);
		if (stdineof == 0)
			FD_SET(fileno(fp), &rset);
		FD_SET(sockfd, &rset);
		maxfdp1 = max(fileno(fp), sockfd) + 1;
		myselect(maxfdp1, &rset, NULL, NULL, NULL);
		if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
			if ( (n = myread(sockfd, buf, MAXLINE)) == 0) {
				if (stdineof == 1)
					return; /* normal termination */
				else
					show_err_sys("Server terminated prematurely");
			}
			n = write(stdoutfd, buf, n);
		}
		if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
			if ( (n = myread(fileno(fp), buf, MAXLINE)) == 0) {
				stdineof = 1;
				Shutdown(sockfd, SHUT_WR); /* send FIN */
				FD_CLR(fileno(fp), &rset);
				continue;
			}
			n = write(sockfd, buf, n);
		}
	}
}
Example #2
0
int main() 
{ 
    char *source = "ABCDE"; 
    char result[8] = {0}; 
    myselect(source, 5, result, 0, 0, 3); 
   // char str[] = "ABCDE"; 
    //perm(str, 5, 0); 
    return 0; 
} 
Example #3
0
void myselect(char* str, int len, char* result, int start, int curr, int dest) 
{ 
    if (curr == dest) 
    { 
        //    puts(result); 
        perm(result, 3, 0); 
    } 
    else 
    { 
        int i; 
        for (i = start; curr + len - i >= dest; ++i) 
        { 
            result[curr] = str[i]; 
            myselect(str, len, result, i+1, curr+1, dest); 
        } 
    } 
} 
Example #4
0
int main()
{
	myselect();
	return 0;
}
Example #5
0
void RecvFromOthers()
{
	short serv_port;
	int sockfd;
	int recvbytes;
	struct sockaddr_in my_addr;
//	struct sockaddr_in my_addr1;
//	Link* head = NULL;
//	Link* pt = NULL;
	Link* p = NULL;
	int ret = 0;
//	int number = 0;
//	int tempvalue = 0;

	socklen_t fromlen ;




	serv_port = atoi(SERVERPORT);

	//DEBUG OUTPUT TEST
	#ifdef DEBUG
		printf(">>>		Thread 2 Working	>>>\n");
		fflush(stdout);
	#endif
	//DEBUG OUTPUT TEST

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
		printf(">>>	Socket Failed.\n");
		return ;
	}

//	{
//		int on = 1;
//		setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
//	}

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(serv_port);
	my_addr.sin_addr.s_addr = INADDR_ANY;

//	my_addr1.sin_family = AF_INET;
//	my_addr1.sin_port = 0;
//	my_addr1.sin_addr.s_addr = 0;

	if(-1== bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)))
	{
		printf("Bind Failed/");
		return ;
	}

	fromlen = sizeof(my_addr);
	while(1)
	{
		ret = myselect(100000,sockfd);
		if(ret == -1)
		{
//			number++;
//			if(number >= 12)   //一分钟
//				break;
			continue;
		}

//		number = 0;
		p = malloc(sizeof(Link));
		p->buf[0] = '\0';
		p->next = NULL;

		if (0 >= (recvbytes = recvfrom(sockfd, p->buf, MAXDATASIZE, 0,
		   (struct sockaddr*)&my_addr, &fromlen)))
//		if(0 >= (recvbytes = recv(sockfd,p->buf,MAXDATASIZE,0)))
		{
			printf("recv failed.");
			break;
		}
		p->buf[recvbytes] = '\0';
//		printf("receive from port %d: %s\n", my_addr.sin_port, p->buf);
		sResOutput("-->   Receive From Others :",p -> buf );


//////////////////////////////////////////
/////////////////////////////////////////
		//remember free p
		//TO-DO something about answering the NEEDANSWER JSON ZIP

		AnalysisJson(p->buf);

///*
// * 		//如果需要应答,则直接在这里发送应答包
//		if(AgentAnswer != NULL)
//		{
//			tempvalue = sendto(sockfd, AgentAnswer, strlen(AgentAnswer), 0,(struct sockaddr *)&my_addr, fromlen);
//			//tempvalue = send(sockfd, AgentAnswer, strlen(AgentAnswer), 0);
//			printf("%s\n",AgentAnswer);
//			printf("\n***\n**%d\n",tempvalue);
//			free(AgentAnswer);
//			AgentAnswer = NULL;
//		}
//*/

		free(p);
		p = NULL;
/////////////////////////////////////////
/////////////////////////////////////////
//		if(my_addr1.sin_port == 0)
//		{
//			my_addr1.sin_port = my_addr.sin_port;
//			my_addr1.sin_addr.s_addr = my_addr.sin_addr.s_addr;
//			if(NULL == head) head = p;
//		}
//		else if(my_addr1.sin_port == my_addr.sin_port &&
//			my_addr1.sin_addr.s_addr == my_addr.sin_addr.s_addr)
//		{
//			if(NULL == head) head = p;
//			else
//			{
//				pt = head;
//				while(pt->next != NULL)
//					pt = pt->next;
//				pt->next = p;
//			}
//		}
//		else
//		{
//			while(head != NULL)
//			{
//				pt = head;
//				if (-1 == sendto(sockfd, pt->buf, strlen(pt->buf), 0,
//		   		(struct sockaddr *)&my_addr, fromlen))
//				{
//					perror("send failed1.");
//					break;
//				}
//				head = head->next;
//				free(pt);
//			}
//
//			if (-1 == sendto(sockfd, p->buf, strlen(p->buf), 0,
//		   	(struct sockaddr *)&my_addr1, fromlen))
//			{
//				perror("send failed2.");
//				break;
//			}
//			free(p);
//		}
	}
	if (-1 != sockfd) close(sockfd);

}