使用 C++ 中的套接字传输文件应用程序到Java应用程序
我正在尝试使用套接字将文件从 C++ 应用程序传输到 Java 应用程序。我用 C++ 编写了这个简单的代码来发送文件:
int main() {
int sock;
struct sockaddr_in sa;
char* memblock;
/* Create socket on which to send. */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
printf("opening datagram socket");
exit(1);
}
/* Construct name of socket to send to. */
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(0x7F000001);
sa.sin_port = htons(4444);
/* Send messages. */
FILE *file;
unsigned long fileLength = 0;
file = fopen(FILE_PATH, "rb");
if (!file) {
printf("Error opening the file.\n");
return 1;
}
// Get file length.
fseek(file, 0, SEEK_END);
fileLength = ftell(file);
printf("File length is: %d.\n", fileLength);
fseek(file, 0, SEEK_SET);
memblock = (char*)malloc(sizeof(char)*fileLength);
if (memblock == NULL) {fputs ("Memory error",stderr); exit(2);}
// copy the file into the buffer:
int result = fread(memblock, 1, fileLength, file);
if (result != fileLength) {fputs ("Reading error", stderr); exit(3);}
int pos = 0;
char* pMemblock = memblock;
while (pos < fileLength) {
int sent = sendto(sock, pMemblock, 80, 0, (struct sockaddr*)&sa, sizeof sa);
if (sent < 0)
printf("Error sending datagram message.\n");
else printf("Sent %d.\n", sent);
pos += 80;
pMemblock += 80;
}
delete memblock;
fclose(file);
close(sock);
return 0;
}
在 Java 端,我是这样写下收到的数据的:
while (true) {
receiveData = new byte[300];
receivedPacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivedPacket);
try {
fou = new FileOutputStream(<filename>, true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
fou.write(receivedPacket.getData(), 0, 80);
fou.close();
}
结果是我收到的文件在某种程度上是正确的,但并不完整。我的代码中 Java 或 C++ 部分有什么问题吗? 谢谢!
I'm trying to transfer a file using sockets from a C++ application to a Java application. I wrote this simple code in C++ to send the file:
int main() {
int sock;
struct sockaddr_in sa;
char* memblock;
/* Create socket on which to send. */
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
printf("opening datagram socket");
exit(1);
}
/* Construct name of socket to send to. */
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(0x7F000001);
sa.sin_port = htons(4444);
/* Send messages. */
FILE *file;
unsigned long fileLength = 0;
file = fopen(FILE_PATH, "rb");
if (!file) {
printf("Error opening the file.\n");
return 1;
}
// Get file length.
fseek(file, 0, SEEK_END);
fileLength = ftell(file);
printf("File length is: %d.\n", fileLength);
fseek(file, 0, SEEK_SET);
memblock = (char*)malloc(sizeof(char)*fileLength);
if (memblock == NULL) {fputs ("Memory error",stderr); exit(2);}
// copy the file into the buffer:
int result = fread(memblock, 1, fileLength, file);
if (result != fileLength) {fputs ("Reading error", stderr); exit(3);}
int pos = 0;
char* pMemblock = memblock;
while (pos < fileLength) {
int sent = sendto(sock, pMemblock, 80, 0, (struct sockaddr*)&sa, sizeof sa);
if (sent < 0)
printf("Error sending datagram message.\n");
else printf("Sent %d.\n", sent);
pos += 80;
pMemblock += 80;
}
delete memblock;
fclose(file);
close(sock);
return 0;
}
On the Java side this is how I write down data received:
while (true) {
receiveData = new byte[300];
receivedPacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivedPacket);
try {
fou = new FileOutputStream(<filename>, true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
fou.write(receivedPacket.getData(), 0, 80);
fou.close();
}
The result is that the file I receive is correct to a certain point, but it is not complete. Is there anything wrong in my code wither in the Java or in the C++ part?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此处。
如果您仅仅因为发送者和接收者位于同一台计算机上就认为这些规则不适用,那么您就错误地认为这些规则不适用。
没有任何形式的额外可靠性和排序的 UDP 对于您想要做的事情来说根本就是错误的选择。
您应该使用 TCP(或其他可靠的传输)而不是 UDP。
See here.
You are wrong to assume that these rules do not apply just because your sender and receiver are on the same machine.
UDP without any form of additional reliability and sequencing is simply the wrong choice for what you want to do.
You should use TCP (or another reliable transport) rather than UDP.
只需查看您的代码,问题可能是,您编写的语句是错误的:
应该是:
另一个注意事项:您是否在 while 循环中有意关闭 FileOutputStream (因此每次执行循环时?(这是并不是真的错,但速度较慢)
Just by looking over your code, the problem probably is, that you're write statement is wrong:
which should be:
Another note: are you closing the FileOutputStream intentionally within the while loop (therefore every time you execute the loop? (this is not really wrong but slower)