Socket communication ignores (skips) if statements

Asked 1 years ago, Updated 1 years ago, 401 views

We use socket communication to create simple games for multiple players.
I would like to use strncmp to determine the string sent by the client so that the appropriate action can be taken.

For example, if the client sends "ADD5", I would like to use strncmp to determine the first ADD and add 5 to the total number.

However, the if statement will not be processed.After a lot of experiments, the client sends a string to the server without any problems, so I'm sure there's a problem with the if statement.

Is there a possibility that the if statement processing is skipped because the client cannot keep up with the processing?I would appreciate it if you could let me know if there is any solution.Sorry for my poor writing.

Server Side

int sum=0;
  int tmp;
  while(1)
  {
    for (inti=0;i<num_player;i++)
    {
      memset(client_buf, '\0', BUF_SIZE);
      
      if(recv(clientsock[i], client_buf, BUF_SIZE, 0)==-1)
        {
            error("ERROR");
            exit(1);
        }  

      if(strncmp(client_buf, "ADD", 3) == 0)
      {
        strcpy(str, client_buf+4);
        
        sum+=temp;
      }
      // Other else if statements follow.
 }

Client Side

while(1){
        memset(client_buf, '\0', sizeof(client_buf)));
        if(recv(clientsock, server_reply, BUF_SIZE, 0) == -1)
        {
            error("ERROR");
            exit(1);
        }  
        char str [BUF_SIZE]
        printf("Enter a number:");
        fgets(str,BUF_SIZE,stdin);
        sprintf(client_buf, "ADD%s", str);
        if(send(clientsock, client_buf, BUF_SIZE, 0)<0){
             error("Send failed";
        }
  }

c socket

2022-09-30 22:05

2 Answers

There are many logical leaps in the questionnaire.As discussed below, it is recommended that you investigate based on specific facts.

After a lot of experiments, the client sends the string to the server without any problems.

No, it's not.What the client sends and what the server receives are two separate issues.It's not well known, but if the client sends it, it means the client sent it, so determine whether the server received it or not based on the server received it.(Koizumi Syntax)
For example,

 if (recv(clientsock[i], client_buf, BUF_SIZE, 0)==-1)

recv() did not return -1, so it does not appear to be a reception error, but how many bytes of data did you receive? There is no receive error and the expected number of bytes received is another issue.I can't tell because the information has been read out of this code.

Similarly

 if(send(clientsock, client_buf, BUF_SIZE, 0)<0){

The client seems to have successfully sent it, but how many bytes of data did they send? It says that the client sends the string to the server without any problems, but it is premature to judge that the transmission was successful without checking the number of bytes sent.

Is there a case where if statement processing is skipped because the client cannot keep up with the processing?

Which line does the server program run in the first place? The question statement does not have it. Not processing the if statement and skipping the if statement are two different issues.In order to determine that it has been skipped, we need the information that if the action at the end of the statement has been performed.


2022-09-30 22:05

int sum=0;
int tmp;
and variable definitions.

sum+=temp;

What's the spelling error line?


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.