c ++ vector访问冲突读取位置

时间:2014-05-17 17:28:06

标签: c++ vector

当我尝试在函数中向我的向量中推送一个元素时,我得到一个Access违规读取位置,但是如果我在类构造函数中这样做它就可以了。

提出问题的类的头文件

#include <winsock2.h>
#include <string>
#include <vector>

#pragma comment(lib,"ws2_32.lib") //Winsock Library


class Network{
    public:

    /*
    * General functions
    */
    Network();
    Network(bool debugModus);

    ~Network();

    /*
    * Server Functions
    */
    int ServerSetup(int port);
    void ServerAcceptConnections();

    /* 
    * Server variable
    */
    SOCKET m_client;
    std::vector<SOCKET> m_clientList;

};

cpp

void Network::ServerAcceptConnections()
{
     SOCKET new_socket;
     m_clientList.push_back(new_socket);
}

主要

void main ()
{
    Network network (true);
    network.ServerSetup(800);
    while(true)
    {
        network.ServerAcceptConnections();
    }

}

如果我执行命令: m_clientList.push_back(new_socket); 在类构造函数中它工作,我可以在调试器中看到它, 但当我到达ServerAcceptsConnection()函数时,它确实包含anny元素。 如果我尝试在那里添加somthing我得到这个错误: server.exe中0x00c730ab处的未处理异常:0xC0000005:访问冲突读取位置0x01000083。

编辑整个代码 标题

    #include <winsock2.h>
    #include <string>
    #include <vector>

    #pragma comment(lib,"ws2_32.lib") //Winsock Library

    struct Message{
        char* message;
        unsigned int max_size;
        unsigned int size;
    };

    class Network{
        public:

        /*
        * General functions
        */
        Network();
        Network(bool debugModus);
        int Connect(std::string ipAddress, int port);
        int Send(SOCKET socket, char* message, unsigned int size);
        Message Receive(SOCKET socket, int size = 2000);

        ~Network();

        /*
        * Server Functions
        */
        int ServerSetup(int port);
        void ServerAcceptConnections();

        private:
        /*
        * General Functions
        */
        int Init();

        /*
        * Server functions
        */
        int Bind(int port);
        int Listen();

        /*
        * General Variable
        */
        bool m_debugModus;
        WSADATA m_wsa;
        SOCKET m_s;
        struct sockaddr_in m_server;

        /* 
        * Server variable
        */
        SOCKET m_client;
        std::vector<SOCKET> m_clientList;
        std::vector<Message> m_outBuffer;
        std::vector<Message> m_inBuffer;
    };

the cpp

#include "NetworkClass.h"
#include<stdio.h>

Network::Network()
{
    m_debugModus = false;
    Init();
}
Network::Network(bool debugModus)
{
    m_debugModus = true;
    if(m_debugModus) printf("\nDEBUG MODUS!!");
    if(m_debugModus) printf("\nThis mode is for debugging only!!");
    if(m_debugModus) printf("\n");
    if(m_debugModus) printf("\n");
    Init();
}

int Network::Init()
{
    SOCKET new_socket;
    m_clientList.push_back(new_socket);
    if(m_debugModus) printf("\nDEBUG MODUS: Initialising Winsock...");

    if (WSAStartup(MAKEWORD(2,2),&m_wsa) != 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    if(m_debugModus) printf(" Initialised.\n");

    /* 
    * creating a socket
    *
    * Settings:
    * Address Family : AF_INET (this is IP version 4)
    * Type : SOCK_STREAM (this means connection oriented TCP protocol)
    * Protocol : 0 [ or IPPROTO_TCP , IPPROTO_UDP ]
    */
    if((m_s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        if(m_debugModus) printf("DEBUG MODUS: Could not create socket : %d" , WSAGetLastError());
    }

    if(m_debugModus) printf("DEBUG MODUS: Socket created.\n");
    return 0;
}

int Network::Connect(std::string ipAddress, int port)
{
    if(m_debugModus) printf("DEBUG MODUS: Trying to connect with: %s on port %d \n", ipAddress.c_str(), port);
    m_server.sin_addr.s_addr = inet_addr(ipAddress.c_str());
    m_server.sin_family = AF_INET;
    m_server.sin_port = htons( port );

    if (connect(m_s , (struct sockaddr *)&m_server , sizeof(m_server)) < 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Error cant open a connecting\n", ipAddress, port);
        return 1;
    }
    if(m_debugModus) printf("DEBUG MODUS: Connected\n");
    return 0;
}

int Network::Send(SOCKET socket, char* message, unsigned int size)
{
    if( send(socket , message , size , 0) < 0)
    {
        if(m_debugModus) printf("DEBUG MODUS: Send failed error: %d\n", WSAGetLastError());
        return 1;
    }
    if(m_debugModus) printf("DEBUG MODUS: Data Send\n");
    return 0;
}

Message Network::Receive(SOCKET socket, int size)
{
    Message message;
    message.message = (char*) malloc(sizeof(char)*size);
    message.max_size = size;
    if((message.size = recv(socket , message.message , message.max_size , 0)) == SOCKET_ERROR)
    {
        if(m_debugModus) printf("DEBUG MODUS: recv failed");
    }

    if(m_debugModus) printf("DEBUG MODUS: Reply received\n");

    return message;
}

int Network::Bind(int port)
{
    m_server.sin_family = AF_INET;
    m_server.sin_addr.s_addr = INADDR_ANY;
    m_server.sin_port = htons( port );

    if( bind(m_s ,(struct sockaddr *)&m_server , sizeof(m_server)) == SOCKET_ERROR)
    {
        if(m_debugModus) printf("DEBUG MODUS: Bind failed with error code : %d\n" , WSAGetLastError());
        return 1;
    }

    if(m_debugModus) printf("DEBUG MODUS: Bind done\n");
    return 0;
}

int Network::Listen()
{
    listen(m_s , 3);
    return 0;
}

int Network::ServerSetup(int port)
{
    Bind(port);
    Listen();
    return 0;
}

void Network::ServerAcceptConnections()
{
    SOCKET new_socket;
    int c = sizeof(struct sockaddr_in);
    new_socket = accept(m_s , (struct sockaddr *)&m_client, &c);
    if (new_socket != INVALID_SOCKET )
    {
        if(m_debugModus) printf("DEBUG MODUS: Connection accepted\n");

        //Reply to the client
        Send(new_socket, "Hello Client , I have received your connection. But I have to go now, bye\n", strlen("Hello Client , I have received your connection. But I have to go now, bye\n"));
        m_clientList.push_back(new_socket);
    }

    if (new_socket == INVALID_SOCKET)
    {
        if(m_debugModus) printf("DEBUG MODUS: accept failed with error code : %d" , WSAGetLastError());
    }
}

Network::~Network()
{
    closesocket(m_s);
    WSACleanup();
}

2 个答案:

答案 0 :(得分:2)

看起来你超出了矢量的容量。在while(true)循环中,您正在调用network.ServerAcceptConnections();函数,该函数在每次调用时构造一个SOCKET对象,然后将其推送到m_clientList,并且它无限期地执行此操作(作为while无限期运行)。至少这是看着你的代码剪断的情况。

另一方面,类构造函数只被调用一次。

答案 1 :(得分:0)

我发现了这个问题 在SOCKET客户端的标题中; 走出界限 如果我把矢量放在这一行之上就行了