【发布时间】:2019-10-02 11:52:27
【问题描述】:
我有一个使用 UDP 数据报向服务器发送数组数据的客户端。为了能够追踪我发送的数据,我用数字填充了我的数组。例如; 在客户端代码中我填buf[7] = 7;这个索引是为了表示分片的总数。我希望在服务器中也看到相同的。
但是,我的buf 在服务器中 没有得到这些值。
我必须在服务器中使用固定大小的字符数组 我无法更改它。 (我对数据进行了长时间的计算,因为与问题无关,所以我没有放)
我要解释一下服务器结构:
- 我在排队。队列包含 3 个缓冲区。如果我获取数据太快并且
如果
buf已满且其数据仍在文件中,则应使用buf2等等。这是为了优化目的。 - 我使用一个线程,它将当前缓冲区数据写入到一个文件中 并行方式。我向线程发出信号,以便它知道何时开始 写作。
我怀疑线程,当我评论它时,收到功能卡住。 我怀疑类型的数组,因为我发送 char 指针数组但接收到固定大小的 char 数组。 (再次接收时我必须使用固定尺寸)
我只想看到正确传输的数据。当我看 服务器 我想在相同的索引上看到相同的数据。
这是我的简单客户端代码
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();
}
这是我的服务器代码
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();
}
【问题讨论】: