1

I have an issues to convert my BUFFER into a string, I like to know how do I convert my BUFFER, recv from the socket.

I would like to have like my db[0] = buffer, which buffer contain a string like "helloworld", so if i want print db[0], i would get "helloworld".

while(1){
    recv(newSocket, buffer, BUFFER_SIZE, 0);
    if(strcmp(buffer, "q") == 0){
        printf("Deconnexion de %s:%d\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
        break;
    }else{
        printf("%s\n",buffer);//Client recu: 
        send(newSocket, buffer, strlen(buffer), 0);
        bzero(buffer, sizeof(buffer));

        db[i]=buffer;

        printf("%c",db[i]);

        //mysql_q(db[i]);// query function

        i++;
    }               
}

db type is a char array, but when i compile it gives me this error :

server.cpp:81:12: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
  db[i]=buffer;
6
  • 1
    What is the type of db and buffer, please show the definition ? error is quite clear db[i] is a char and buffer a char *. you should change db type Commented Jun 5, 2018 at 10:21
  • what kind of db? char[]? you should not assign char* to char, db[i] is a char. char* db[i] is better. Commented Jun 5, 2018 at 10:21
  • 2
    c++ is not c. Choose a language. Commented Jun 5, 2018 at 10:23
  • this is my db and buffer type : char buffer[BUFFER_SIZE]; char db[BUFFER_SIZE]; @Ôrel Commented Jun 5, 2018 at 10:24
  • @azures edit your question and insert complement inside. db[i] is a char and cannot receive a value of type char[]. Commented Jun 5, 2018 at 10:30

2 Answers 2

3

You need to process return values of recv and send because they do incomplete reads and writes which must be handled.

Also, recv does not zero terminate received data, you need to do that yourself again to be able to call strcmp.

Most importantly, you need a way to delimit complete messages.

In other words, you need to rewrite the whole piece with message extraction, partial read/write and error handling to make it work.

Sign up to request clarification or add additional context in comments.

Comments

0

if db[i] is a char array for which db should be double dimension array, you need to do a memcpy or strcpy (if you are sure that buffer will have null terminated string) to get contents of buffer to db[i].

db[i] = buffer

is wrong, thats why compilation error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.