Esempio n. 1
0
int
main(int argc, char **argv)
{
	int					listenfd, listenfd2, connfd, udpfd, nready, maxfdp1;
	char				mesg[MAXLINE];
	pid_t				childpid;
	fd_set				rset;
	ssize_t				n;
	socklen_t			len;
	const int			on = 1;
	struct sockaddr_in	cliaddr, servaddr;
	void				sig_chld(int);

	/* create listening TCP socket : fingerprint packet recv */
	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT+1);

	Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	/* create listening TCP socket : other packet */
	listenfd2 = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family      = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port        = htons(SERV_PORT);

	Setsockopt(listenfd2, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
	Bind(listenfd2, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd2, LISTENQ);

	Signal(SIGCHLD, sig_chld);	/* must call waitpid() */

	FD_ZERO(&rset);
	maxfdp1 = max(listenfd, listenfd2) + 1;
	for ( ; ; ) {
		FD_SET(listenfd, &rset);
		FD_SET(listenfd2, &rset);
		if ( (nready = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0) {
			if (errno == EINTR)
				continue;		/* back to for() */
			else
				err_sys("select error");
		}

		if (FD_ISSET(listenfd, &rset)) {
			len = sizeof(cliaddr);
			connfd = Accept(listenfd, (SA *) &cliaddr, &len);
	
			if ( (childpid = Fork()) == 0) {	/* child process */
				Close(listenfd);	/* close listening socket */
				recv_fp(connfd);	/* process the request : fp packet */
				exit(0);
			}
			
			Close(connfd);			/* parent closes connected socket */
		}

		if (FD_ISSET(listenfd2, &rset)) {
			len = sizeof(cliaddr);
			connfd = Accept(listenfd2, (SA *) &cliaddr, &len);
	
			if ( (childpid = Fork()) == 0) {	/* child process */
				Close(listenfd2);	/* close listening socket */
				recv_chunk(connfd);	/* process the request */
				exit(0);
			}
			
			Close(connfd);			/* parent closes connected socket */
		}
	}
}
int main(int argc, char **argv)
{
	int listenfd, connfd, udpfd, nready, maxfdp1;
	char mesg[MAXLINE];
	pid_t childpid;
	fd_set rset;
	ssize_t n;
	socklen_t len;
	const int on =1;
	struct sockaddr_in cliaddr, servaddr;
	void sig_chld(int);     //信号处理函数,防止产生僵尸进程

	//创建TCP套接字
	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

	//允许TCP端口上可以绑定多个地址
	Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
	Bind(listenfd, (struct sockaddr*) &servaddr, sizeof(servaddr));
	
	Listen(listenfd, LISTENQ);

	//创建UDP套接字
	udpfd = Socket(AF_INET, SOCK_DGRAM, 0);

	//经测试,此处绑定UDP时也可以直接使用上面TCP的地址值,但是一旦端口号或者IP地址变了之后还得重新加上
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);      //TCP与UDP端口独立,不存在重复
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

	Bind(udpfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

	Signal(SIGCHLD, sig_chld);    //避免出现僵尸进程,在子进程退出后调用waitpid

	FD_ZERO(&rset);

	maxfdp1 = max(listenfd, udpfd) + 1;


	for(; ;)
	{
		FD_SET(listenfd, &rset);
		FD_SET(udpfd, &rset);
		if((nready = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0)
		{
			if(errno == EINTR)
				continue;       //被信号中断重新调用select
			else
				err_sys("select error");
		}

		if(FD_ISSET(listenfd, &rset))
		{
			//TCP套接字建立新的连接
			len = sizeof(cliaddr);
			connfd = Accept(listenfd, (struct sockaddr*)&cliaddr, &len);

			if((childpid = Fork()) == 0)
			{                 
				//子进程
				Close(listenfd);
				str_echo(connfd);
				exit(0);
			}
			Close(connfd);
		}

		if(FD_ISSET(udpfd, &rset))
		{
			//UDP套接字的连接
			len = sizeof(cliaddr);

			n = Recvfrom(udpfd, mesg, MAXLINE, 0, (struct sockaddr*)&cliaddr, &len);

			Sendto(udpfd, mesg, n, 0, (struct sockaddr*)&cliaddr, len);
		}
	}

	return 0;
}
Esempio n. 3
0
int main()
{
    int Listenfd,connfd;
    socklen_t clilen;
    struct sockaddr_in cliaddr, servaddr;
    int val,len,i,last_in = 0,last_out = 0;
    struct tcp_info_user info;
    char writebuf[MAX_PKT_SIZE];

    Listenfd = Socket(AF_INET,SOCK_STREAM,0);

    memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port =  htons(SERV_PORT);
    
    Bind(Listenfd,(SA*)&servaddr,sizeof(servaddr));
    Listen(Listenfd,LISTENQ);

    for( ; ;){
        clilen = sizeof(cliaddr);
        connfd = Accept(Listenfd,(SA*)&cliaddr,&clilen);
        
        sleep(3);
        
        val = 1;
        len = sizeof(int);
        Setsockopt(connfd, SOL_TCP, TCP_NODELAY,(void *)&val, len);
        
        
        snprintf(writebuf,TRANSSIZE,"world01");
        Write(connfd,writebuf,strlen(writebuf)+1);
        
        sleep_ms(20);
        snprintf(writebuf,TRANSSIZE,"world02");
        Write(connfd,writebuf,strlen(writebuf)+1);
        
        sleep_ms(20);
        snprintf(writebuf,TRANSSIZE,"world03");
        Write(connfd,writebuf,strlen(writebuf)+1);
        
        
        sleep_ms(10);
        snprintf(writebuf,TRANSSIZE,"world04");
        Write(connfd,writebuf,strlen(writebuf)+1);
        
        
        sleep_ms(10);
        snprintf(writebuf,TRANSSIZE,"world05");
        Write(connfd,writebuf,strlen(writebuf)+1);;
        
        
        i = 0;
        while(i < 100*200)
        {
            len = sizeof(info);
            Getsockopt(connfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len);
            if( (last_in != info.tcpi_segs_in) || (last_out != info.tcpi_segs_out) )
            {
                sleep_ms(1);
                len = sizeof(info);
                Getsockopt(connfd, SOL_TCP, TCP_INFO,(void *)&info, (socklen_t *)&len);
                
                printftcpinfo(&info);

                printf("i=%d\n",i);
                
                last_in = info.tcpi_segs_in;
                last_out = info.tcpi_segs_out;
            }
            
            sleep_ms(10);
            i++;
        }
        
        sleep(200);
        
        printf("close\n");
        Close(connfd);
    }
    return 0;
}