gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:52:03 25 4
gpt4 key购买 nike

我得到了一些客户端服务器程序的代码,我自己修改了它。但是我找不到为什么我的代码不能正常工作。编译没问题,但在客户端出现中止错误?我已经尝试过,我是编程的新手。所以请看一下下面提供的客户端服务器代码并检查问题出在哪里。

//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;
}

//客户端.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;
}

//服务器.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
};

最佳答案

删除 client.cpp 中的这一行,它将起作用:

SOCKET ConnectSocket;

我之前删除了这一行后运行了代码,所以它运行良好。

这一行意味着你在函数start_listening()的范围内定义了一个局部变量ConnectSocket这个局部变量隐藏了成员变量。似乎您为成员变量分配了一个有效的文件描述符,但成员变量 ConnectSocket 始终未初始化,因此 send() 失败。

关于c++ - 我的客户端服务器代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com