示例#1
0
static void cmd_echo(char ch)
{
    if ((ch == 0x08 || ch == 0x7F) && cmd_args.len > 0)//退格或删除
    {
        char str[] = {0x08, ' ', 0x08};
        cmd_args.len--;
        str_echo(str, sizeof(str));
    }
    else
    {
        if (ch > 0x1f && ch < 0x80)
        {
            if (cmd_args.len < sizeof(cmd_args.cmdline) - 1)
            {
                cmd_args.cmdline[cmd_args.len++] = ch;
                str_echo(&ch, 1);
            }
        }
        else if (ch == 0x0D || ch == 0x0A)//newline carriage return
        {
            char str[] = {0x0D, 0x0A};

            cmd_args.cmdline[cmd_args.len++] = '\0';
            str_echo(str, sizeof(str));

            xSemaphoreGiveFromISR(xRxCompleteSemaphore, NULL);
        }
    }
}
示例#2
0
int main(void)
{
	//  daemon(0,0);
	signal(SIGCLD, SIG_IGN);
	socklen_t cliaddr_len;
	int listenfd,connfd;
	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);
	int nREUSEADDR = 1;
	setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&nREUSEADDR,sizeof(int));
	bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
	listen(listenfd,20);
	printf("Accepting connections...\n");
	while(1){
		cliaddr_len = sizeof(cliaddr);
		if((connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&cliaddr_len))<0){
			if(errno == EINTR)
				continue;
			else{
				perror("accept:");
				exit(0);
			}
		}
		char is_long ;
		recv(connfd,&is_long,1,0);
		//		printf("connfd is_long:%c\n",is_long);
		if(is_long == 'k')
		{
			int i;
			i = fork();
			if(i == -1){
				perror("fork error");
				exit(1);
			}else if(i == 0){
				close(listenfd);
				str_echo(connfd);
				exit(0);
			}else
				close(connfd);
		}else
		{
			str_echo(connfd);
		}
	}
}	
示例#3
0
文件: server_signal.c 项目: fitwu/unp
int main()
{
    int listenfd, connfd;
    pid_t pid;
    socklen_t clilen;
    struct sockaddr_in servaddr, cliaddr;
    
    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);

    bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    listen(listenfd, ACCEPTQ);
    signal(SIGCHLD, sig_chld);

    for(;;)
    {
        clilen = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
        if((pid = fork()) == 0)
        {
            close(listenfd);
            str_echo(connfd);
            exit(0);
        }
        close(connfd);
    }
}
示例#4
0
int main(int argc, char **argv)
{
  int serverSock, clientSock;
  struct sockaddr_in serverAddr, clientAddr;
  serverSock = socket(AF_INET, SOCK_STREAM, 0);

  bzero(&serverAddr, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serverAddr.sin_port = htons(5011);
 
  if (bind(serverSock,(struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
    perror("bind error");
    exit(1);
  }
  
  if (listen(serverSock, 0) < 0)
  {
    perror("listen error");
    exit(0);
  } 

  while(1)
  {
    int cliLen = sizeof(clientAddr);
    clientSock = accept(serverSock,
                        (struct sockaddr*)&clientAddr, 
                        (socklen_t *)&cliLen);
    str_echo(clientSock);
  }

  return 0;
}
示例#5
0
int main(void)
{
	int sockfd,connfd;
	pid_t chldpid;
	socklen_t chldlen;
	
	struct sockaddr_in servaddr,cliaddr;
	sockfd=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(PORT);
	bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
	listen(sockfd,LISTENQ);
	signal(SIGCHLD,sig_chld);
	for(;;)
	{
		chldlen=sizeof(cliaddr);
		if( (connfd=accept(sockfd,(struct sockaddr*)&cliaddr,&chldlen))<0 )
		{
			if(errno==EINTR)
			{
				printf("interupted sys call\n");
				continue;// restart sys call by self;
			}
		}	
		if( (chldpid=fork())==0)
		{
			close(sockfd);
			str_echo(connfd);
			exit(0);
		}
		close(connfd);
	}
}
示例#6
0
int main( int argc, char** argv ) {
	int listenfd;	
	struct sockaddr_in servaddr;

	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( 7778 );
	Bind( listenfd, (struct sockaddr*)&servaddr, sizeof( servaddr ) );
	Listen( listenfd, 5 );

	socklen_t clilen;
	struct sockaddr_in cliaddr;
	int connfd;
	pid_t childpid;
	for( ; ; ) {
		clilen = sizeof( cliaddr );
		connfd = Accept( listenfd, ( struct sockaddr* )&cliaddr, &clilen );
		if( ( childpid = Fork()) == 0 ) {
			Close( listenfd );
			str_echo( connfd );
			exit( 0 );
		}

		Close( connfd );
	}
}
示例#7
0
int main(int argc, const char *argv[])
{
	int listenfd, connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;
	listenfd = socket_r(AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(argv[1]);
	servaddr.sin_port = htons(80);
	//void bind_r(int listening_socket, const struct sockaddr *addr, socklen_t addrlen, void *arg);
	bind_r(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr), (char *)argv[1]);
	//void listen_r(int sockfd, int backlog, void *arg);
	listen_r(listenfd, 10, (char *)argv[1]);
	for(;;){
		clilen = sizeof(cliaddr);
		//int accept_r(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
		connfd = accept_r(listenfd, (struct sockaddr *)&cliaddr, &clilen);	
		if((childpid = fork_r()) == 0){
			close_r(listenfd);
			str_echo(connfd);
			exit(0);
		}
	}
	close_r(connfd);
	return 0;
}
示例#8
0
int main(int argc,char *argv[]){
	int listenfd,sockfd;
	pid_t pid;
	socklen_t len;
	struct sockaddr_un un;
	if(argc != 2)
		err_quit("usage server_un <pathname>");
	memset(&un,0,sizeof(un));
	un.sun_family = AF_LOCAL;
	strncpy(un.sun_path,argv[1],sizeof(argv[1]));
	sockfd = socket(AF_LOCAL,AF_STREAM,0);
	bind(listenfd,&un,SUN_LEN(&un));
	Listen(listenfd,LISTENQ);
	for(;;){
		if((sockfd=accept(listenfd,&un,&len))<0){
			if(errno == EINTR)
				continue;
			else
				err_sys("accept error");
		}
		if((pid=fork()) == 0) {
			Close(listenfd);
			str_echo(sockfd);
			exit(0);
		}
		Close(sockfd);
	}
}
示例#9
0
文件: serv.c 项目: zz198808/unp-1
int main(int argc,char **argv)
{
	int listenfd,connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr,servaddr;

	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);

	Bind(listenfd,&servaddr,sizeof(servaddr));
	Listen(listenfd,20);
	
	signal(SIGCHLD,sig_chld);

	printf("server is listening\n");

	for(;;)
	{
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd,&cliaddr,&clilen);
		if( (childpid = Fork() == 0) )
		{
			Close(listenfd);	
			str_echo(connfd);
			exit(0);
		}
		Close(connfd);
	}
}
示例#10
0
int main(int argc , char ** argv)
{
    int  listenfd, connfd;// usually, server creates two sockets,one for listen , one for connect
    pid_t childpid;
    socklen_t clilen;
    struct sockaddr_in cliaddr, servaddr;//define two ip address structs

    listenfd = socket(AF_INET, SOCK_STREAM , 0);

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // 注意, 这里是要把地址填进 struct sockaddr_in 中, 必须要用network byte order,
    servaddr.sin_port = htons(SERV_PORT);//SERV_PORT  is defined as  9877 in my own head file  "wy.h"

    bind(listenfd,(SA *) &servaddr , sizeof(servaddr));
    listen(listenfd,10);// the book uesed LISTENQ as the backlog,attention the second argu..,an int it represents the max number of connections waiting for
    for( ; ; )
    {
        clilen=sizeof(cliaddr);
        connfd=accept(listenfd,(SA *)&cliaddr, &clilen); // !accpet,bind,connect have three same types of arguments!!,a socket number and the address of its address struct and the lenth. accept returns a new socket number that has established connection
        printf("the connect socket descriptor is : %d \n", connfd);
        if((childpid=fork())==0)
        {
            close(listenfd);//attention!!这里关掉的是child process 中的 listenfd, the listenfd in the parent process is stillthere
            str_echo(connfd);
            exit(0);  //child process exits
        }
        close(connfd);  // parent process closes the connect socket, after the child process exits
    }



}
示例#11
0
int main()
{
    int         listenfd, connfd;
    pid_t       childpid;
    socklen_t   clilen;
    struct sockaddr_in      cliaddr, servaddr;

    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(9877);
    
    bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ);

    while (1) {
        clilen = sizeof(cliaddr);
        connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);

        if ((childpid = fork()) == 0) {
            close(listenfd);
            str_echo(connfd);
            exit(0);
        }

        close(connfd);
    }
}
示例#12
0
int main(int argc, char const *argv[])
{
  int listenfd, connfd;
  struct sockaddr_in servaddr, cliaddr;
  int len;

  if (argc != 2)
  {
    fprintf(stderr, "usage: tcpserv <port>\n");
    exit(1);
  }

  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("socket error");
    exit(1);
  }

  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(atoi(argv[1]));
  servaddr.sin_addr.s_addr = htons(INADDR_ANY);

  if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
  {
    perror("bind error");
    exit(1);
  }

  if (listen(listenfd, LISTENQ) < 0)
  {
    perror("listen error");
    exit(1);
  }

  Signal(SIGCHLD, sig_chld);

  while (1)
  {
    len = sizeof(cliaddr);
    // pause();
    if ((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &len)) < 0)
    {
      if (errno == EINTR)
        continue;

      perror("accept error");
      exit(1);
    }

    if (fork() == 0)
    {
      close(listenfd);
      str_echo(connfd, &cliaddr);
      exit(0);
    }

    close(connfd);
  }
  return 0;
}
示例#13
0
int main(int argc, char **argv)
{
	int listenfd, connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_un cliaddr, servaddr;
	void sig_child(int);

	listenfd = Socket(AF_LOCAL, SOCK_STREAM, 0);

	unlink(UNIXSTR_PATH);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sun_family = AF_LOCAL;
	strcpy(servaddr.sun_path, UNIXSTR_PATH);

	Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sigchld_waitpid);

	for ( ; ; )
	{
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *)&cliaddr, &clilen);
        if ((childpid = Fork()) == 0)
		{
			Close(listenfd);
			str_echo(connfd);
			exit(0);
		}
		Close(connfd);
	}
}
示例#14
0
int
main(int argc, char **argv)
{
	int					listenfd, connfd;
	pid_t				childpid;
	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;

	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);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

		if ( (childpid = Fork()) == 0) {	/* child process */
			Close(listenfd);	/* close listening socket */
			str_echo(connfd);	/* process the request */
            sleep(10);
			exit(0);
		}
		Close(connfd);			/* parent closes connected socket */
	}
}
示例#15
0
int main(int argc, char **argv){
  int listenfd, connfd;
  pid_t childpid;
  socklen_t clilen;
  struct sockaddr_in cliaddr, servaddr;

  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);

  Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
  Listen(listenfd, LISTENQ);

  for( ; ; ) {
    clilen = sizeof(cliaddr);
    /*connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);*/
    // handler break slow system call
    if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
      if(errno == EINTR)
        continue;
      else
        err_sys("accept error");
    }

    if( (childpid = Fork()) == 0) {
      Close(listenfd);
      str_echo(connfd);
      exit(0);
    }
    Close(connfd);
  }
}
示例#16
0
int main(int argc, char **argv) {
    int listenfd, connfd;
    struct sockaddr_in servaddr, client_addr;
    char buff[MAXLINE];
    time_t ticks;
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    /*for any interface on server */
    servaddr.sin_port = htons(atoi(argv[1]));
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    listen(listenfd, LISTENQ);
    bzero(&client_addr, sizeof(client_addr));

    signal(SIGCHLD, sig_chld);


    while (1) {
        int addr_len = sizeof(client_addr);
        connfd = accept(listenfd, (SA *)&client_addr, (socklen_t *)&addr_len);
        printf("IP address is: %s, ", inet_ntoa(client_addr.sin_addr));
        printf("port is: %d\n", (int) ntohs(client_addr.sin_port));
        pid_t pid;
        if ((pid = fork()) == 0) { /* child process */
            close(listenfd); /* close listening socket */
            str_echo(connfd); /* process the request */
            exit (0);
        }
        close(connfd);
        /* parent closes connected socket */
    }
    return 0;
}
int main(int argc, char *argv[])
{
    int listenfd, connfd;
    pid_t childpid;
    socklen_t clilen;
    struct sockaddr_in cliaddr, servaddr;
    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);
    Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    Listen(listenfd, LISTENQ);
    while (1)
    {
        clilen = sizeof(cliaddr);
        connfd = Accept(listenfd, (SA *)&cliaddr, &clilen);
        if ((childpid = Fork()) == 0)
        {
            Close(listenfd);
            str_echo(connfd);
            exit(0);
        }
        Close(connfd);
    }
    return 0;
}
示例#18
0
int main(int argc,char **argv)
{

	int listenfd,connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr,servaddr;

	listenfd=socket(AF_INET,SOCK_STREAM,0);
	if(listenfd<0)
	printf("error socket\n");

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

	if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
	printf("error bind\n");
	if(listen(listenfd,LISTENQ)<0)
	printf("error linstn\n");
	for(;;){
		clilen=sizeof(cliaddr);
		if((connfd=accept(listenfd,(struct sockaddr*) &cliaddr,&clilen))<0){
		printf("error accept");	
	
	}
		if((childpid=fork())==0){
			close(listenfd);
			str_echo(connfd);
			exit(0);
		}
		close(connfd);
	}
}
示例#19
0
int main(int argc, char **argv) {
    int listenfd, connfd, status;
    pid_t childpid;
    char line[MAXLINE];
    socklen_t clilen;
    struct sockaddr_in servaddr;
    struct sockaddr_in cliaddr;
    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(PORT);
    bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
    listen(listenfd, LISTENQ);

    for( ;;) {
        clilen = sizeof(cliaddr);
        connfd=accept(listenfd, (SA *)&cliaddr, &clilen);
        printf("connection from %s, port %d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
        childpid = fork();

        if (childpid == 0) {
            close(listenfd);
            str_echo(connfd);
            exit(0);
        } else {
            signal(SIGCHLD, sig_chld);
        }

        close(connfd);
    }
}
示例#20
0
int main(int argc, char **argv)
{
	int listenfd,connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr,servaddr;

	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);
	bind(listenfd, (SA *)&servaddr, sizeof(servaddr));

printf("start listening...\n");
	listen(listenfd, LISTENQ);
	
	for( ; ; )
	{
		clilen = sizeof(cliaddr);
		connfd = accept(listenfd, (SA *)&cliaddr, &clilen);

		if( (childpid = fork()) == 0 )
		{
			close(listenfd);
//printf("i am in clild process\n");
			str_echo(connfd);
			exit(0);
		}
		close(connfd);
	}
}
示例#21
0
static void* doit(void* arg)
{
	Pthread_detach(pthread_self());
	str_echo((int)arg);
	Close((int)arg);
	return NULL;
}
示例#22
0
int main(int argc, char **argv)
{
	int listenfd, connfd;
	pid_t childpid;
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;
	listenfd = socket (AF_INET, SOCK_STREAM, 0);
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl (INADDR_ANY); // for any interface
	servaddr.sin_port = htons (SERV_PORT); //user define
	bind(listenfd, (__SOCKADDR_ARG) &servaddr, sizeof(servaddr));
	listen(listenfd, LISTENQ);
	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = accept(listenfd, (__SOCKADDR_ARG) &cliaddr, &clilen);
		if ( (childpid = fork()) == 0) { /* child process */
			close(listenfd);
			/* close listening socket */
			str_echo(connfd); /* process the request */
			exit (0); //end child process
		}
		close(connfd);
		/* parent closes connected socket */
	 }
 }
示例#23
0
int
main(int argc, char **argv)
{
	int					listenfd, connfd;
	pid_t				childpid;
	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;
	void				sig_chld(int);			// 处理信号量函数, 这里在Mac OS X系统上编译不过

	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);

	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	Signal(SIGCHLD, sig_chld);					// 捕获`SIGCHLD`,并调用`wait`或`waitpid`来处理僵死进程
	
	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

		if ( (childpid = Fork()) == 0) {	/* child process */
			Close(listenfd);	/* close listening socket */
			str_echo(connfd);	/* process the request */
			exit(0);
		}
		Close(connfd);			/* parent closes connected socket */
	}
}
示例#24
0
int
main(int argc, char **argv)
{
	int sock_fd,msg_flags,connfd,childpid;
	sctp_assoc_t assoc;
	char readbuf[BUFFSIZE];
	struct sockaddr_in servaddr, cliaddr;
	struct sctp_sndrcvinfo sri;
	struct sctp_event_subscribe evnts;
	socklen_t len;
	size_t rd_sz;

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

	Bind(sock_fd, (SA *) &servaddr, sizeof(servaddr));
	
	bzero(&evnts, sizeof(evnts));
	evnts.sctp_data_io_event = 1;
	Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS,
		   &evnts, sizeof(evnts));

	Listen(sock_fd, LISTENQ);
	printf("Start waiting...\n");
/* include mod_servfork */
	for ( ; ; ) {
		len = sizeof(struct sockaddr_in);
		rd_sz = Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf),
			     (SA *)&cliaddr, &len,
			     &sri,&msg_flags);
		Sctp_sendmsg(sock_fd, readbuf, rd_sz, 
			     (SA *)&cliaddr, len,
			     sri.sinfo_ppid,
			     sri.sinfo_flags,
			     sri.sinfo_stream,
			     0, 0);
		assoc = sctp_address_to_associd(sock_fd,(SA *)&cliaddr,len);
		if((int)assoc == 0){
			err_ret("Can't get association id");
			continue;
		} 
		connfd = sctp_peeloff(sock_fd,assoc);
		if(connfd == -1){
			err_ret("sctp_peeloff fails");
			continue;
		}
		if((childpid = fork()) == 0) {
			Close(sock_fd);
			str_echo(connfd);
			exit(0);
		} else {
			Close(connfd);
		}
	}
/* end mod_servfork */
}
int main()
{
	int listenfd,connectfd;
	struct sockaddr_in server,client;
    pid_t child_pid;
    socklen_t clilen;
	if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
      {
      	perror("Socket not created");
      }
     else
     {
     	printf("Socket created\n");
     } 		

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

     if((bind(listenfd,(struct sockaddr*)&server,sizeof(server)))<0)
     {
     	perror("Bind failed");
     }
     else
     {
     	printf("Bind successful \n");
     } 
     
     listen(listenfd,5);
      
     signal(SIGCHLD,sig_chld);
      
     /*Daemon :D*/ 
     for(;;)
     {
     	//clilen=sizeof(client);
        if( (connectfd=accept(listenfd,(struct sockaddr*) NULL,NULL) )<0)
        {
          if(errno == EINTR)   /* Interrupted system call */
             continue;
           else
            err_sys("accept error");
        }


        if((child_pid=fork())==0)
        {
        	close(listenfd);
        	str_echo(connectfd);
            exit(0);   
                        
        }   
        close(connectfd);
     }	


}
示例#26
0
static void *
doit(void *arg)
{
	Pthread_detach(pthread_self());
	str_echo((int) arg);	/* same function as before */
	Close((int) arg);		/* done with connected socket */
	return(NULL);
}
示例#27
0
int
main(int argc, char **argv)
{
	int					listenfd, connfd;
	pid_t				childpid;
	socklen_t			clilen;
	struct sockaddr_in	cliaddr, servaddr;
	void				sig_chld(int);

	if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket error");
		exit(1);
	}

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

	if (bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) < 0) {
		perror("bind error");
		exit(1);
	}

	if (listen(listenfd, LISTENQ) < 0) {
		perror("listen error");
		exit(1);
	}

	if (signal(SIGCHLD, sig_chld) == SIG_ERR) {
		perror("signal error");
		exit(1);
	}

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		if ((connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
			perror("accept error");
			exit(1);
		}

		if ( (childpid = fork()) < 0) {
			perror("fork error");
			exit(1);
		} else if (childpid == 0) {	/* child process */
			if (close(listenfd) == -1) {	/* close listening socket */
				perror("close error");
				exit(1);
			}
			str_echo(connfd);	/* process the request */
			exit(0);
		} 
		if (close(connfd) == -1) {	/* parent closes connected socket */
			perror("close error");
			exit(1);
		}
	}
}
示例#28
0
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);
    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);
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    listen(listenfd, LISTENQ);
    /* for create UDP socket */
    udpfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    bind(udpfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    signal(SIGCHLD, sig_chld);/* must call 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; /* back to for() */ else
                printf("select error");
        }
        if (FD_ISSET(listenfd, &rset)) {
            len = sizeof(cliaddr);
            connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len);
            print_ip_port(cliaddr);
            if ( (childpid = fork()) == 0) { /* child process */
                close(listenfd);
                str_echo(connfd);
                exit(0);
            }
            close(connfd);
        }
        if (FD_ISSET(udpfd, &rset)) {
            len = sizeof(cliaddr);
            n = recvfrom(udpfd, mesg, MAXLINE, 0, (struct sockaddr *) &cliaddr, &len);
            puts("udp");
            sendto(udpfd, mesg, n, 0, (struct sockaddr *) &cliaddr, len);
        }
    }
    return 0;
}
示例#29
0
int
main(int argc, char **argv)
{
	int                 listenfd, connfd;
	pid_t               childpid;
	socklen_t           clilen;
	struct sockaddr_in  cliaddr, servaddr;
    uint16_t            port;

    if (argc != 2) {
        usage(argv[0]);
        return EXIT_FAILURE;
    }
    port = atoi(argv[1]);
	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(port);

    /* SO_REUSEADDR allows a new server to be started
     * on the same port as an existing server that is
     * bound to the wildcard address, as long as each
     * instance binds a different local IP address.
     * This is common for a site hosting multiple HTTP
     * servers using the IP alias technique */
    int reuseaddr_on = 1;
    if( setsockopt( listenfd, SOL_SOCKET, SO_REUSEADDR,
                &reuseaddr_on, sizeof( reuseaddr_on)) < 0)
    {
        // log
    }

	Bind( listenfd, (SA *) &servaddr, sizeof( servaddr));

	Listen( listenfd, LISTENQ);

	Signal( SIGCHLD, sig_chld);

	for ( ; ; ) {
		clilen = sizeof(cliaddr);
		if ( ( connfd = accept( listenfd, (SA *) &cliaddr, &clilen)) < 0) {
			if ( errno == EINTR)
				continue;		/* back to for() */
			else
				err_sys( "accept error");
		}

		if ( ( childpid = Fork()) == 0) {	/* child process */
			Close( listenfd);               /* close listening socket */
			str_echo( connfd);              /* process the request */
			exit( 0);
		}
		Close( connfd);			/* parent closes connected socket */
	}
    return 0;
}
示例#30
0
void *
doit(void *arg)
{
  long val = (long)arg;
  pthread_detach(pthread_self());
  str_echo(val);
  close(val);
  return NULL;
}