Exemplo n.º 1
0
int main(int argc,char *argv[])
{
	int	connfd,stdno,maxfd,flag;
	char	sendmsg[MAXLINE],recvmsg[MAXLINE];
	fd_set	rset;
	maxfd = connfd = stdno = -1;
	flag = 1;
	if( argc == 2 )
		connfd = Tcp_connect(NULL,argv[1]);
	else if(argc == 3)
		connfd = Tcp_connect(argv[1],argv[2]);
	else
		err_quit("usage: client02 [<host>] <serv>\n");
	FD_ZERO(&rset);
	
	stdno = fileno(stdin);
	maxfd = max(stdno,connfd);
	while(1){
		FD_SET(connfd,&rset);
		if(flag){
			FD_SET(stdno,&rset);
		}

		if((select(maxfd+1,&rset,NULL,NULL,NULL)) < 0)
			err_quit("select error\n");

		if(FD_ISSET(stdno,&rset)) {
			if(Fgets(sendmsg,MAXLINE,stdin) == NULL){
				flag = 0;
				FD_CLR(stdno,&rset);
				shutdown(connfd,SHUT_WR);
				continue;
			}else{
				Writen(connfd,sendmsg,strlen(sendmsg));
			}
		}

		if(FD_ISSET(connfd,&rset)) {
			if(Readline(connfd,recvmsg,MAXLINE) == 0) {
				if(flag == 0){
					printf("client program over!\n");
					break;	//客户端程序结束
				}else{
					err_quit("server terminated prematurely");
				}
			} else {
				Fputs(recvmsg,stdout);
			}
		}
	}
	exit(0);
}
Exemplo n.º 2
0
int main(int argc,char *argv[])
{
	int	sockfd;
	if(argc == 2)
		sockfd = Tcp_connect(NULL,argv[1]);
	else if(argc == 3)
		sockfd = Tcp_connect(argv[1],argv[2]);
	else
		err_quit("usage:client01 [<hostname>] service\n");
	
	str_cli(stdin,sockfd);
	Close(sockfd);
	exit(0);
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	int		sockfd, n, npend;
	char	recvline[MAXLINE + 1];
	socklen_t	len;
	struct sockaddr_storage	ss;

	if (argc != 3)
		err_quit("usage: a.out <hostname or IPaddress> <service or port#>");

	sockfd = Tcp_connect(argv[1], argv[2]);

	len = sizeof(ss);
	Getpeername(sockfd, (SA *)&ss, &len);
	printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len));

	for ( ; ; ) {
		if ( (n = Recv(sockfd, recvline, MAXLINE, MSG_PEEK)) == 0)
			break;		/* server closed connection */

		Ioctl(sockfd, FIONREAD, &npend);	/* check FIONREAD support */
		printf("%d bytes from PEEK, %d bytes pending\n", n, npend);

		n = Read(sockfd, recvline, MAXLINE);
		recvline[n] = 0;	/* null terminate */
		Fputs(recvline, stdout);
	}
	exit(0);
}
Exemplo n.º 4
0
int
main(int argc, char **argv)
{
	int		sockfd;

	if (argc != 3)
		err_quit("usage: tcpsend01 <host> <port#>");

	sockfd = Tcp_connect(argv[1], argv[2]);

	Write(sockfd, "123", 3);
	printf("wrote 3 bytes of normal data\n");
	sleep(1);

	Send(sockfd, "4", 1, MSG_OOB);
	printf("wrote 1 byte of OOB data\n");
	sleep(1);

	Write(sockfd, "56", 2);
	printf("wrote 2 bytes of normal data\n");
	sleep(1);

	Send(sockfd, "7", 1, MSG_OOB);
	printf("wrote 1 byte of OOB data\n");
	sleep(1);

	Write(sockfd, "89", 2);
	printf("wrote 2 bytes of normal data\n");
	sleep(1);

	exit(0);
}
Exemplo n.º 5
0
int
main(int argc, char **argv)
{
    int				sockfd, n;
    char			recvline[MAXLINE + 1];
    socklen_t		len;
    struct sockaddr	*sa;

    if (argc != 3)
        err_quit("usage: daytimetcpcli <hostname/IPaddress> <service/port#>");

    sockfd = Tcp_connect(argv[1], argv[2]);

    sa = Malloc(sizeof(struct sockaddr_storage));
    len = sizeof(struct sockaddr_storage);
    Getpeername(sockfd, sa, &len);
    printf("connected to %s\n", Sock_ntop_host(sa, len));
    sleep(5);

    while ( (n = Read(sockfd, recvline, MAXLINE)) > 0)
    {
        recvline[n] = 0;	/* null terminate */
        printf("%d bytes: %s", n, recvline);
    }
    exit(0);
}
int 
main(int argc, char **argv) 
{ 
	int sockfd, n; 
	char recvline[MAXLINE + 1]; 
	socklen_t len; 
	struct sockaddr_storage ss; 
 
	if (argc != 3) 
		err_quit("usage: daytimetcpcli <hostname/IPaddress> <service/port#>"); 
 
	sockfd = Tcp_connect(argv[1], argv[2]); 
 
	len = sizeof(ss); 
	Getpeername(sockfd, (SA *)&ss, &len); 
	printf("connected to %s\n", Sock_ntop_host((SA *)&ss, len)); 
	sleep(5);
	
	while ( (n = Read(sockfd, recvline, MAXLINE)) > 0) { 
		recvline[n] = 0; /* null terminate */ 
		fprintf(stderr, "nbytes read: %d\n", n);
		Fputs(recvline, stdout); 
	} 
	exit(0); 
} 
Exemplo n.º 7
0
void
home_page(const char *host, const char *fname)
{
	int					fd, n;
	char				line[MAXLINE];

	fd = Tcp_connect(host, SERV);

	strcpy(line, "GET ");
	strcat(line, fname);
	strcat(line, " HTTP/1.0\r\n\r\n");
	n = strlen(line);
	if (writen(fd, line, n) != n)
		err_sys("writen error");

	for ( ; ; ) {
		if ( (n = read(fd, line, MAXLINE)) <= 0) {
			if (n == 0)
				break;		/* server closed connection */
			else
				err_sys("read error");
		}
		printf("read %d bytes of home page\n", n);
		/* do whatever with data */
	}
	printf("end-of-file on home page\n");
	close(fd);
}
Exemplo n.º 8
0
void *
do_get_read(void *vptr)
{
  int         fd, n;
  char        line[MAXLINE];
  struct file     *fptr;

  fptr = (struct file *) vptr;

  fd = Tcp_connect(fptr->f_host, SERV);
  fptr->f_fd = fd;
  printf("do_get_read for %s, fd %d, thread %d\n",
      fptr->f_name, fd, fptr->f_tid);

  write_get_cmd(fptr);  /* write() the GET command */

    /* 4Read server's reply */
  for ( ; ; ) {
    if ( (n = Read(fd, line, MAXLINE)) == 0)
      break;    /* server closed connection */

    printf("read %d bytes from %s\n", n, fptr->f_name);
  }
  printf("end-of-file on %s\n", fptr->f_name);
  Close(fd);
  fptr->f_flags = F_DONE;   /* clears F_READING */

  Pthread_mutex_lock(&ndone_mutex);
  ndone++;
  Pthread_cond_signal(&ndone_cond);
  Pthread_mutex_unlock(&ndone_mutex);

  return(fptr);   /* terminate thread */
}
Exemplo n.º 9
0
int
main(int argc, char **argv)
{
	int		sockfd, size;
	char	buff[16384];

	if (argc != 3)
		err_quit("usage: tcpsend04 <host> <port#>");

	sockfd = Tcp_connect(argv[1], argv[2]);

	size = 32768;
	Setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));

	Write(sockfd, buff, 16384);
	printf("wrote 16384 bytes of normal data\n");
	sleep(5);

	Send(sockfd, "a", 1, MSG_OOB);
	printf("wrote 1 byte of OOB data\n");

	Write(sockfd, buff, 1024);
	printf("wrote 1024 bytes of normal data\n");

	exit(0);
}
int
client_socket(const char *host, const char *service)
{
     int sockfd;
     sockfd = Tcp_connect(host, service);
     return (sockfd);
     
}
Exemplo n.º 11
0
int main()
{
	pthread_t timeloop_thread_id;
	timed_action_t *time_action_cb;
	timed_action_t *ta_enroll_check;
	printf("===============%s version[%s], COMPILE_TIME[%s: %s]\n", APP_NAME, APP_VERSION, __DATE__, __TIME__);
//	printf("---------%s---------\n", PROJECT_VERSION);
	printf("------------------------------------------------\n");
	printf("------------------------------------------------\n");

	time_loop_init();
	linkage_head_init();
	int rc = curl_global_init(CURL_GLOBAL_ALL);
	if (rc != CURLE_OK) {
		GDGL_DEBUG("curl_global_init error\n");
		exit (1);
	}
	int tempvalue;
	//获取网关报警时长
	if(http_get_warningduration(&tempvalue) ==0) {
		setting_warningduration(tempvalue);
	}
	ta_enroll_check = timed_action_schedule_periodic(notifier, TIMEOUT_UNIT, 0, &app5s_task, NULL); //5s task

	//读数据库启用的定时规则信息,保存到列表
	if(time_action_get_enable_list(list_time) <0) {
		//读表出错,程序退出
		GDGL_DEBUG("time action enable list read error!\n");
		exit(1);
	}
	//读联动列表信息,保存到双向链表
	if(linkage_get_enable_list_db(&linkage_loop_head) <0) {
		//读表出错,程序退出
		GDGL_DEBUG("linkage enable list read error!\n");
		exit(1);
	}
	linkage_traverse_printf();
	list_time_printf_tid();
	//新开线程,循坏检测定时列表。触发定时操作后,列表需更新
	Pthread_create(&timeloop_thread_id, NULL, time_loop_main, NULL);

	//连接端口5018,实时检测相关callback信息,维护列表
	while(1){
		fd_connect_callback = Tcp_connect(NULL, FEATURE_GDGL_CB_DAEMON_CALLBACK_PORT_STR);
		//send heartbeat
		time_action_cb = timed_action_schedule_periodic(notifier, CB_HEART_BEAT_TIME, 0, &send_heartbeat, &fd_connect_callback);
	    if(time_action_cb ==NULL){
	    	GDGL_DEBUG("time task send heartbeat init error\n");
	    	exit(1);
	    }
		//read data
		read_callback_data_handle(fd_connect_callback);
		timed_action_unschedule(notifier, time_action_cb);//stop time task
	}
	curl_global_cleanup();
}
Exemplo n.º 12
0
void home_page(const char *hostname,const char *filename){
	int fd,i,n;
	char buffer[MAXLINE];
	fd = Tcp_connect(hostname,SERV);
	snprintf(buffer,sizeof(buffer),GET_CMD,filename);
	Writen(fd,buffer,sizeof(buffer);
	while((n = Readn(fd,buffer,MAXLINE)) > 0)
		printf("read %d bytes from %s\n",n,filename);
	printf("end-of-file home page\n");
	Close(fd);
}
Exemplo n.º 13
0
int
main(int argc, char **argv)
{
	int		tfd;

	if (argc != 3)
		err_quit("usage: test01 <hostname/IPaddress> <service/port#>");

	tfd = Tcp_connect(argv[1], argv[2]);

	t_snd(tfd, "", 1, T_EXPEDITED);

	exit(0);
}
Exemplo n.º 14
0
int
main(int argc, char **argv)
{
	int		i, j, fd, nchildren, nloops, nbytes;
	pid_t	pid;
	ssize_t	n;
	char	request[MAXLINE], reply[MAXN];
	int count = 0;

	if (argc != 6)
		err_quit("usage: client <hostname or IPaddr> <port> <#children> "
				 "<#loops/child> <#bytes/request>");

	nchildren = atoi(argv[3]);
	nloops = atoi(argv[4]);
	nbytes = atoi(argv[5]);
	snprintf(request, sizeof(request), "%d\n", nbytes); /* newline at end */
	my_lock_init(NULL);

	for (i = 0; i < nchildren; i++) {
		if ( (pid = Fork()) == 0) {		/* child */
			for (j = 0; j < nloops; j++) {
				fd = Tcp_connect(argv[1], argv[2]);

				/*printf("%d : %d\n", getpid(), j);
				Write(fd, request, strlen(request));

				if ( (n = Readn(fd, reply, nbytes)) != nbytes)
					err_quit("server returned %d bytes", n);

				Close(fd);*/		/* TIME_WAIT on client, not server */
			}
			my_lock_wait();
			printf("child %d done\n", i);
			my_lock_release();
			sleep(10);
			exit(0);
		}
		/* parent loops around to fork() again */
	}

	while (wait(NULL) > 0)	/* now parent waits for all children */
		;
	if (errno != ECHILD)
		err_sys("wait error");
	printf("all child: %d\n", count);
	exit(0);
}
Exemplo n.º 15
0
int main(int argc, char** argv) {

    int sockfd;
    
    if (argc < 3)
	usage("");
    
    sockfd = Tcp_connect( argv[1], atoi( argv[2]), AF_INET);
    if( sockfd < 0) exit(-1);
    
    printf( "OK\n");
    sleep( 15);
    Close( sockfd);
        
    return (EXIT_SUCCESS);
}
Exemplo n.º 16
0
int
main(int argc, char **argv)
{
	int		fd;
	fd_set	exset;

	if (argc != 3)
		err_quit("usage: test01 <hostname/IPaddress> <service/port#>");

	fd = Tcp_connect(argv[1], argv[2]);

	FD_ZERO(&exset);
	FD_SET(fd, &exset);
	select(fd+1, NULL, NULL, &exset, NULL);

	exit(0);
}
Exemplo n.º 17
0
void home_page(const char *host, const char *fname)
{
    int fd, n;
    char line[MAXLINE];

    fd = Tcp_connect(host, SERV);
    n  = snprintf(line, sizeof(line), GET_CMD, fname);
    Writen(fd, line, n);

    while(true)
    {
        if((n = read(fd, line, MAXLINE)) == 0)
            break;
        printf("read %d bytes of home page\n", n);
    }
    printf("end of file on homepage\n");
    close(fd);
}
Exemplo n.º 18
0
void home_page(const char* host, const char* fname)
{
    int fd, n;
    char line[MAXLINE];

    fd = Tcp_connect(host, SERV); /* blocking connect() */

    n = snprintf(line, sizeof(line), GET_CMD, fname);
    Writen(fd, line, n);

    for (;;) {
        if ((n = Read(fd, line, MAXLINE)) == 0)
            break; /* server closed connection */

        printf("read %d bytes of home page\n", n);
        /* do whatever with data */
    }
    printf("end-of-file on home page\n");
    Close(fd);
}
Exemplo n.º 19
0
int main(int argc, char *argv[]) 
{
	int	sockfd, n;
	char	recvline[MAXLINE + 1]; /* plus 1 for terminator */
	socklen_t len;
	struct	sockaddr_storage ss;

	if (argc != 3)
		err_quit("usage: daytimetcpcli <hostname/IPaddress>"
			 " <service/port#>");

	sockfd = Tcp_connect(argv[1], argv[2]);
	
	len = sizeof(ss);
	Getpeername(sockfd, (SA *)&ss, &len);
	printf("connect to %s\n", Sock_ntop_host((SA *)&ss, len));

	while ((n = Read(sockfd, recvline, MAXLINE)) > 0) {
		recvline[n] = 0;
		Fputs(recvline, stdout);
	}
	exit(0);
}
Exemplo n.º 20
0
int main(int argc, char const *argv[])
{
	int sockfd,n;
	char recvline[MAXLINE+1];
	socklen_t len;
	struct sockaddr_storage ss;
	if (argc!=3)
	{
		err_quit("usage: %s <hostname/IPaddress> <service/port#>",argv[0]);
	}

	sockfd=Tcp_connect(argv[1],argv[2]);

	len=sizeof(ss);
	Getpeername(sockfd,(SA*)&ss,&len);
	printf("connected to %s\n",Sock_ntop_host((SA*)&ss,len));

	while((n=Read(sockfd,recvline,MAXLINE))>0)
	{
		recvline[n]='\0';
		Fputs(recvline,stdout);
	}
	return 0;
}
Exemplo n.º 21
0
int main(int argc, char** argv)
{
    int i, j, fd, nchildren, nloops, nbytes;
    pid_t pid;
    ssize_t n;
    char request[MAXLINE], reply[MAXN];

    if (argc != 6)
        err_quit(
            "usage: client <hostname or IPaddr> <port> <#children> "
            "<#loops/child> <#bytes/request>");

    nchildren = atoi(argv[3]);
    nloops = atoi(argv[4]);
    nbytes = atoi(argv[5]);
    snprintf(request, sizeof(request), "%d\n", nbytes); /* newline at end */

    for (i = 0; i < nchildren; i++) {
        if ((pid = Fork()) == 0) { /* child */
            for (j = 0; j < nloops; j++) {
                fd = Tcp_connect(argv[1], argv[2]);

                /*
                 * We want to see what happens to the server when it has
                 * connections outstanding and an RST arrives for one of
                 * them, before the connection is accepted.
                 * With the XTI server, this should generate some
                 * T_DISCONNECT events from t_accept(), which must be
                 * handled correctly.
                 *
                 * We do this for every third connection from the third
                 * client child.  (Could add more command-line args ...)
                 */

                if (i == 2 && (j % 3) == 0) {
                    struct linger ling;

                    ling.l_onoff = 1;
                    ling.l_linger = 0;
                    Setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
                    Close(fd);

                    /* and just continue on for this client connection ... */
                    fd = Tcp_connect(argv[1], argv[2]);
                }

                Write(fd, request, strlen(request));

                if ((n = Readn(fd, reply, nbytes)) != nbytes)
                    err_quit("server returned %d bytes", n);

                Close(fd); /* TIME_WAIT on client, not server */
            }
            printf("child %d done\n", i);
            exit(0);
        }
        /* parent loops around to fork() again */
    }

    while (wait(NULL) > 0) /* now parent waits for all children */
        ;
    if (errno != ECHILD) err_sys("wait error");

    exit(0);
}