2008年11月29日 星期六

Midterm ex1

Please a design a simple calculator client and server such that the client sends an expression (e.g."123+456), which contains two operands and an operator, to the server. After received the expression, the server replies the caculating result to the client. Note that, the operands are 3-digit integer, the operator is in {+,-,*,/,%}, the expression is inputted from the keyboard, and the caculating result should be shown on the console of the client.

//---------------------------------------------------------------------------------------------
//tcpcli01.c
#include "unp.h"

void inexp(FILE *fp, int sockfd)
{
 char sendline[MAXLINE], recvline[MAXLINE];

 do {
  printf("Please enter an expression (e.g. 123+456):\n");
  if (Fgets(sendline, MAXLINE, fp) != NULL) {
   Writen(sockfd, sendline, strlen(sendline));
   if (Readline(sockfd, recvline, MAXLINE) == 0)
    err_quit("inexp: server terminated prematurely");
   Fputs(recvline, stdout);
  }
 } while (strlen(recvline)>10);
}

int main(int argc, char **argv)
{
 int sockfd;
 struct sockaddr_in servaddr;

 if (argc != 2)
  err_quit("usage: tcpcli ");

 sockfd = Socket(AF_INET, SOCK_STREAM, 0);

 bzero(&servaddr, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_port = htons(SERV_PORT);
 Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

 Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

 inexp(stdin, sockfd); /* do it all */

 exit(0);
}
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
//tcpserv01.c
#include "unp.h"

void retrt(int sockfd)
{
 char buf[MAXLINE], rep[MAXLINE], op;
 int a, b;
 int success=0;

 do
 {
  a=b=0;
  if (read(sockfd, buf, MAXLINE)>0)
  {
   printf("Received expression: %s\n",buf);
   if (sscanf(buf, "%d%c%d",&a, &op, &b)!=3)
    snprintf(rep, sizeof(rep), "Invalid input: %s\n", buf);
   else if (a>=100 && a<=999 && b>=100 && b<=999)
   {
    switch(op)
    {
     case '+': snprintf(rep, sizeof(rep), "%d\n", a+b); success=1;
     break;
     case '-': snprintf(rep, sizeof(rep), "%d\n", a-b); success=1;
     break;
     case '*': snprintf(rep, sizeof(rep), "%d\n", a*b); success=1;
     break;
     case '/': snprintf(rep, sizeof(rep), "%d\n", a/b); success=1;
     break;
     case '%': snprintf(rep, sizeof(rep), "%d\n", a%b); success=1;
     break;
     default: snprintf(rep, sizeof(rep), "Invalid operator: %c\n", op);
    }
   }
   else snprintf(rep, sizeof(rep), "Invalid number: %d or %d\n", a, b);
   Writen(sockfd, rep, strlen(rep));
  }
 } while(success==0);
}

int main(int argc, char **argv)
{
 int listenfd, connfd;
 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(SERV_PORT);

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

 Listen(listenfd, LISTENQ);

 for ( ; ; ) {
  connfd = Accept(listenfd, (SA *) NULL, NULL);
  retrt(connfd); /* process the request */
  Close(connfd);
 }
}
//---------------------------------------------------------------------------------------------

沒有留言: