C Program, recv() error: Connection reset by peer

3k views Asked by At

I have some Problems using the recv() and send() functions.

here's my code:

#include "performConnection.h"
#include "errno.h"



//checks if the first arguement of an answer is "+"
bool checkPlus(char buffer[]){
int position = strcspn(buffer,"+");
if (position==0){
    return true;
}
else{
    return false;
}

}

int performConnection(int socketDescriptor){
bool check;
char buffer[256];
memset(&buffer, 0, sizeof(buffer));
char string[1024];
memset(&string, 0, sizeof(string));
int n;                      //Bytes received/send




//receiving data from server//accepting connections
n = recv(socketDescriptor, buffer, 255,0);
if(n<0){
    perror("recv() failed");
    printf("Exiting program");

    return EXIT_FAILURE;                    //exit program if receive failed
}
else{
    check = checkPlus(buffer);  //+ check for positive answer
    if (check==false){
        perror("failure");
        return EXIT_FAILURE;    //exit program if -
    }

}
printf("%s\n", buffer);
memset(buffer, 0, sizeof(buffer));



//send GAMEVERSION to server
n = send(socketDescriptor,GAMEVERSION, 255, 0);
if(n<0){
    perror("send() failed");
    printf("exiting program");
    return EXIT_FAILURE;                    //exit program if send() fails
}

printf("Gameversion: %i, %s",n, GAMEVERSION);

//receive server data
n = recv(socketDescriptor, buffer, 255, 0);
 if(n<0){
    perror("recv() failed");
    printf("Exiting program");

    return EXIT_FAILURE;                    //exit program if recv() fails
}
else{
    check = checkPlus(buffer);  //+ check for positive answer
    if (check==false){
        perror("failure");
        return EXIT_FAILURE;                //exit program if negative answer
    }
}

printf("%s\n", buffer);
memset(&buffer,0, sizeof(buffer));



    strcpy(buffer,"ID K.GcY_EqHAs\n");
    printf("%s\n",buffer);
    n = send(socketDescriptor, buffer, 255,0);
    if(n<0){
        perror("send() failed");
        printf("Exiting program");

        return EXIT_FAILURE;
    }

   memset(&buffer, 0, sizeof(buffer));


n = recv(socketDescriptor, buffer, 255, 0);

 if(n<0){
    perror("recv() failed");
    printf("Exiting program");
    printf("\n%s\n", buffer);
    return EXIT_FAILURE;                    //exit program if recv() fails
}
else{
    check = checkPlus(buffer);  //+ check for positive answer
    if (check==false){
        perror("failure");

        return EXIT_FAILURE;                //exit program if negative answer
    }
}   
printf("%s",buffer);

return EXIT_SUCCESS;

}

The connection is already established in another file.

Here's an example for positive Answers.

Server: + MNM Gameserver <<GameserverVersion>> accepting connections
Client: VERSION <<Client Version>>
Server: + Client version accepted - please send GameID to join
Client: ID <<ID>>
Server: + Playing

When something is wrong, server replies with "-".

so but i get this:

+MNM Gameserver v1.0 accepting connections

Gameversion: 255, VERSION 1.1

+Client version accepted - please send Game-ID to join

ID K.GcY_EqHAs

recv() failed: Connection reset by peer
Exiting program

Please, anyone help me, I have no ideas...

LG Max

0

There are 0 answers