对于我的程序段,我收到一个char数组并检查所有字符是否为整数。
当其他程序没有发送一串整数时,例如asdf
,它会捕获它。但是在退出之前它会循环四次循环。
如果第一个字符不是int,我怎么能立即继续这段?我已经尝试了很多东西,但一段时间以来一直困扰着同样的问题。我觉得这很简单。
编辑:我希望它只打印收到的传入连接和无效第一部分。然后从那里继续。
int iRecvB = recv(acceptSocket, recvSerial, STRLEN, 0);
recvSerial[iRecvB] = '\0';
if (iRecvB > 0)
{
for (int n = 0; n < iRecvB; n++)
{
if (recvSerial[n] < '0' || recvSerial[n] > '9')
{
isNumber = false;
}
if (!isNumber)
{
int iSend = send(acceptSocket, "no", 2, 0);
cout << "\nIncoming connection received.\n" << "Data received:" << endl;
cout << "\tSerial: " << recvSerial << endl;
cout << "\t INVALID\n" << endl;
if (iSend == SOCKET_ERROR)
{
cerr << "ERROR: Failed to send message11111\n";
cleanup(acceptSocket);
return 1;
}
}
}// END OF FOR LOOP FOR NUM CHECK
}
else if (iRecvB == 0)
{
cout << "Connection closed\n";
cleanup(acceptSocket);
return 0;
}
else
{
cerr << "ERROR: Failed to receive message\n";
cleanup(acceptSocket);
return 1;
}
这是控制台显示的内容:
Incoming connection received.
Data received:
Serial: asdf
INVALID
Incoming connection received.
Data received:
Serial: asdf
INVALID
Incoming connection received.
Data received:
Serial: asdf
INVALID
ERROR: Failed to send message11111
C:\Users\Admin\Desktop\Activation\ActivationServer\Debug>
答案 0 :(得分:2)
您可以使用break
关键字。
例如:
for (int n = 0; n < iRecvB; n++)
{
if (recvSerial[n] < '0' || recvSerial[n] > '9')
{
isNumber = false;
}
if (!isNumber)
{
int iSend = send(acceptSocket, "no", 2, 0);
cout << "\nIncoming connection received.\n" << "Data received:" << endl;
cout << "\tSerial: " << recvSerial << endl;
cout << "\t INVALID\n" << endl;
if (iSend == SOCKET_ERROR)
{
cerr << "ERROR: Failed to send message11111\n";
cleanup(acceptSocket);
return 1;
}
break; // <-- HERE
}
}// END OF FOR LOOP FOR NUM CHECK