我的客户端服务器代码无法正常工作

时间:2014-03-03 00:13:11

标签: c++ tcp

我得到一些客户端服务器程序的代码,我自己修改它。但我无法找出为什么我的代码无法正常工作。编译很好,但在客户端中出现中止错误?我已经尝试过了,我是编程的新手。如果你能看一下下面给出的客户端服务器代码,请查看问题是什么。

//Client side code 

#include <iostream>
#include <winsock2.h>
#include <vector>
#include <algorithm>
#include <ctime>
#include "client.h"
#pragma comment(lib, "Ws2_32.lib")

using namespace std;
tcp_client::tcp_client()
{}

tcp_client::~tcp_client()
{}
int tcp_client::start_listening()

{

    WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    SOCKET ConnectSocket;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (ConnectSocket == INVALID_SOCKET)
    {
        cout<<"Client: Error at socket(): %ld.\n"<< WSAGetLastError();
        WSACleanup();
        return 0;
    }

    sockaddr_in clientService;
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
    clientService.sin_port = htons(55555);

    if (connect(ConnectSocket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
    {
        cout<<"Client: Failed to connect\0";
        WSACleanup();
        return 0;
    }
    return 0;
}

int tcp_client::start_sending()
{

    int bytesSent;
    int bytesRecv = SOCKET_ERROR;

    std::vector<double> m_vector;
    uint32_t  nlength =0;
    std::vector<double> m_vector1(nlength/sizeof(double)); 


    for( int i = 0; i <100; i++)
    {    
        m_vector.push_back(i);
    }

    uint32_t siz = (m_vector.size())*sizeof(double);



    int total_bytes = 0;
    int count=0;


    for(int j=0; j<10; j++)
    {  

        bytesSent = send(ConnectSocket,(char*)&siz, 4, 0);
        assert (bytesSent == sizeof (uint32_t));
        std::cout<<"length information is in:"<<bytesSent<<"bytes"<<std::endl;

        bytesSent = send(ConnectSocket,(char*)m_vector.data(), siz, 0);
        total_bytes = total_bytes+bytesSent;

        std::cout<<"Client: Bytes sent:"<<total_bytes<<std::endl;
    }

    closesocket (ConnectSocket);
    return 0;
}


int main()
{

    std::clock_t c_start = std::clock();

    tcp_client a;
    a.start_listening();
    a.start_sending();

    std::clock_t c_end = std::clock();
    std::cout << "CPU time used: "<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC<< " ms\n";

    WSACleanup();
    system("pause");
    return 0;
}

// Client.h

#include <string.h>
#include <winsock2.h>
class tcp_client
{

public:
    tcp_client();
    virtual ~tcp_client();
    int start_listening();
    int start_sending();

protected:

   SOCKET ConnectSocket; // client Socket
   };

//服务器代码

#include <iostream>
#include <winsock2.h>
#include <vector>
#include <algorithm>
#include <ctime>
#pragma comment(lib, "Ws2_32.lib")
#include "server.h"

using namespace std;
 tcp_server::tcp_server()
{}

tcp_server::~tcp_server()
{}

int tcp_server::start_listening()
{

    WORD wVersionRequested;
    WSADATA wsaData;
    int wsaerr;
    wVersionRequested = MAKEWORD(2, 2);
    wsaerr = WSAStartup(wVersionRequested, &wsaData);


    if (wsaerr != 0)
    {
        std::cout<<"server starting...."<<std::endl;
    }


    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (m_socket == INVALID_SOCKET)
    {
        cout<<"Server: Error at socket(): "<<WSAGetLastError()<<std::endl;
        WSACleanup();
        return 0;
    }


    sockaddr_in service;
    service.sin_family = AF_INET;

    service.sin_addr.s_addr = inet_addr("127.0.0.1");
    service.sin_port = htons(55555);

    if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
    {
        cout<<"Server: bind() failed:"<< WSAGetLastError();
        closesocket(m_socket);
        return 0;
    }


    if (listen(m_socket, 10) == SOCKET_ERROR)
    {
        cout<<"Server: listen(): Error listening on socket "<< WSAGetLastError()<<std::endl;
    }

    SOCKET AcceptSocket;

    while (1)
    {
        AcceptSocket = SOCKET_ERROR;
        while (AcceptSocket == SOCKET_ERROR)
        {
            AcceptSocket = accept(m_socket, NULL, NULL);
        }

        cout<<"Server: Client Connected"<<std::endl;
        m_socket = AcceptSocket;
        break;
    }

    return 0;
}

int tcp_server::start_receiving()
{

    int bytesSent;
    int bytesRecv;
    uint32_t  nlength =0;
    int total_br =0;
    std::vector<double> m_vector(nlength/sizeof(double));


    std::clock_t c_start=0;

    int count =0;

    while(1)
    {

        int length_received = recv(m_socket,(char*)&nlength, 4, 0); 
        m_vector.resize(nlength/sizeof(double));
        bytesRecv = recv(m_socket,(char*)m_vector.data(), nlength, 0);


        if(bytesRecv > 0)
        {
            total_br = total_br + bytesRecv;

            cout<<"Server: Received bytes are"<<total_br<<std::endl;
        }
            else
        {
            std::cout<<"Data Receiving has finished"<<std::endl;
            break;
        }

}
closesocket (m_socket);
WSACleanup();   
return total_br;
}

int main()

{
    std::clock_t c_start = std::clock();
    tcp_server a;
    a.start_listening();
    int byte= a.start_receiving();
    std::cout<<"total byte received are"<<byte;

    std::clock_t c_end = std::clock();
    std::cout << "CPU time used: "<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC<< " ms\n";
    system("pause");
    return 0;
}

// server.h

#include <string.h>
#include <winsock2.h>
class tcp_server
{

public:
    tcp_server();
    virtual ~tcp_server();
    int start_listening();
    int start_receiving();

protected:

   SOCKET m_socket; // Server Socket
    };

1 个答案:

答案 0 :(得分:1)

在client.cpp中删除此行,它将起作用:

SOCKET ConnectSocket;

我之前删除此行后运行了代码,因此工作正常。

此行表示您在函数ConnectSocket范围内定义局部变量start_listening()此局部变量隐藏成员变量。您似乎将有效的文件描述符分配给成员变量,但成员变量ConnectSocket始终未初始化,因此send()失败。