Example #1
0
int main() {
    //The following code are provided by the Lab Slides or from TA
    int serverSockFd, clientSockFd, pid;
    socklen_t serverLen, clientLen;
    struct sockaddr_in serverAddress;
    struct sockaddr_in clientAddress;
    struct sockaddr_storage addr;
	serverLen = sizeof addr;
    serverSockFd = socket(AF_INET, SOCK_STREAM, 0);
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddress.sin_port = htons(SERVER_PORT_NUM);
    serverLen = sizeof(serverAddress);
    clientLen = sizeof(clientAddress);
    bind(serverSockFd, (struct sockaddr *)&serverAddress, serverLen);
    listen(serverSockFd, BACKLOGS);
    while (TRUE) {
		printf("\nWaiting for clients...\n");
		clientSockFd = accept(serverSockFd, (struct sockaddr *)&clientAddress, &clientLen);
		//TEST FOR GET IP of the clients
		getpeername(clientSockFd, (struct sockaddr*)&addr, &serverLen);
    	struct sockaddr_in *s = (struct sockaddr_in *)&addr;
    	inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
    	printf("\nCustomer IP: %s\n", ipstr);
    	//TEST FOR GET IP of the clients
		if (clientSockFd == -1) {			//check client available
			printf("\nAccept Error\n");
			exit(1);
		}
        pid = fork();		//create threads
        printf("\nCreating new thread...\n");
    	if (pid < 0)
            printf("\nFork Error\n");
        if (pid == 0)  {
            close(serverSockFd);
            workout(clientSockFd);	//target thread workout
            exit(0);
        }
        else close(clientSockFd);   //just kick out
     }
    close(serverSockFd); //usually never reach here
    return 0;
}
Example #2
0
int main(int argc, char *argv[])
{
	double wores;
	DWORD res;
	int i;

	if (argc <= 1)
	{
		CUIPutS(
			"参数:表达式1 [表达式2] [表达式3] ...\n"
			"表达式为标准的数学表达式\n"
			"可使用括号,如(1+2)*abs(-3)或sin(3.14)\n"
			"可使用二元运算:加+ 减- 乘* 除/ 整数模% 实数模# 幂^\n"
			"可使用函数如下:\n"
			"绝对值abs 反余弦acs 反正弦asn 反正切atn 余弦cos 双曲余弦ch e的幂exp\n"
			"常用对数lg 自然对数ln 正弦sin 双曲正弦sh 开方sqr 正切tan 双曲正切th\n"
		);
		return NO_ERROR;
	}
	for (i = 1; i < argc; i++)
	{
		char buf[128], *bufp;

		bufp = strcpy(buf, argv[i]) - 1;
		if ((res = workout(argv[i], NULL, NULL, 0, &wores)) != WOERR_NOERR)
		{
			bufp = strcpy(bufp, "无法计算:") - 1;
			bufp = strcpy(bufp, WoErrStr[res]) - 1;
			strcpy(bufp, "\n");
		}
		else
		{
			bufp = strcpy(bufp, " = ") - 1;
			bufp = ftoa(bufp, wores);
			strcpy(bufp, "\n");
		}
		CUIPutS(buf);
	}
	return NO_ERROR;
}