Ejemplo n.º 1
0
int main(int argc, char *argv[])
{    
  	if(signal(SIGTSTP,handler)==SIG_ERR||signal(SIGINT,handler) == SIG_ERR)
        	unix_error("signal error");

    char cmdline[MAXLINE];
	printf("%s\n","We have following commands in our main system: ");
    printf("%s\n","help\t we are glad to offer you our best services...");
    printf("%s\n","checkin\t welcome to join our Oxford Heights Community...");
    printf("%s\n","checkout\t we are sorry to see you are leaving us...");
    printf("%s\n","payrent\t thanks for being our loyal tenants...");
    printf("%s\n","info\t get to know our super developer...");
    printf("%s\n","IAmTheBoss\t to exit this system...");

   	printf("type 0 to return to main system:");
	Fgets(cmdline, MAXLINE, stdin);

	if(feof(stdin))
		exit(0);

	while( strcmp(cmdline, "0\n") != 0 ){
	   	printf("type 0 to return to main system:");
		Fgets(cmdline, MAXLINE, stdin);

		if(feof(stdin))
			exit(0);
	}

    exit(EXIT_SUCCESS);
}
Ejemplo n.º 2
0
int main(int argc,char ** argv)
{
	int  n;
	FILE *fp;

	char buff[MAXLINE],command[MAXLINE];


	if(Fgets(buff,MAXLINE,stdin)==NULL)
		err_quit("input error");

	n=strlen(buff);

	if(buff[n-1]=='\n')
		buff[n-1]=0;
	snprintf(command,sizeof(command),"cat %s",buff);

	fp=Popen(command,"r");

	while(Fgets(buff,MAXLINE,fp)!=NULL)
		Fputs(buff,stdout);

	Pclose(fp);
	exit(0);

}
Ejemplo n.º 3
0
Archivo: str.c Proyecto: 4179e1/misc
void
str_cli (FILE *fp, int sockfd) {
	int maxfdp1;
	fd_set rset;
	char sendline[MAXLINE], recvline[MAXLINE];

	FD_ZERO(&rset);
	for (;;) {
		FD_SET (fileno(fp), &rset);
		FD_SET (sockfd, &rset);
		maxfdp1 = MAX (fileno(fp), sockfd) + 1;

		Select (maxfdp1, &rset, NULL, NULL, NULL);

		if (FD_ISSET (sockfd, &rset)) {
			if (Readline (sockfd, recvline, MAXLINE) == 0) {
				err_quit ("str_cli: server terminated prematurely");
			}
			Fputs (recvline, stdout);
		}

		if (FD_ISSET (fileno(fp), &rset)) {
			if (Fgets (sendline, MAXLINE, fp) == NULL) {
				return;
			}
			Writen (sockfd, sendline, strlen(sendline));
		}
	}
}
Ejemplo n.º 4
0
int ftp_command(char *buf,int success,FILE *out,char *fmt,...)
{
    va_list va;
    char line[1200];
    int val;

    va_start(va,fmt);
    vsprintf(line,fmt,va);
    va_end(va);

    if (write(fileno(out),line,strlen(line)) < 0)
        return(-1);

    bzero(buf,200);
    while(1) {
        Fgets(line,sizeof(line),out);
#ifdef DEBUG
        printf("%s",line);
#endif
        if (*(line+3)!='-') break;
    }
    strncpy(buf,line,200);
    val = atoi(line);
    if (success != val) return(-1);
    return(1);
}
Ejemplo n.º 5
0
/*
signal occur must in recvfrom ,or it must be a dead cycle
*/
void dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen){
	int n;
	const in on = 1;
	char sendline[MAXLINE+1],recvline[MAXLINE+1];
	socklen_t len;
	struct sockaddr_in *preply_addr;
	preply_addr = malloc(sizeof(struct sockaddr_in));
	bzero(preply_addr);
	SetSockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
	Signal(SIGALAM,recvfrom_alarm);
	len = servlen;
	while(Fgets(sendline,MAXLINE,fp) != NULL){
		sendto(sockfd,sendline,strlen(sendline),0,pservaddr,len);
		alarm(5);
		for(;;){
			len=servlen;
			n = recvfrom(sockfd,recvlien,MAXLINE,0,preply_addr,&len);
			if(n < 0){
				if(errno == EINTR)
					break;
				else
					err_quit("recvfrom error!");
			}else{
				recvfrom[n] = 0;
				printf("from %s : %s",inet_ntop(preply_addr->sin_family,preply_addr->sin_addr.s_addr,preply_addr->sin_len),recvfrom);
			}
		}
	}
}
int main(int argc, char **argv) 
{
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
	fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
	exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while (Fgets(buf, MAXLINE, stdin) != NULL) {
    printf("client get: %s\n",buf);
	Rio_writen(clientfd, buf, strlen(buf));
    printf("client buf: %s\n",buf);
	Rio_readlineb(&rio, buf, MAXLINE);
	Fputs(buf, stdout);
    }
    
//    printf("outside\n");
    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}
Ejemplo n.º 7
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int	n;
	char	sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t		len;
	struct sockaddr	*preply_addr;

	preply_addr = Malloc(servlen);

	while (Fgets(sendline, MAXLINE, fp) != NULL) {

		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

		len = servlen;
		n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
		if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) {
			printf("reply from %s (ignored)\n",
					Sock_ntop(preply_addr, len));
			continue;
		}

		recvline[n] = 0;	/* null terminate */
		Fputs(recvline, stdout);
	}
}
Ejemplo n.º 8
0
void client(int readfd, int writefd)
{
	size_t len;
	ssize_t n;
	char *ptr;
	struct mymesg mesg;

	//start buffer with pid and a blank
	snprintf(mesg.mesg_data, MAXMESGDATA, "%ld", (long) getpid());
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;

	// read pathname
	Fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	// write PID and pathname to IPC channed
	mesg_send(writefd, &mesg);

	// read from IPC, write to standard output
	mesg.mesg_type = getpid();
	while ((n = mesg_recv(readfd, &mesg)) > 0)
		Write(STDOUT_FILENO, mesg.mesg_data, n);
}
Ejemplo n.º 9
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
    int	n;
    char	sendline[MAXLINE], recvline[MAXLINE + 1];

    Signal(SIGALRM, sig_alrm);

    while (Fgets(sendline, MAXLINE, fp) != NULL)
    {

        Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

        alarm(5);
        if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0)
        {
            if (errno == EINTR)
                fprintf(stderr, "socket timeout\n");
            else
                err_sys("recvfrom error");
        }
        else
        {
            alarm(0);
            recvline[n] = 0;	/* null terminate */
            Fputs(recvline, stdout);
        }
    }
}
Ejemplo n.º 10
0
void interaction::traceFname() {
    FILE* fFname = fopen(ipHostFname,"r");
    if(fFname==NULL) {
        sys_err("call to fopen() failed");
    }
    // read till the end of file
    char* pch;
    const char* delim=" ";
    while(Fgets(ipHostFname, BUFSIZE, fFname)==1) {
        //---------------------------------
        // remove '\n' at the end of string
        //---------------------------------
        ipHostFname[strlen(ipHostFname)-1]=0;
        pch = strtok(ipHostFname,delim);
        //--------------------------------------------------------
        //This magic number 'ipHostFname+strlen(ipHostFname)+1' is
        //the address of strings followed 'traceroute'
        //--------------------------------------------------------
        char* magic = ipHostFname+strlen(ipHostFname)+1;
        if(strcmp(pch,"traceroute")==0) {
            // check if traceroute 'me'
            if(strcmp(magic,"me")==0) {
                traceMe();
            } else {
                traceIpHost(magic);
            }
        } else {
            dprintf(connfd, "INVALID INPUT in file: %s %s", ipHostFname, magic);
        }
    }
    fclose(fFname);
}
Ejemplo n.º 11
0
void
server(int readfd, int writefd)
{
	FILE	*fp;
	ssize_t	n;
	struct mymesg	mesg;

		/* 4read pathname from IPC channel */
	mesg.mesg_type = 1;
	if ( (n = Mesg_recv(readfd, &mesg)) == 0)
		err_quit("pathname missing");
	mesg.mesg_data[n] = '\0';	/* null terminate pathname */

	if ( (fp = fopen(mesg.mesg_data, "r")) == NULL) {
			/* 4error: must tell client */
		snprintf(mesg.mesg_data + n, sizeof(mesg.mesg_data) - n,
				 ": can't open, %s\n", strerror(errno));
		mesg.mesg_len = strlen(mesg.mesg_data);
		Mesg_send(writefd, &mesg);

	} else {
			/* 4fopen succeeded: copy file to IPC channel */
		while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) {
			mesg.mesg_len = strlen(mesg.mesg_data);
			Mesg_send(writefd, &mesg);
		}
		Fclose(fp);
	}

		/* 4send a 0-length message to signify the end */
	mesg.mesg_len = 0;
	Mesg_send(writefd, &mesg);
}
Ejemplo n.º 12
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int n;
	const int on = 1;
	char sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t len;
	struct sockaddr *preply_addr;

	preply_addr = (struct sockaddr *)Malloc(servlen);

	Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

	Signal(SIGALRM, recvfrom_alarm);

	while (Fgets(sendline, MAXLINE, fp) != NULL)
	{
		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
		alarm(5);
		for ( ; ; )
		{
			if (sigsetjmp(jmpbuf, 1) != 0)
				break;
			len = servlen;
			n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
			recvline[n] = 0;
			printf("from %s: %s", (char *)Sock_ntop_host(preply_addr, len), recvline);
		}
	}
	free(preply_addr);
}
Ejemplo n.º 13
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int	icmpfd, maxfdp1;
	char	sendline[MAXLINE], recvline[MAXLINE + 1];
	fd_set	rset;
	ssize_t	n;
	struct timeval tv;
	struct icmpd_err icmpd_err;
	struct sockaddr_un sun;

	Sock_bind_wild(sockfd, pservaddr->sa_family);
	icmpfd = Socket(AF_LOCAL, SOCK_STREAM, 0);
	sun.sun_family = AF_LOCAL;
	strcpy(sun.sun_path, ICMPD_PATH);
	Connect(icmpfd, (SA *)&sun, sizeof(sun));
	Write_fd(icmpfd, "1", 1, sockfd);
	n = Read(icmpfd, recvline, 1);
	if (n != 1 || recvline[0] != '1')
		err_quit("error creating icmp socket, n = %d, char = %c",
				n, recvline[0]);
	FD_ZERO(&rset);
	maxfdp1 = max(sockfd, icmpfd) + 1;

	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		Sendto(sockfd, sendline, strlen(sendline), 0, 
				pservaddr, servlen);

		tv.tv_sec = 5;
		tv.tv_usec = 0;
		FD_SET(sockfd, &rset);
		FD_SET(icmpfd, &rset);
		if ((n = Select(maxfdp1, &rset, NULL, NULL, &tv)) == 0) {
			fprintf(stderr, "socket timeout\n");
			continue;
		}

		if (FD_ISSET(sockfd, &rset)) {
			n = Recvfrom(sockfd, recvline, MAXLINE, 0, 
					NULL, NULL);
			recvline[n] = 0;
			Fputs(recvline, stdout);
		}

		if (FD_ISSET(icmpfd, &rset)) {
			if ((n = Read(icmpfd, &icmpd_err, 
					sizeof(icmpd_err))) == 0)
				err_quit("ICMP daemon terminate");
			else if (n != sizeof(icmpd_err))
				err_quit("n = %d, expected %d",
					n, sizeof(icmpd_err));
			printf("ICMP error: dest = %s, %s, type = %d,"
				" code = %d\n",
				Sock_ntop((SA *)&icmpd_err.icmpd_dest,
					icmpd_err.icmpd_len),
				strerror(icmpd_err.icmpd_errno),
				icmpd_err.icmpd_type, 
				icmpd_err.icmpd_code);
		}
	}
}
Ejemplo n.º 14
0
void readcommand()
{
        // Allocate userpass char array
        int i;
        for(i = 0; i < 128; i++)
	{
            execcom[i] = malloc(128);
                
        }

        FILE *file;
        char line[128];
        i = 0;

        file = fopen("rrshcommands.txt", "r");
        if (file)
        {
                while (Fgets(line, sizeof(line), file) != NULL)
                {
			strtok(line, "\n");
                        strcpy(execcom[i], line);
                        i++;
                }
                fclose(file);
        }

}
Ejemplo n.º 15
0
void readconfig()
{
	// Allocate userpass char array
	int i;
	int j;
        for(i=0; i<2; i++ ){
                for(j=0; j<128; j++){
                        userpass[i][j] = malloc(40);
                }
         }

        FILE *file;
	char line[128];
	int c = 0;
	char *username;
	char *password;

        file = fopen("rrshusers.txt", "r");
        if (file) 
	{
                while (Fgets(line, sizeof(line), file) != NULL)
		{
			char *str = line;
			username = strtok(str, " ");
			password = strtok(NULL, " ");			

			strtok(password, "\n");
			strcpy(userpass[0][c], username);
			strcpy(userpass[1][c], password);	
			c++;
                }
                fclose(file);
        }

}
Ejemplo n.º 16
0
void command(void)
{
    char buf[MAXLINE];
    if (Fgets(buf, MAXLINE, stdin) == NULL)
        exit(0);
    Fputs(buf, stdout);
}
Ejemplo n.º 17
0
static void
CheckConfFileHeader (const char sMagic[], const char sFile[], FILE *pFile) // in
{
char s[SLEN];
Fgets(s, SLEN-1, pFile);

int n = static_cast<int>(strlen(sMagic));
if (strncmp(s, sMagic, n) != 0) // check CONF file magic number
    {
    // truncate s at the first white space or after 20 chars
    int i;
    for (i = 0; i < n+1 && i < 20; i++)
        if (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')
            break;
    s[i] = 0;
    Err("%s is not a %s config file\n"
        "       First part of %s is \"%s\", expected \"%s%d\"",
        sFile, sMagic, sFile, s, sMagic, CONF_FILE_VERSION);
    }
int nVersion;
if (sscanf(s+n, "%d", &nVersion) != 1)
    Err("%s has a bad header (could not read version number)", sFile);

if (nVersion != CONF_FILE_VERSION)
    Err("%s has a bad version number %d (expected %d)",
        sFile, nVersion, CONF_FILE_VERSION);
}
Ejemplo n.º 18
0
void str_cli(FILE *fp, int fd)
{
    char readline[MAXLINE], recvline[MAXLINE];
    fd_set fdset;
    int max_fd;

    for ( ; ; ) {
        FD_ZERO(&fdset);
        FD_SET(fd, &fdset);
        FD_SET(fileno(fp), &fdset);
        max_fd = max(fd, fileno(fp)) + 1;
        select(max_fd, &fdset, NULL, NULL, NULL);
        
        if (FD_ISSET(fd, &fdset)) {
            if (Readline(fd, recvline, sizeof(recvline)) == 0) {
                err_quit("str_cli: server terminated");
            }
            Fputs(recvline, stdout);
        }

        if (FD_ISSET(fileno(fp), &fdset)) {
            if (Fgets(readline, sizeof(readline), fp) == NULL) {
                return;
            }
            Writen(fd, readline, strlen(readline));
        }
    }
}
Ejemplo n.º 19
0
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int				n;
	const int		on = 1;
	char			sendline[MAXLINE], recvline[MAXLINE + 1];
	socklen_t		len;
	struct sockaddr	*preply_addr;
	preply_addr = Malloc(servlen);
	Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); //告诉内核广播
	Signal(SIGALRM, recvfrom_alarm);
	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
		alarm(5);
		for ( ; ; ) {
			len = servlen;
			n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
			if (n < 0) {
				if (errno == EINTR)
					break;		/* waited long enough for replies */
				else
					err_sys("recvfrom error");
			} else {
				recvline[n] = 0;	/* null terminate */
				printf("from %s: %s",
						Sock_ntop_host(preply_addr, len), recvline);
			}
		}
	}
	free(preply_addr);
}
Ejemplo n.º 20
0
void str_cli(FILE *fp, int sockfd)
{
	int	maxfdp1;
	fd_set	rset;
	char	sendline[MAXLINE], recvline[MAXLINE];

	FD_ZERO(&rset);
	for ( ; ; ) {
		FD_SET(fileno(fp), &rset);
		FD_SET(sockfd, &rset);
		maxfdp1 = max(fileno(fp), sockfd) + 1;
		Select(maxfdp1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(sockfd, &rset)) { /* sockfd is readable */
			if (Readline(sockfd, recvline, MAXLINE) == 0)
				err_quit("str_cli: server terminated prematruely");
			Fputs(recvline, stdout);
		}

		if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
			if (Fgets(sendline, MAXLINE, fp) == NULL)
				return;	/* all done */
			Writen(sockfd, sendline, strlen(sendline));
		}
	}
}
Ejemplo n.º 21
0
/*
 * main - shell main routine 
 */
int main(int argc, char **argv) 
{
    char cmdline[MAXLINE]; /* command line */

    if (argc > 2) 
	usage();
    if (argc == 2) {
	if (!strcmp(argv[1], "-v")) 
	    verbose = 1;
	else 
	    usage();
    }

    Signal(SIGINT, sigint_handler);   /* ctrl-c */
    Signal(SIGCHLD, sigchld_handler); 
    Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */

    initjobs(jobs);

    /* read/eval loop */
    while (1) {
	/* read command line */
	printf("%s", prompt);                   
	Fgets(cmdline, MAXLINE, stdin); 
	if (feof(stdin))
	    exit(0);

	/* evaluate command line */
	eval(cmdline);
    } 

    exit(0);
}
Ejemplo n.º 22
0
void * keyboard(void * vargp) {
    char buf[MAXLINE];
    rio_t rio;

    fprintf(stderr,"fprintf 7\n");

    int clientfd = *((int*) vargp);

    fprintf(stderr,"fprintf 8\n");

    Rio_readinitb(&rio, clientfd);

    while(Fgets(buf, MAXLINE, stdin) != NULL) {
        fprintf(stderr,"fprintf 9\n");
        if(active)
            Rio_writen(clientfd, buf, strlen(buf));
        else
            break;
    }

    if(active) {
        fprintf(stderr,"fprintf 10\n");
        strcpy(buf, "$q\n");
        Rio_writen(clientfd, buf, strlen(buf));
    }

    return NULL;
}
Ejemplo n.º 23
0
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
	int				n;
	char			sendline[MAXLINE], recvline[MAXLINE + 1];
	struct timeval	tv;

	tv.tv_sec = 5;
	tv.tv_usec = 0;
	Setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));

	while (Fgets(sendline, MAXLINE, fp) != NULL) {

		Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

		n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
		if (n < 0) {
			if (errno == EWOULDBLOCK) {
				fprintf(stderr, "socket timeout\n");
				continue;
			} else
				err_sys("recvfrom error");
		}

		recvline[n] = 0;	/* null terminate */
		Fputs(recvline, stdout);
	}
}
Ejemplo n.º 24
0
void command(void)
{
    char buf[MAXLINE];
    if (!Fgets(buf, MAXLINE, stdin))
        exit(0);                /* EOF */
    printf("%s", buf);          /* Process the input command */
}
Ejemplo n.º 25
0
/* cat_libfile:
 * Write library files onto the given file pointer.
 * arglib is an NULL-terminated array of char*
 * Each non-trivial entry should be the name of a file to be included.
 * stdlib is an NULL-terminated array of char*
 * Each of these is a line of a standard library to be included.
 * If any item in arglib is the empty string, the stdlib is not used.
 * The stdlib is printed first, if used, followed by the user libraries.
 * We check that for web-safe file usage.
 */
void cat_libfile(FILE * ofp, char **arglib, char **stdlib)
{
    FILE *fp;
    char *p, **s, *bp;
    int i, use_stdlib = TRUE;

    /* check for empty string to turn off stdlib */
    if (arglib) {
	for (i = 0; use_stdlib && ((p = arglib[i])); i++) {
	    if (*p == '\0')
		use_stdlib = FALSE;
	}
    }
    if (use_stdlib)
	for (s = stdlib; *s; s++) {
	    fputs(*s, ofp);
	    fputc('\n', ofp);
	}
    if (arglib) {
	for (i = 0; (p = arglib[i]) != 0; i++) {
	    if (*p == '\0')
		continue;	/* ignore empty string */
	    p = safefile(p);	/* make sure filename is okay */
	    if ((fp = fopen(p, "r"))) {
		while ((bp = Fgets(fp)))
		    fputs(bp, ofp);
	    } else
		agerr(AGWARN, "can't open library file %s\n", p);
	}
    }
}
Ejemplo n.º 26
0
void
client(int readid, int writeid)
{
	size_t	len;
	ssize_t	n;
	char	*ptr;
	struct mymesg	mesg;

	/* start buffer with msqid and a blank */
	snprintf(mesg.mesg_data, MAXMESGDATA, "%d ", readid);
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;

	/* read pathname */
	Fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;				/* delete newline from fgets() */
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	/* write msqid and pathname to server's well-known queue */
	Mesg_send(writeid, &mesg);

	/* read from our queue, write to standard output */
	while ( (n = Mesg_recv(readid, &mesg)) > 0)
		Write(STDOUT_FILENO, mesg.mesg_data, n);
}
Ejemplo n.º 27
0
void dg_cli(FILE* fp, int sockfd, const SA* pservaddr, socklen_t servlen)
{
    int n;
    char sendline[MAXLINE], recvline[MAXLINE + 1];
    socklen_t len;
    struct sockaddr* preply_addr;

    preply_addr = Malloc(servlen);

    Signal(SIGALRM, recvfrom_alarm);

    while (Fgets(sendline, MAXLINE, fp) != NULL) {
        Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);

        alarm(5);
        for (;;) {
            if (sigsetjmp(jmpbuf, 1) != 0) break;
            len = servlen;
            n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
            recvline[n] = 0; /* null terminate */
            printf("from %s: %s", Sock_ntop_host(preply_addr, servlen),
                   recvline);
        }
    }
}
Ejemplo n.º 28
0
int main(int argc, char *argv[])
{
    int clientfd, port;
    char *host, buf[MAXLINE];
    rio_t rio;

    /*
    if(argc!= 3)
    {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(1);
    }
    */

    //host = argv[1];
    //port = atoi(argv[2]);
    host ="127.0.0.1";
    port=8080;

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while(Fgets(buf, MAXLINE, stdin) != NULL)
    {
        Rio_writen(clientfd, buf, strlen(buf));
        Fputs(buf, stdout);
    }
    close(clientfd);
    exit(0);
}
Ejemplo n.º 29
0
int echoClient(int argc, char** argv) {
    int clientfd, port;
    char* host, buf[MAXLINE];
    rio_t rio;

    if (argc != 3) {
        fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
        exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = Open_clientfd(host, port);
    Rio_readinitb(&rio, clientfd);

    while (Fgets(buf, MAXLINE, stdin) != NULL) {
        Rio_writen(clientfd, buf, strlen(buf));
        // get response from server
        Rio_readlineb(&rio, buf, MAXLINE);
        //Fputs(buf, stdout);
    }

    Close(clientfd);
    exit(0);
}
Ejemplo n.º 30
0
void client(int readid, int writeid)
{
	size_t len;
	ssize_t n;
	char *ptr;
	struct mymesg mesg;

	//start buffer with msqid and a blank
	snprintf(mesg.mesg_data, MAXMESGDATA, "%d ", readid);
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;

	//read pathname
	Fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	//write msqid and pathname to server well-know queue
	mesg_send(writeid, &mesg);

	//read from our queue, wirte to standard output
	while ((n = mesg_recv(readid, &mesg)) > 0)
		Write(STDOUT_FILENO, mesg.mesg_data, n);
}