English 中文(简体)
如何用一把一只笼罩在一带的袖子里寄出一个床子?
原标题:How do i send a .bmp file using socket apis in c at one go?

I have a file pict.bmp which i need to send over the LAN.There is a server which starts listening on a port.After the client establishes connection with the server,it requests the server providing a file name for the file.The server then uses the filename and sends to the client.But the problem is that the server sends it successfully using the sendfile() system call but the file does not appear in the directory in which the client is running.is there a better way to do it i.e some other system call which fulfills the same purpose.I am copy pasting the sending a receiving part here :

服务器:

 while(1){

                    nsd = accept(sd,(struct sockaddr*)&clit,&clen);

                    if(nsd < 0){

                            eMsg("Accept error");

                    }

                    rc = recv(nsd, filename, sizeof(filename), 0);

                    if (rc < 0) {

                            eMsg("Receive error");

                    }

                    printf("Filename : %s
",filename);

                    filename[strlen(filename) - 1] =   ;

                    if (strcmp(filename, "quit") == 0) {

                            fprintf(stderr, "quit command received, shutting down server
");

                            break;
                    }

                    fd = open("2bird.bmp", O_RDONLY);

                    printf("fd is %d
",fd);

                    if (fd < 0) {

                            eMsg("File Open error");

                    }

                    offset = 0;

                    rc = sendfile (nsd, fd, NULL, 1);

                    if (rc < 0) {

                        eMsg("File Send error");

                        fprintf(stderr,"Sending failed");

                }

                if (rc != stat_buf.st_size) {

                        eMsg("File Transfer error");

                }

                close(fd);

                close(nsd);

客户:

bzero((char *) &serv_addr, sizeof(serv_addr));
     39
     40         serv_addr.sin_family = AF_INET;
     41
     42         bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,server->h_length);
     43
     44         serv_addr.sin_port = htons(atoi(argv[2]));
     45
     46         if (connect(sd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
     47
     48                 eMsg("Connection error");
     49
     50         }
     51
     52         printf("Please enter the filename : ");
     53
     54         bzero(buffer,256);
     55
     56         fgets(buffer,255,stdin);
     57
     58         n = write(sd,buffer,strlen(buffer));
     59
     60         if (n < 0)
     61
     62         error("Writing To Socket error");
     63
     64
     65         close(sd);
     66
     67         return 0;
     68
     69 }
     70
     71 void eMsg(char *M){
     72
     73         perror(M);
问题回答

正如我可以看到的那样,在服务器上,你要求寄发文件,复印数据来自fd(症状说明书)至nsd(摘要)。 因此,你将文件内容抄送给记本员。 但是,为了在客户中取出这一内容,你必须从客户方面收到的数据。

既然你没有这样做,那就使你无法在客户一方存档。

插入e.g。

    buffer[strcspn(buffer, "
")] =   ;
    int fd = open(buffer, O_CREAT|O_WRONLY, 0666);
    while (n = read(sd, buffer, 256), n > 0) write(fd, buffer, n);
    close(fd);

附 件

    close(sd);

(待补) 当然,较高命令的缓冲规模会提高效率。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签