我正在用 C 编写一个基于套接字的应用程序,它需要接受用户输入并根据它们执行各种任务。该程序还打开了一个服务器,需要等待传入的连接。
这是完成这部分的代码,
while(1)
{
if((sockClient = accept(sockServer, (struct sockaddr*)&client, (socklen_t *)&len)) == -1)
{
perror("Accept failed: ");
exit(-1);
}
else
{
send(sockClient, msg, strlen(msg), 0);
printf("Connected to client : %s\n", inet_ntoa(client.sin_addr));
}
......
......
}
现在在同一个无限循环中,我需要获取用户输入并相应地处理它们。这是执行此操作的代码。
char usrInputStr[256];
printf("Enter Instruction: ");
scanf ("%[^\n]%*c", usrInputStr);
memcpy(cmdHist[histCount], usrInputStr, strlen(usrInputStr));
histCount++;
char** currentTokens = (char**)malloc(sizeof(char*)*256);
for(int i=0; i<256; i++)
{
currentTokens[i] = (char*)malloc(sizeof(char)*256);
}
processInstruction(usrInputStr, currentTokens);
问题在于此功能未按预期运行。当我取消接受调用的代码时,用户输入得到正确处理。谁能告诉我如何重构此代码以使两者协同工作。
最佳答案
关于c - 在同一 while 循环中接受用户输入和传入连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33083665/