Unix 网络编程 SOCKET 的问题
程序要求要在客户端循环输入操作符和操作数,在服务器端接收返回结果。
客户端需要多次输入,输入 Q终止操作。
服务器端的一个方法:
void operationServ(FILE *fp, int sockfd)
{
float readline[MAXLINE];
ssize_t n; // store the num of value that pass from the client
int i;
float* ptr;
float temp = 0;
// while( (n = read(sockfd, readline, MAXLINE)) > 0)
// {
again:
n = read(sockfd, readline, MAXLINE);
n = n / sizeof(float); // get the number of the values
printf("-----------------result = %d\n", (int)n);
switch((int)readline[0])
{
case 1:
{
// do some additions
temp = 0;
for(i = 2; i < n ; i++)
{
temp = readline[i] + temp;
}
printf("result = %f", temp);
write(sockfd, &temp, sizeof(float));
}
break;
case 2:
{
// do some subtractions
temp = 0;
for(i = 2; i < n; i++)
{
temp = temp - readline[i];
}
write(sockfd, &temp, sizeof(float));
}
break;
case 3:
{
// do some multiplications
temp = 1;
for(i = 2; i < n; i++)
{
temp = temp * readline[i];
}
write(sockfd, &temp, sizeof(float));
}
break;
case 4:
{
// do some divisions
temp = 1;
for(i = 2; i < n; i++)
{
temp = temp / readline[i];
}
write(sockfd, &temp, sizeof(float));
}
break;
default:
return;
break;
}
printf("\nresult = %d",(int)n);
goto again;
// }
}
单次的时候可用,但是用了GOTO做循环测试的时候就不可以用了。
请问为什么,如何解决?