gpt4 book ai didi

c++ - C++中客户端到服务器的数据传输问题

转载 作者:行者123 更新时间:2023-11-28 01:17:26 25 4
gpt4 key购买 nike

我有一个使用 UDP 数据报将数组数据发送到服务器的客户端。为了能够跟踪我发送的数据,我用数字填充了我的数组。例如; 在客户端代码中我填buf[7] = 7;这个索引是为了表示分片总数。我希望在服务器中也能看到相同的

但是,我的 buf in server 没有得到这些值。我必须在服务器中使用固定大小的字符数组 我无法更改它。 (我对数据进行了长时间的计算,因为与问题无关,所以没有放)

我要解释一下服务器结构:

  • 我排长队。队列包含 3 个缓冲区。如果我获取数据的速度太快并且如果 buf 已满且其数据仍在文件中,则应使用 buf2等等。这是为了优化目的。
  • 我使用一个线程,它将当前缓冲区数据写入一个文件并行的方式。我向线程发出信号,以便它知道何时开始写作。

我怀疑线程,当我评论它时,接收功能卡住了。我怀疑类型的数组,因为我发送了 char 指针数组但收到了固定大小的 char 数组。 (同样,我必须在接收时使用固定大小)

All I want is to see the data correctly transferred. When I look at server I want to see the same data at same indexes.

Here is my simple client code

void main(int argc, char* argv[]){
WSADATA data;
WORD version = MAKEWORD(2, 2);

int wsOk = WSAStartup(version, &data);
if (wsOk != 0)
{
cout << "Can't start Winsock! " << wsOk;
return;
}

sockaddr_in server;
server.sin_family = AF_INET; // AF_INET = IPv4 addresses
server.sin_port = htons(...port no...); // Little to big endian conversion
inet_pton(AF_INET, "...IP....", &server.sin_addr); // Convert from string to byte array

SOCKET out = socket(AF_INET, SOCK_DGRAM, 0);
int sendOk = 0;

int *buf = new int[1450];

//just fill it with ordered number to be able to trace from other side
for (int i = 0; i < 1450; i++)
{
buf[i] = i;
}

buf[7] = 7; // this index represent TotalFragmentCount in UDP

while (true) {

for (int fragmentNumber = 1; fragmentNumber < 8; fragmentNumber++) {
//I want to set this index to count fragment number when I look at server
buf[9] = fragmentNumber;
//I give it as char * array with reinterpret_cast, since sendto accepts pointer char array
sendOk = sendto(out, reinterpret_cast<char*>(&buf), 1450, 0, (sockaddr*)& server, sizeof(server));
}

}

if (sendOk == SOCKET_ERROR)
{
cout << "That didn't work! " << WSAGetLastError() << endl;
}

closesocket(out);
WSACleanup();

}

Here is my server code

void writeToFile(char buf[], vector<uint16_t> &intData, ofstream &file) {


while (true) {

if (signaled == 1) {
thread_mutex.lock();
//writing...
for (const auto& e : intData) {
file << e << "\n";
}
buffers.pop(); // pops front which is written by this time
buffers.push(buf);
thread_mutex.unlock();
break;
}
else{
continue;
}
}
}
// Main entry point into the server
void main()
{
WSADATA data;
WORD version = MAKEWORD(2, 2);

int wsOk = WSAStartup(version, &data);
if (wsOk != 0)
{
// Not ok! Get out quickly
cout << "Can't start Winsock! " << wsOk;
return;
}

// Create a socket, notice that it is a user datagram socket (UDP)
SOCKET in = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in serverHint;
serverHint.sin_addr.S_un.S_addr = ADDR_ANY; // Us any IP address available on the machine
serverHint.sin_family = AF_INET; // Address format is IPv4
serverHint.sin_port = htons(4660); // Convert from little to big endian

// Try and bind the socket to the IP and port
if (bind(in, (sockaddr*)&serverHint, sizeof(serverHint)) == SOCKET_ERROR)
{
cout << "Can't bind socket! " << WSAGetLastError() << endl;
return;
}

sockaddr_in client; // Use to hold the client information (port / ip address)
int clientLength = sizeof(client); // The size of the client information


char buf[1550] = { 0 }; //message gets here
char buf2[1550]= { 0 };
char buf3[1550]= { 0 };

// Control buffer traffic with queue
buffers.push(buf);
buffers.push(buf2);
buffers.push(buf3);

std::thread thread_write(writeToFile, std::ref(buffers.front()), std::ref( intData), std::ref(file)); //front returns first element of the queue

bytesIn = recvfrom(in, buffers.front(), 1550, 0, (sockaddr*)& client, &clientLength);
cout << "";
while (ReceivedFrameCount != 100)
{
ZeroMemory(&client, clientLength); // Clear the client structure

// Wait for message
bytesIn = recvfrom(in, buffers.front(), 1550, 0, (sockaddr*)& client, &clientLength);

if (bytesIn == SOCKET_ERROR)
{
cout << "Error receiving from client " << WSAGetLastError() << endl;
continue;
}

// Parse the byte array

TotalFragmentCount = (uint16_t)(buf[6] << 8 | buf[7]);
FrameFragmentNo = (uint16_t)(buf[8] << 8 | buf[9]);

signaled = 1;


}// end of while


// Close socket
closesocket(in);

file.close();
WSACleanup();

thread_write.join();
}

最佳答案

reinterpret_cast<char*>(&buf)不做你认为它做的事。应该是reinterpret_cast<char*>(buf) .和 1450应该是 1450*sizeof(int)因为你有 1450 个整数,每个整数都超过一个字节。

sendto 需要一个指向它将发送的数据的指针。如果你通过了&buf (指向 buf 的指针)那么您正在发送指针 buf .如果你通过了buf (指向缓冲区的指针)然后您正在发送缓冲区。


当你构建一个整数数组时,你的计算机将它们作为字节存储在内存中,如下所示:(也许 - 它是特定于平台的!)

Byte  0: 0 \
Byte 1: 0 |
Byte 2: 0 | int 0
Byte 3: 0 /
Byte 4: 1 \
Byte 5: 0 |
Byte 6: 0 | int 1
Byte 7: 0 /
Byte 8: 2 \
Byte 9: 0 |
Byte 10: 0 | int 2
Byte 11: 0 /

当服务器将这些视为字节时,它将在索引 8 处看到数字 2,依此类推。如果您希望它匹配,那么客户端也许应该创建一个字节数组(字符,与服务器相同)而不是整数。

关于c++ - C++中客户端到服务器的数据传输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58200957/

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