C++ 单流上的Sctp文件(png)传输
我目前正在为Linux开发一个需要通过一个流发送文件的SCTP服务器-客户端对。这是因为其他流将用于发送命令、遥测等。如果有帮助的话,我正在使用QtCreator。为此,我需要使用SCTP。从现在起,服务器和客户端作为单独的应用程序在本地端口上运行C++ 单流上的Sctp文件(png)传输,c++,linux,sockets,png,sctp,C++,Linux,Sockets,Png,Sctp,我目前正在为Linux开发一个需要通过一个流发送文件的SCTP服务器-客户端对。这是因为其他流将用于发送命令、遥测等。如果有帮助的话,我正在使用QtCreator。为此,我需要使用SCTP。从现在起,服务器和客户端作为单独的应用程序在本地端口上运行 /* * Client (Recieving Side) */ //creates a file if it isn't there std::ofstream file("write.png"); //instantiates ofstream s
/*
* Client (Recieving Side)
*/
//creates a file if it isn't there
std::ofstream file("write.png");
//instantiates ofstream
std::ofstream ofs;
//opens the file using binary, I've tried it with "| std::ios::out" too
ofs.open ("write.png", std::ios::binary);
//tried different buffer sizes, same results, currently set at 512
char inBuffer[MAX_BUFFER+1];
while(error != -1){
memset(inBuffer, 0, MAX_BUFFER+1);
in = sctp_recvmsg( connSock, (void *)inBuffer
, sizeof(inBuffer), (struct sockaddr *)NULL, 0
, &sndrcvinfo, &flags);
if( in != -1 && sndrcvinfo.sinfo_stream == 3){
//writes the buffer to the file every time it recieves
//I've tried changing the pointers around,
//also tried using sizeof() instead of strlen()
//this changed the results, nothing worked so far
ofs.write((char *)&inBuffer, strlen(inBuffer));
//sends a handhake message (stored in s_buffer) to server
snd_ret = sctp_sendmsg( connSock, (void *)s_buffer
,(size_t)strlen(s_buffer)
,NULL, 0, 0, 0, 4, 0, 0 );
}
}
下面是一些重要的代码片段。客户机和服务器都在各自的main()中的独立线程中运行,这允许将来实现额外的代码
/*
*Server (Sending side):
*/
std::ifstream inFile("/home/username/test.png", std::ios::binary);
char buf[MAX_BUFFER+1];
while(inFile.read((char *)buf,sizeof(buf)))
{
//Sends the current buffer over stream 3
snd_ret = sctp_sendmsg( goodSock, (void *)buf, (size_t)strlen(buf),
NULL, 0, 0, 0, 3, 0, 0 );
/*
* in another thread there is a loop that receives messages
* the client side sends a message whenever it receives
* this ensures the data was sent and waits before sending more.
*/
while(confirmHandshake == false){/*...*/}
//delays so I can view and compare console outputs for debugging
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
目前,我还可以使用ofstream将它读取的缓冲区写入单独的png。这方面在很大程度上起作用;它不会读写整个文件,但这是我现在最不关心的问题
/*
* Client (Recieving Side)
*/
//creates a file if it isn't there
std::ofstream file("write.png");
//instantiates ofstream
std::ofstream ofs;
//opens the file using binary, I've tried it with "| std::ios::out" too
ofs.open ("write.png", std::ios::binary);
//tried different buffer sizes, same results, currently set at 512
char inBuffer[MAX_BUFFER+1];
while(error != -1){
memset(inBuffer, 0, MAX_BUFFER+1);
in = sctp_recvmsg( connSock, (void *)inBuffer
, sizeof(inBuffer), (struct sockaddr *)NULL, 0
, &sndrcvinfo, &flags);
if( in != -1 && sndrcvinfo.sinfo_stream == 3){
//writes the buffer to the file every time it recieves
//I've tried changing the pointers around,
//also tried using sizeof() instead of strlen()
//this changed the results, nothing worked so far
ofs.write((char *)&inBuffer, strlen(inBuffer));
//sends a handhake message (stored in s_buffer) to server
snd_ret = sctp_sendmsg( connSock, (void *)s_buffer
,(size_t)strlen(s_buffer)
,NULL, 0, 0, 0, 4, 0, 0 );
}
}
我面临的问题是,接收的数据与发送的数据不同。发送的数据似乎很好,因为我可以将缓冲区写入文件而不会损坏文件。
当我在客户端打开写入的png时,图片无法显示,根据我在调试过程中所做的不同更改,会出现各种错误。
我不确定数据哪里坏了,是通过套接字还是写入文件。我在控制台上打印两面时得到的乱码ASCII表示看起来几乎相同
我还想知道是否有更好的解决方案来读写文件。或发送数据的不同类型的数据(我认为\n可能是一个问题)。我想可能会尝试将文件转换为十六进制以进行传输,如果所有其他操作都失败,则将其转换回二进制。不过,我想让它只使用二进制文件
我想知道的另一件事是:是否可以简单地将socket.h的send和recv函数与sctp_sendmsg和sctp_recvmsg同时使用。我认为它会挂起程序,但我对套接字协议几乎没有经验
我也愿意接受任何其他建议。只要它仍然使用SCTP和流
谢谢