Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无法传输二进制文件_C_Sockets_Network Programming_Binaryfiles - Fatal编程技术网

C 无法传输二进制文件

C 无法传输二进制文件,c,sockets,network-programming,binaryfiles,C,Sockets,Network Programming,Binaryfiles,我正在尝试构建一个服务器客户端代码。客户端可以从服务器下载任何文件,但我在使用二进制文件时遇到问题 这是我的客户代码: #include <stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/time.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h>

我正在尝试构建一个服务器客户端代码。客户端可以从服务器下载任何文件,但我在使用二进制文件时遇到问题

这是我的客户代码:

#include <stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/time.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<fcntl.h>
#define Max_len 1025
//-------------------------- to replay for pecific secret simpol 0x55
char secret(char * str)
{
    char  sub=0x55;
    if(sub==str[0])
        {
            sub=0xAA;
            return sub;
        }
    else
        {
            printf("server does not give the right secret code\n");
            exit(0);
        }
}
//-------------------------warraper function for perror
void error(char * str)
{
    perror(str);
    exit(0);
}
int main ( int nof_arg, char **argv)
{
    //------------------------define data types
    int sockfd,number,portno,val=0,fil_fd;  //to store the file descreptore of socket
    char rec_lin [Max_len],f_name[20]="\0",car;  // act as buffer
    struct sockaddr_in servaddr;
    char * read_one;
    FILE * file_ptr;  //------------------------ welcome message identife the writer
    printf("\n\t Welcom the client \n\t Name: Eftikhar Emad \n\t ID: 20100175003\n\tsection #1: 9:15-10:15\n\n");
    //------------------------ prepair the client to start connection
    if (nof_arg < 3)            //check the number of entered arregument
        error("you need the IP and port number to connect please check them again\n");

    portno=atoi(argv[2]);           // convert the port from string to int
    if((portno<=22000) || (portno>=23000))  //check to see if the port numberif valiled
        error("your port number is out of range please try agin start with 22XXX\n");

    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)   //socket creation
        error("Socket creation error");

    bzero(&servaddr,sizeof(servaddr));      //static define for the server
    servaddr.sin_family=AF_INET;
    servaddr.sin_port = htons(portno);

    if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)//it can be used to check
        error("inet_pton error ");

    if((connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))<0)//connection
        error("connect error");

    //------------------------------------start define the server valited
    bzero(rec_lin,sizeof(rec_lin));
    read(sockfd,rec_lin ,sizeof(rec_lin));
    printf("received the byte 0x55 from the server\n"); //will readonly one char
    car=secret(rec_lin);
    printf("Sending ack 0xAA to the server\n");
    bzero(rec_lin,sizeof(rec_lin));
    snprintf(rec_lin,sizeof(rec_lin),"%c",car);
    write(sockfd,rec_lin,1);            //send 0xAA if 0x55


    //-------------------------------------print the list from server
    read(sockfd,rec_lin,sizeof(rec_lin));
    while(rec_lin[0]!= '\0')
        {
            read(sockfd,rec_lin,sizeof(rec_lin));
            printf("%s\n",rec_lin);
        }


    //-------------------------after know the server start to ask and send file name
    printf("enter file name to download : ");
    gets(f_name);                   //rad file name from user
    snprintf(rec_lin,sizeof(rec_lin),"%s",f_name);
    write(sockfd,rec_lin,sizeof(rec_lin));       //send the file name to server
    bzero(rec_lin,sizeof(rec_lin));
    //--------------------------------------check server reply 0/1 for finding or not
    read(sockfd,rec_lin,/*sizeof(rec_lin)*/1);
    int p;
    if(rec_lin[0]==0x31)    //if file found inform the user,start,ending msg
        {           //no creation to file unless it found on server
            printf("Received 1 from server %s exists\nStart downloading the file\n",f_name);
            fil_fd=open(f_name,O_RDWR| O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
            while((val=read(sockfd,rec_lin,sizeof(rec_lin)-1))>0)
                {
                    p=strlen(rec_lin);
                    //  rec_lin[val]='\0';
                    write(fil_fd,rec_lin,/*sizeof(rec_lin)*/p+1);
                    bzero(rec_lin,1049);
                }
            printf("Done Downloadng the file.......exit\n\n");
            close(fil_fd);
        }
    else            //if file not found terminate the connection
        {
            printf("file not found get ... connection will closed\n\n");
        }
    // -----------------------------terminate connection if it Done in the right way
    close(sockfd);
    return 0;

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大长度1025
//--------------------------要重播特定机密simpol 0x55
字符秘密(char*str)
{
char sub=0x55;
if(sub==str[0])
{
sub=0xAA;
返回子节点;
}
其他的
{
printf(“服务器未提供正确的密码\n”);
出口(0);
}
}
//-------------------------perror的warraper函数
无效错误(字符*str)
{
perror(str);
出口(0);
}
int main(int nof_参数,字符**argv)
{
//------------------------定义数据类型
int sockfd,number,portno,val=0,fil_fd;//用于存储套接字的文件descreptore
char rec_lin[Max_len],f_name[20]=“0”,car;//充当缓冲区
servaddr中的结构sockaddr_;
字符*读一个;
FILE*FILE_ptr;//---------------------------欢迎消息标识,请输入作者
printf(“\n\t Welcom客户名称\n\t Eftikhar Emad\n\t ID:20100175003\n\t第1节:9:15-10:15\n\n”);
//------------------------对客户端进行预处理以启动连接
if(nof_arg<3)//检查输入的欠款数量
错误(“您需要IP和端口号才能连接,请再次检查它们\n”);
portno=atoi(argv[2]);//将端口从字符串转换为int
if((portno=23000))//检查端口号是否有效
错误(“您的端口号超出范围,请再次尝试从22XXX开始\n”);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))0)
{buff[n]='\0';
写入(connfd,buff,/*sizeof(buff)*/n+1);//发送文件进程
bzero(buff,sizeof(buff));
}//上传结束
printf(“结束下载thf文件”);
关闭(fil_fd);
}//if_else结束
关闭(connfd);
出口(0);
}//子代码结尾
其他的
{
//等待(空);
printf(“------------------------------------\n”);
关闭(connfd);
}
}//for()的结束
//--------------------------------------------预装服务器连接
关闭(listenfd);
出口(0);
返回0;
}//main()
如果我将此代码与“file.txt”一起使用,则此代码可以正常工作。 我想将其用于二进制文件,而不使用
fread()
fwrite
fopen

有什么办法吗?

您无法编写正确的网络软件来忽略read()、recv()等返回的值。这些函数要么返回-1表示错误,要么返回零表示流结束,要么返回读取计数。您当前的代码忽略了所有三种可能性,并且它还假设read()填充了缓冲区。没有任何地方这样说


编辑:我应该明确,这同样适用于任何来源的阅读,而不仅仅是网络阅读。

我的问题是。。。。使用
open()


解决方案:使用
fopen(文件名,“r+b”)
可以创建以不同于文本文件的方式使用二进制文件的文件

当您尝试下载二进制文件时会出现什么问题?我无法在同一台机器上测试它,因此读取和写入将在同一个文件上这应该是我问题的答案吗?无论如何,看起来问题可能与服务器在将
buf
写入连接之前将
\0
添加到
buf
的末尾有关。你为什么这么做?只有字符串才需要尾随null,而不是字节数组。我注意到原始数据中添加了一些垃圾,如^D^D^D^D^D^D^D@^D^@D^我认为读写需要null>>>我不知道我在二进制文件中的位置。。抱歉,前面的回复中有一个strlen()调用,这在网络代码中几乎没有好的结尾。
#include<stdio.h>
#include<errno.h>
#include<arpa/inet.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<fcntl.h>
#include<netdb.h>
#include<dirent.h>
#define Max_len 1025
void error (char * str)
{
    perror(str);
    exit(0);
}
int main (int argc, char ** arg)
{
    / /------------------------- define data type
        int listenfd,connfd,clilen,portno,fil_fd,n;
    char buff[Max_len], *str,f_name[100],car=0xAA,h_IP[30],h_P[20];
    struct sockaddr_in servaddr,cliaddr;
    DIR * ddir;
    struct dirent * ndir;
    //------------------------- socket intinalizing
    printf("\n\t Welcom the server \n\t Name: Eftikhar Emad \n\t ID: 20100175003\n\t section #1: 9:15-10:15\n");

    if(argc != 2)           //of user forget to insert the port number
        error("missing port number to complet establishment\n");

    portno=atoi(arg[1]);        //convert the port to int and check if it was within the rang
    if((portno<=22000) || (portno>=23000))
        error("your port number not valid .... pleas insert one in reng (22000,22999)\n");

    listenfd=socket(AF_INET,SOCK_STREAM,0);     //creat the listing socket
    bzero((char *)&servaddr ,sizeof(servaddr));
    servaddr.sin_family=AF_INET;        //Ipv4
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(portno);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));//bind the liten socket to specific port
    listen(listenfd,5);
    //-------------------------- server start to wait to handle requests
    for(;;)
        {
            printf("server wait for connections\n");
            bzero((char *)&cliaddr ,sizeof(cliaddr));   //clear the previus client information
            clilen=sizeof(cliaddr);             //client informaton container
            connfd=accept(listenfd,(struct  sockaddr *)&cliaddr,&clilen);
            int f=fork();       //start of fork()
            if(f==0)
                {

                    close(listenfd);
                    if(connfd<0)
                        error("error in accept this connection\n");
                    //---------------------------------------       //start the secret hand chacking
                    bzero(buff,sizeof(buff));
                    getnameinfo((struct sockaddr *)&cliaddr,sizeof(cliaddr),h_IP,sizeof(h_IP),h_P,sizeof(h_P),0);
                    printf("Sending byte 0x55 to client with IP %s and port of %s\n",h_IP,h_P);
                    //get the IP AND port# of the client
                    snprintf(buff,sizeof(buff),"%c",0x55);
                    write(connfd,buff,sizeof(buff));
                    //---------------------------------------       //read file name
                    car=0xAA;
                    read(connfd,buff,sizeof(buff));
                    printf("received 0XAA from client \n");
                    if(car==buff[0])
                        {
                            //------- send the list to the client
                            snprintf(buff,sizeof(buff),"select file to download it\n");
                            write(connfd,buff,sizeof(buff));
                            ddir=opendir(".");
                            if(ddir==NULL)
                                {printf("error\n");exit(0);}
                            while((ndir=readdir(ddir))!=NULL)
                                {//printf("%s\n",ndir->d_name);
                                    snprintf(buff,sizeof(buff),"%s",ndir->d_name);
                                    printf("%s\n",buff); 
                                    write(connfd,buff,sizeof(buff));
                                }
                            write(connfd,"\0",1);
                            closedir(ddir);


                            read(connfd,buff,sizeof(buff));     //get the name after get 0xAA
                            strncpy(f_name,buff,sizeof(buff)-1);
                        }
                    else
                        { printf("this is not secure client\n");break;}

                    //--------------------------------------   if file found send 1 else 0
                    fil_fd=open(f_name,O_RDONLY);       //file opent to be read only
                    if(fil_fd < 0)
                        {
                            snprintf(buff, sizeof(buff),"%d",0);
                            write(connfd,buff,1);
                            printf("file is not found\n");      //retminate connection only
                            break;
                        }
                    else
                        {
                            snprintf(buff, sizeof(buff),"%d",1);
                            write(connfd,buff,sizeof(buff));
                            printf("file exists.Send %s to client\n",buff);
                            //-----------------------------------reading and sending
                            while((n=read(fil_fd,buff,sizeof(buff)-1))>0)
                                {   buff[n]='\0';
                                    write(connfd,buff,/*sizeof(buff)*/n+1); //sending the file process
                                    bzero(buff,sizeof(buff));
                                }//end of uploading
                            printf("end download thf file");
                            close(fil_fd);
                        }//end of if_else
                    close(connfd);
                    exit(0);
                }//end of child code
            else
                {
                    //wait(NULL);
                    printf("-------------------------------\n");
                    close(connfd);
                }
        }//end of for()
    //--------------------------------------------prepair the server connection
    close(listenfd);
    exit(0);

    return 0;
}//main()