如何阅读和C写的tar文件? [英] How to read and write tar file in C?

查看:173
本文介绍了如何阅读和C写的tar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的tar文件,并使用C.什么我米以下这里的程序写入到另一个tar文件是:


  1. 创建文件夹的tar文件

  2. 编写客户端套接字程序使用FREAD函数读取tar文件作为二进制文件用C

  3. 编写任何缓冲区来插座

  4. 编写服务器套接字程序来接收发送的数据到缓冲区

  5. 编写接收缓冲区到另一个tar文件。

  6. 关闭文件和插座。

下面是code:

Server.c

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; SYS / un.h>#定义MAX 1024
#定义SOCK_PATH的/ tmp / foo的INT S,S2,T,LEN;
结构sockaddr_un本地,远程;无效createSSocket()
{
    如果((S =插座(AF_UNIX,SOCK_STREAM,0))== - 1)
    {
        PERROR(插座);
        出口(1);
    }
    的printf(\\ n服务器套接字创建......);
}无效setSSocketPath()
{
    local.sun_family = AF_UNIX;
    的strcpy(local.sun_path,SOCK_PATH);
    取消链接(local.sun_path);
    LEN = strlen的(local.sun_path)+的sizeof(local.sun_family);
    的printf(\\ n服务器套接字路径设置...);
}无效bindSocket()
{
    如果(绑定(S,(结构sockaddr *)及本地,LEN)== -1)
    {
        PERROR(绑定);
        出口(1);
    }
    的printf(\\ nSocket绑定...);
}无效listenSocket()
{
    如果(听(S,5)== -1)
    {
        PERROR(听);
        出口(1);
    }
    的printf(\\ nListening插座......);
}无效acceptConnection()
{
    的printf(\\ nWaiting一个连接...);
    T = sizeof的(远程);
    如果((S2 =接受(S,(结构sockaddr *)及远程,电话电报公司))== -1)
    {
        PERROR(接受);
        出口(1);
    }
    的printf(\\ n服务器插座进行连接......);
}无效closeSSocket()
{
    接近(S);
    关闭(S2);
    的printf(\\ n服务器关闭socket ...);
}
无效receiveTar()
{
    INT LEN,RET;
    焦炭BUF [MAX] = {0};
    焦炭路径[MAX] =/home/priyanka/$c$cs/3455.tgz;
    FILE * FP;
    FP = FOPEN(路径,WB);    而((LEN = recv的(S2,buf中,MAX,0))大于0)
    {
        BUF [长度] = 0;
        的printf(\\ nReceived数:%d,LEN);
        //的fputs(BUF,FP);
        //的printf(%S,BUF);
        RET = FWRITE(BUF,1,strlen的(BUF),FP);
        如果(RET == -1)
        {
            PERROR(写入文件时出错);
        }
        的printf(写数:%d,RET);
    }
    FCLOSE(FP);
}诠释的main()
{
    createSSocket();
    setSSocketPath();
    bindSocket();
    listenSocket();
    acceptConnection();    receiveTar();    closeSSocket();
    返回0;
}

服务器程序的输出:

服务器套接字创建...

服务器套接字路径设置...

套接字绑定...

监听套接字...

等待连接...

服务器插座进行连接...

收稿日期:1024写:1024

收稿日期:459写:459

收稿日期:239写:239

收稿日期:529写:529

收稿日期:425写:425

收稿日期:411写:411

收稿日期:493写:493

收稿日期:142写:142

收稿日期:1024写:1024

收稿日期:397写:397

收稿日期:41写:41

收稿日期:158写:158

收稿日期:1024写:1024

收稿日期:705写:705

收稿日期:505写:505

收稿日期:1024写:1024

收稿日期:87写:87

收稿日期:1024写:1024

收稿日期:326写:326

收稿日期:234写:234

收稿日期:311写:311

收稿日期:819写:819

收稿日期:571写:571

收稿日期:1024写:1024

收稿日期:1024写:1024

收稿日期:341写:341

收稿日期:243写:243

收稿日期:630写:630

收稿日期:50写:50

收稿日期:35写:35

收稿日期:215写:215

服务器关闭socket ...

Client.c

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; SYS / un.h>#定义MAX 1024
#定义SOCK_PATH的/ tmp / foo的
INT S,T,LEN;
结构sockaddr_un遥控;无效createCSocket()
{
    如果((S =插座(AF_UNIX,SOCK_STREAM,0))== - 1)
    {
        PERROR(插座);
        出口(1);
    }
    的printf(\\ nClient插座创建......);
}无效setCSocketPath()
{
    remote.sun_family = AF_UNIX;
    的strcpy(remote.sun_path,SOCK_PATH);
    LEN = strlen的(remote.sun_path)+的sizeof(remote.sun_family);
    的printf(\\ nClient套接字路径设置...);
}无效connectSocket()
{
    的printf(\\ nTrying连接...);
    如果(连接(S,(结构sockaddr *)及远程,LEN)== -1)
    {
        PERROR(连接);
        出口(1);
    }
    的printf(\\ nClient连接... \\ n);
}无效closeCSocket()
{
    接近(S);
    的printf(\\ nClient插座关闭......);
}无效sendTar()
{
    FILE * FP;
    INT RET,LEN;
    焦炭BUF [MAX] = {0};
    FP = FOPEN(/家庭/普里/ 3455.tgz,RB);
    而(LEN = FREAD(BUF,1,1024,FP))
    //而((BUF [0] =龟etc(FP))!= EOF)
    {
        的printf(\\ NREAD数:%d,LEN);
        RET =发送(S,BUF,strlen的(BUF),0);
        的printf(已发送数:%d,RET);
        如果(RET == -1)
        {
            PERROR(错误发送数据:客户端);
        }
    }
    FCLOSE(FP);
}诠释的main()
{
    createCSocket();
    setCSocketPath();
    connectSocket();    sendTar();    closeCSocket();
    返回0;
}

客户端程序的输出:

客户端的Socket创建...

客户端套接字路径设置...

正在尝试连接...

客户端连接...

阅读:1024发送:3

阅读:1024发送:1027

阅读:1024发送:273

阅读:1024发送:180

阅读:1024发送:239

阅读:1024发送:529

阅读:1024发送:425

阅读:1024发送:411

阅读:1024发送:493

阅读:1024发送:142

阅读:1024发送:1027

阅读:1024发送:394

阅读:1024发送:41

阅读:1024发送:158

阅读:1024发送:702

阅读:1024发送:1027

阅读:1024发送:503

阅读:1024发送:2

阅读:1024发送:1027

阅读:1024发送:84

阅读:1024发送:1027

阅读:1024发送:323

阅读:1024发送:234

阅读:1024发送:311

阅读:1024发送:819

阅读:1024发送:571

阅读:1024发送:1027

阅读:1024发送:1027

阅读:1024发送:335

阅读:1024发送:243

阅读:1024发送:630

阅读:1024发送:50

阅读:1024发送:35

阅读:315发送:215

客户端的Socket关闭...

我米面临的问题一样:


  1. 不管我在只有它的几个字节的客户端程序M-阅读实际发送,仍然接收功能是读取字节的最大数量。而我希望我的程序读取1024个字节,发送1024个字节,收到1024个字节,写1024字节,这是不是在这种情况下发生的。将发送缓冲区长度帮助理清问题?


  2. 这是我为使用正确的一个插座上发送tar文件还是有做??

  3. 不同的方式方法
  4. 如何检查数据是否成功读取,发送,接收和书面??



解决方案

您正在阅读与 FREAD 的二进制文件,并把块,好像他们是字符串:

  = RET发送(S,BUF,strlen的(BUF),0);

使用这个代替送只 LEN 字节。

  = RET发送(S,BUF,LEN,0);

同样,服务器端程序应该只写了 LEN 字节接收。追加'\\ 0'可能会导致缓冲区溢出,写的strlen(BUF)字节将无法写入所有如果接收到NULL字节字节接收,因为肯定会如果tar文件被发送的情况下:

逻辑更改为:

  = RET fwrite的(BUF,1,LEN,FP);

您可能还需要使用的netcat实用程序:男人NC

I want to read the tar file and write it to another tar file using C. The procedure what I m following here is :

  1. Creating a tar file of the folder
  2. Writing client socket program for reading that tar file as binary file in C using fread function
  3. Writing whatever comes in buffer to socket
  4. Writing server socket program to receive sent data into a buffer
  5. Writing the received buffer into the another tar file.
  6. Closing files and socket.

Here is code :

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define MAX 1024
#define SOCK_PATH "/tmp/foo"

int s, s2, t, len;
struct sockaddr_un local, remote;

void createSSocket()
{
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
    {
        perror("socket");
        exit(1);
    }
    printf("\nServer Socket Created...");
}

void setSSocketPath()
{
    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, SOCK_PATH);
    unlink(local.sun_path);
    len = strlen(local.sun_path) + sizeof(local.sun_family);
    printf("\nServer Socket Path Set...");
}

void bindSocket()
{
    if (bind(s, (struct sockaddr *)&local, len) == -1) 
    {
        perror("bind");
        exit(1);
    }
    printf("\nSocket Binded...");
}

void listenSocket()
{   
    if (listen(s, 5) == -1) 
    {
        perror("listen");
        exit(1);
    }
    printf("\nListening Socket...");
}

void acceptConnection()
{
    printf("\nWaiting for a connection...");
    t = sizeof(remote);
    if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) 
    {
        perror("accept");
        exit(1);
    }
    printf("\nServer Socket Connected...");
}

void closeSSocket()
{
    close(s);
    close(s2);
    printf("\nServer Socket Closed...");
}


void receiveTar()
{
    int len,ret;
    char buf[MAX] = {0};
    char path[MAX] = "/home/priyanka/Codes/3455.tgz";
    FILE* fp;
    fp = fopen(path,"wb");

    while((len = recv(s2,buf,MAX,0)) > 0)
    {
        buf[len] = 0;
        printf("\nReceived : %d",len);
        //fputs(buf,fp);
        //printf("%s",buf);
        ret = fwrite(buf,1,strlen(buf),fp);
        if(ret == -1)
        {
            perror("Error writing to file");
        }
        printf("    Write : %d",ret);
    }
    fclose(fp);
}

int main()
{
    createSSocket();    
    setSSocketPath();
    bindSocket();
    listenSocket();
    acceptConnection(); 

    receiveTar();

    closeSSocket();
    return 0;   
}

Output of Server Program :

Server Socket Created...
Server Socket Path Set...
Socket Binded...
Listening Socket...
Waiting for a connection...
Server Socket Connected...
Received : 1024 Write : 1024
Received : 459 Write : 459
Received : 239 Write : 239
Received : 529 Write : 529
Received : 425 Write : 425
Received : 411 Write : 411
Received : 493 Write : 493
Received : 142 Write : 142
Received : 1024 Write : 1024
Received : 397 Write : 397
Received : 41 Write : 41
Received : 158 Write : 158
Received : 1024 Write : 1024
Received : 705 Write : 705
Received : 505 Write : 505
Received : 1024 Write : 1024
Received : 87 Write : 87
Received : 1024 Write : 1024
Received : 326 Write : 326
Received : 234 Write : 234
Received : 311 Write : 311
Received : 819 Write : 819
Received : 571 Write : 571
Received : 1024 Write : 1024
Received : 1024 Write : 1024
Received : 341 Write : 341
Received : 243 Write : 243
Received : 630 Write : 630
Received : 50 Write : 50
Received : 35 Write : 35
Received : 215 Write : 215
Server Socket Closed...

Client.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define MAX 1024
#define SOCK_PATH "/tmp/foo"


int s, t, len;
struct sockaddr_un remote;

void createCSocket()
{
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 
    {
        perror("socket");
        exit(1);
    }
    printf("\nClient Socket Created...");
}

void setCSocketPath()
{
    remote.sun_family = AF_UNIX;
    strcpy(remote.sun_path, SOCK_PATH);
    len = strlen(remote.sun_path) + sizeof(remote.sun_family);
    printf("\nClient Socket Path Set...");
}

void connectSocket()
{
    printf("\nTrying to connect...");
    if (connect(s, (struct sockaddr *)&remote, len) == -1) 
    {
        perror("connect");
        exit(1);
    }
    printf("\nClient Connected...\n");
}

void closeCSocket()
{
    close(s);
    printf("\nClient Socket Closed...");
}

void sendTar()
{
    FILE *fp;
    int ret,len;
    char buf[MAX] = {0};
    fp = fopen("/home/priyanka/3455.tgz","rb");
    while(len = fread(buf,1,1024,fp))
    //while((buf[0] = fgetc(fp)) != EOF)
    {
        printf("\nRead : %d",len);
        ret = send(s,buf,strlen(buf),0);
        printf("    Sent : %d",ret);
        if(ret == -1)
        {
            perror("Error sending data : Client");
        }
    }
    fclose(fp);
}

int main()
{
    createCSocket();
    setCSocketPath();
    connectSocket();

    sendTar();

    closeCSocket(); 
    return 0;
}

Output of Client Program :

Client Socket Created...
Client Socket Path Set...
Trying to connect...
Client Connected...

Read : 1024 Sent : 3
Read : 1024 Sent : 1027
Read : 1024 Sent : 273
Read : 1024 Sent : 180
Read : 1024 Sent : 239
Read : 1024 Sent : 529
Read : 1024 Sent : 425
Read : 1024 Sent : 411
Read : 1024 Sent : 493
Read : 1024 Sent : 142
Read : 1024 Sent : 1027
Read : 1024 Sent : 394
Read : 1024 Sent : 41
Read : 1024 Sent : 158
Read : 1024 Sent : 702
Read : 1024 Sent : 1027
Read : 1024 Sent : 503
Read : 1024 Sent : 2
Read : 1024 Sent : 1027
Read : 1024 Sent : 84
Read : 1024 Sent : 1027
Read : 1024 Sent : 323
Read : 1024 Sent : 234
Read : 1024 Sent : 311
Read : 1024 Sent : 819
Read : 1024 Sent : 571
Read : 1024 Sent : 1027
Read : 1024 Sent : 1027
Read : 1024 Sent : 335
Read : 1024 Sent : 243
Read : 1024 Sent : 630
Read : 1024 Sent : 50
Read : 1024 Sent : 35
Read : 315 Sent : 215
Client Socket Closed...

I m facing problems like :

  1. Whatever I m reading in client program only few bytes of it are actually sent and still the receive function is reading MAX number of bytes. Whereas I want my program to read 1024 bytes, sent 1024 bytes, receive 1024 bytes and write 1024 bytes, which is not happening in this case. Will sending length of buffer help to sort out the problem??

  2. Is it the way I m using the right one to send tar file over socket or are there different ways of doing it??

  3. How to check if the data is successfully read, sent, received and written??

解决方案

You are reading a binary file with fread and sending chunks as if they were strings:

        ret = send(s,buf,strlen(buf),0);

Use this instead to send just len bytes.

        ret = send(s,buf,len,0);

Similarly, the server program should just write the len bytes received. Appending a '\0' may cause a buffer overflow and writing strlen(buf) bytes will fail to write all the bytes received if a NUL byte is received, as will certainly be the case if a tar file is transmitted:

Change the logic to:

    ret = fwrite(buf,1,len,fp);

You may also want to use the netcat utility: man nc

这篇关于如何阅读和C写的tar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆