C程序,recv()错误:由对等方重置连接

时间:2014-11-12 17:30:52

标签: c sockets send recv

使用recv()和send()函数时遇到了一些问题。

这是我的代码:

#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;

}

已在另一个文件中建立连接。

这是正面答案的一个例子。

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

如果出现问题,服务器会回复&#34; - &#34;。

所以,但我得到了这个:

+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

拜托,有人帮助我,我没有想法......

LG 最大

0 个答案:

没有答案