如何使用 C 在 BSD 套接字中处理 3 路 send() 和 recv()
How to handle 3 way send() and recv() in BSD socket using C
发送 "wrong" 用户名后 - 客户端不会从头开始循环,实际上,没有 server asks:?
不知道如何处理这种身份验证的 3 路客户端-服务器消息发送方。我必须了解这一点才能继续进一步接收此类消息。
client.c:
int is_authenticated = 0;
size_t sendline_s;
while (!is_authenticated) {
recv(sockfd, recvline, MAXLINE, 0);
printf("%s", "server asks:");
fputs(recvline, stdout);
printf("?> ");
fflush(stdout);
while (fgets(sendline, MAXLINE, stdin) != NULL) {
sendline_s = strlen(sendline);
if (sendline[sendline_s-1] == '\n') {
sendline[sendline_s-1] = '[=10=]';
send(sockfd, sendline, sendline_s+1, 0);
puts("username sended");
break;
}
// handling ^Z (EOF) here
//
}
recv(sockfd, recvline, MAXLINE, 0);
printf("\nawaiting for server ACK\n");
puts(recvline);
if (strcmp(recvline, "ACCEPTED_AUTH") == 0) {
puts("authentication complete successful");
is_authenticated = 1;
}
else {
puts("authentication declined");
}
}
server.c
int is_authenticated = 0;
char *accepted = "ACCEPTED_AUTH";
char *name = "kaldown";
char *wrong = "wrong";
size_t name_s = strlen(name);
size_t accepted_s = strlen(accepted);
size_t wrong_s = strlen(wrong);
while (!is_authenticated) {
send(connfd, name, name_s+1, 0);
puts("authentication request was send");
recv(connfd, buf, MAXLINE, 0);
printf("username was recieved: ");
puts(buf);
if (strcmp(buf, name) == 0) {
puts("hurray");
send(connfd, accepted, accepted_s+1, 0);
is_authenticated = 1;
//break;
}
else {
puts("WRONG NAME");
send(connfd, wrong, wrong_s+1, 0);
}
}
但是,如果我发送正确的用户名 - 它会通过阻止,一切顺利。
服务器:
while (!is_authenticated) {
if ((n = recv(connfd, buf, MAXLINE-1,0))== -1) {
perror("recv");
exit(1);
}
else if (n == 0) {
printf("Connection closed\n");
//So I can now wait for another client
break;
}
printf("\n%d truely recieved", n);
buf[n] = '[=10=]';
printf("Server:Msg Received %s\n", buf);
if (strcmp(buf, "DONE") == 0) {
strcpy(buf, "DONE");
if ((send(connfd,buf, strlen(buf),0))== -1)
{
fprintf(stderr, "Failure Sending Message\n");
close(connfd);
break;
}
puts("KONEC");
is_authenticated = 1;
}
else {
if ((send(connfd,buf, strlen(buf),0))== -1)
{
fprintf(stderr, "Failure Sending Message\n");
close(connfd);
break;
}
}
printf("Server:Msg being sent: %s\nNumber of bytes sent: %d\n", buf, strlen(buf));
}
客户:
while (!is_authenticated) {
printf("Client: Enter Data for Server:\n");
if (fgets(sendline, MAXLINE-1, stdin) != NULL) {
if (sendline[(strlen(sendline)-1)] == '\n') {
sendline[strlen(sendline)-1] = '[=11=]';
if ((send(sockfd,sendline, strlen(sendline),0))== -1) {
fprintf(stderr, "Failure Sending Message\n");
close(sockfd);
exit(1);
}
else {
printf("Client:Message being sent: %s\n",sendline);
n = recv(sockfd, sendline, sizeof(sendline),0);
if ( n <= 0 )
{
printf("Either Connection Closed or Error\n");
//Break from the While
break;
}
sendline[n] = '[=11=]';
if (strcmp(sendline, "DONE") == 0) {
puts("AUTH PASSED");
is_authenticated = 1;
}
printf("Client:Message Received From Server - %s\n",sendline);
}
}
//EOF
}
}
发送 "wrong" 用户名后 - 客户端不会从头开始循环,实际上,没有 server asks:?
不知道如何处理这种身份验证的 3 路客户端-服务器消息发送方。我必须了解这一点才能继续进一步接收此类消息。
client.c:
int is_authenticated = 0;
size_t sendline_s;
while (!is_authenticated) {
recv(sockfd, recvline, MAXLINE, 0);
printf("%s", "server asks:");
fputs(recvline, stdout);
printf("?> ");
fflush(stdout);
while (fgets(sendline, MAXLINE, stdin) != NULL) {
sendline_s = strlen(sendline);
if (sendline[sendline_s-1] == '\n') {
sendline[sendline_s-1] = '[=10=]';
send(sockfd, sendline, sendline_s+1, 0);
puts("username sended");
break;
}
// handling ^Z (EOF) here
//
}
recv(sockfd, recvline, MAXLINE, 0);
printf("\nawaiting for server ACK\n");
puts(recvline);
if (strcmp(recvline, "ACCEPTED_AUTH") == 0) {
puts("authentication complete successful");
is_authenticated = 1;
}
else {
puts("authentication declined");
}
}
server.c
int is_authenticated = 0;
char *accepted = "ACCEPTED_AUTH";
char *name = "kaldown";
char *wrong = "wrong";
size_t name_s = strlen(name);
size_t accepted_s = strlen(accepted);
size_t wrong_s = strlen(wrong);
while (!is_authenticated) {
send(connfd, name, name_s+1, 0);
puts("authentication request was send");
recv(connfd, buf, MAXLINE, 0);
printf("username was recieved: ");
puts(buf);
if (strcmp(buf, name) == 0) {
puts("hurray");
send(connfd, accepted, accepted_s+1, 0);
is_authenticated = 1;
//break;
}
else {
puts("WRONG NAME");
send(connfd, wrong, wrong_s+1, 0);
}
}
但是,如果我发送正确的用户名 - 它会通过阻止,一切顺利。
服务器:
while (!is_authenticated) {
if ((n = recv(connfd, buf, MAXLINE-1,0))== -1) {
perror("recv");
exit(1);
}
else if (n == 0) {
printf("Connection closed\n");
//So I can now wait for another client
break;
}
printf("\n%d truely recieved", n);
buf[n] = '[=10=]';
printf("Server:Msg Received %s\n", buf);
if (strcmp(buf, "DONE") == 0) {
strcpy(buf, "DONE");
if ((send(connfd,buf, strlen(buf),0))== -1)
{
fprintf(stderr, "Failure Sending Message\n");
close(connfd);
break;
}
puts("KONEC");
is_authenticated = 1;
}
else {
if ((send(connfd,buf, strlen(buf),0))== -1)
{
fprintf(stderr, "Failure Sending Message\n");
close(connfd);
break;
}
}
printf("Server:Msg being sent: %s\nNumber of bytes sent: %d\n", buf, strlen(buf));
}
客户:
while (!is_authenticated) {
printf("Client: Enter Data for Server:\n");
if (fgets(sendline, MAXLINE-1, stdin) != NULL) {
if (sendline[(strlen(sendline)-1)] == '\n') {
sendline[strlen(sendline)-1] = '[=11=]';
if ((send(sockfd,sendline, strlen(sendline),0))== -1) {
fprintf(stderr, "Failure Sending Message\n");
close(sockfd);
exit(1);
}
else {
printf("Client:Message being sent: %s\n",sendline);
n = recv(sockfd, sendline, sizeof(sendline),0);
if ( n <= 0 )
{
printf("Either Connection Closed or Error\n");
//Break from the While
break;
}
sendline[n] = '[=11=]';
if (strcmp(sendline, "DONE") == 0) {
puts("AUTH PASSED");
is_authenticated = 1;
}
printf("Client:Message Received From Server - %s\n",sendline);
}
}
//EOF
}
}