我试图通过套接字发送文件.我创建了一个程序,它适用于.cpp,.txt和其他文本文件等文件类型.但是二进制文件,图像(.jpg,.png)和压缩文件(如.zip和.rar)未正确发送.我知道这与文件的大小无关,因为我测试了大的.txt文件.我不知道问题,我收到所有发送的字节,但文件无法打开.大多数情况下文件已损坏且无法查看.我已经通过谷歌搜索了一个解决方案,并且发现其他人遇到了同样的问题并没有解决方案.所以通过帮助我,你也在帮助其他需要解决方案的人.
服务器代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main ( int agrc, char *argv[] )
{
/******** Program Variable Define & Initialize **********/
int Main_Socket; // Main Socket For Server
int Communication_Socket; // Socket For Special Clients
int Status; // Status Of Function
struct sockaddr_in Server_Address; // Address Of Server
struct sockaddr_in Client_Address;// Address Of Client That Communicate with Server
int Port;
char Buff[100] = "";
Port = atoi(argv[2]); …Run Code Online (Sandbox Code Playgroud) sockets ×1