我有一个客户端,该客户端使用UDP数据报将数组数据发送到服务器。为了能够跟踪我发送的数据,我用数字填充了数组。例如; 在客户端代码中我填写buf[7] = 7;
,该索引用于指示片段的总数。我希望服务器中也能看到相同的 。
但是,我在服务器中的buf
没有获得这些值。
我必须在服务器中使用固定大小的char数组 我无法更改它。 (我对数据进行了长时间的计算,因为与问题无关,所以我没有写)
我必须解释服务器的结构:
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();
}
答案 0 :(得分:2)
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,依此类推。如果您希望它匹配,那么客户端可能应该创建一个字节数组(字符,与服务器相同)而不是整数。