#include#define PORT 5000
typedef struct{
double a;
double b;
char s[10];
char echo_buf[1024];
}Mes;
int main(int argc,char *argv[])
{
int connect_fd;
int ret;
char recv_buf[1024];
int i;
int port;
int len;
static struct sockaddr_in srv_addr;
if(argc!=1){
printf("Usage: %s server_ip_address port \n",argv[0]);
exit(1);
}
connect_fd = socket(PF_INET,SOCK_STREAM,0);
if(connect_fd < 0){
perror("can not create communication socket\n");
exit(1);
}
memset(&srv_addr,0,sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
srv_addr.sin_port = htons(PORT);
if((connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr)))==-1){
perror("can not to the server\n");
close(connect_fd);
exit(1);
}
memset(recv_buf,0,1024);
while(1){
Mes mes;
int n=sizeof(mes);
printf("请输入表达式:\n");
scanf("%s",mes.s);
if(strcmp(mes.s,"echo")==0){
fgets(mes.echo_buf,1024,stdin);
mes.a = 0;
mes.b = 0;
}else{
scanf("%lf%lf",&mes.a,&mes.b);
sprintf(mes.echo_buf,"%s","NULL");
}
write(connect_fd,&mes,n);
len = read(connect_fd,recv_buf,n);
if(strcmp(mes.s,"echo")==0){
printf("服务端回复的消息:%s\n",recv_buf);
}else if(len > 0){
double result;
sscanf(recv_buf,"%lf",&result);
printf("计算的结果: %.2lf\n",result);
if(recv_buf[0] == '@')
break;
}
}
close(connect_fd);
return 0;
}