Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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

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++;使用scketapi_C++_Sockets_Tcp_Client Server - Fatal编程技术网

C++ 在C++;使用scketapi

C++ 在C++;使用scketapi,c++,sockets,tcp,client-server,C++,Sockets,Tcp,Client Server,我正在做的客户端和服务器,是能够发送和接收文件。但我真的找不到任何有用的函数来满足我的目的 在客户端,我告诉服务器要下载哪个文件,在哪个端口,在服务器上,我告诉端口,并限制下载速度(kB/s)。我知道如何进行连接,但我真的不知道如何选择文件,如何发送和接收文件+如何设置下载速度限制。(我应该使用套接字API和TCP协议)这是一个可以从中开始的东西 服务器 /*Receive File from Client */ #define LENGTH 512 char* fr_name = "/

我正在做的客户端和服务器,是能够发送和接收文件。但我真的找不到任何有用的函数来满足我的目的


在客户端,我告诉服务器要下载哪个文件,在哪个端口,在服务器上,我告诉端口,并限制下载速度(kB/s)。我知道如何进行连接,但我真的不知道如何选择文件,如何发送和接收文件+如何设置下载速度限制。(我应该使用套接字API和TCP协议)

这是一个可以从中开始的东西

服务器

/*Receive File from Client */
#define LENGTH 512
    char* fr_name = "/file.jpg";
    FILE *fr = fopen( fr_name, "wb");
    if( fr == NULL)
        printf("File %s Cannot be opened file on server.\n", fr_name);
    else
    {
        bzero( revbuf, LENGTH); 
        int fr_block_sz = 0;

        bpt::ptime start, stop;
        start = bpt::microsec_clock::local_time();
        int totalBytes = 0;
        double transferRate = 0.0;

        while((fr_block_sz = recv( nsockfd, revbuf, LENGTH, 0)) > 0) 
        {
            stop = bpt::microsec_clock::local_time();
            bpt::time_duration dur = stop - start;
            double seconds = dur.total_milliseconds() / 1000.0;
            totalBytes += fr_block_sz;
            transferRate = totalBytes / seconds; // b/s

            int write_sz = fwrite( revbuf, sizeof(char), fr_block_sz, fr);
            if( write_sz < fr_block_sz)
            {
                error( "File write failed on server.\n");
            }
            bzero( revbuf, LENGTH);
            if ( fr_block_sz == 0 || fr_block_sz != 512) 
            {
                break;
            }
        }
        if(fr_block_sz < 0)
        {
            if (errno == EAGAIN)
            {
                printf("recv() timed out.\n");
            }
            else
            {
                fprintf(stderr, "recv() failed due to errno = %d\n", errno);
                exit(1);
            }
        }
        printf("Ok received from client!\n");
        fclose(fr); 
    }
/*从客户端接收文件*/
#定义长度512
char*fr_name=“/file.jpg”;
文件*fr=fopen(fr_名称,“wb”);
如果(fr==NULL)
printf(“无法在服务器上打开文件%s。\n”,fr\u名称);
其他的
{
bzero(revbuf,长度);
int fr_block_sz=0;
bpt::ptime开始、停止;
开始=bpt::微秒时钟::本地时间();
int totalBytes=0;
双转移率=0.0;
而((fr_block_sz=recv(nsockfd,revbuf,LENGTH,0))>0)
{
停止=bpt::微秒时钟::本地时间();
bpt::time_dur=停止-启动;
双秒=dur.total_毫秒()/1000.0;
totalBytes+=fr_block_sz;
传输速率=总字节/秒;//b/s
int write_sz=fwrite(revbuf,sizeof(char),fr_block_sz,fr);
if(写入_sz
客户

FILE *fs = fopen(fs_name, "rb");
    if(fs == NULL)
    {
        printf("ERROR: File %s not found.\n", fs_name);
        exit(1);
    }

    bzero(sdbuf, LENGTH); 
    int fs_block_sz;

    bpt::ptime start, stop;
    start = bpt::microsec_clock::local_time();
    int totalBytes = 0;
    double transferRate = 0.0;

    while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
    {
        if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
        {
            fprintf(stderr, "ERROR: Failed to send file %s. (errno = %d)\n", fs_name, errno);
            break;
        }
        stop = bpt::microsec_clock::local_time();
        bpt::time_duration dur = stop - start;
        double seconds = dur.total_milliseconds() / 1000.0;
        totalBytes += fs_block_sz;
        transferRate = totalBytes / seconds; // b/s

        bzero(sdbuf, LENGTH);
    }
FILE*fs=fopen(fs_名称,“rb”);
如果(fs==NULL)
{
printf(“错误:找不到文件%s。\n”,fs\U名称);
出口(1);
}
bzero(sdbuf,长度);
int fs_block_sz;
bpt::ptime开始、停止;
开始=bpt::微秒时钟::本地时间();
int totalBytes=0;
双转移率=0.0;
而((fs_block_sz=fread(sdbuf,sizeof(char),LENGTH,fs))>0)
{
如果(发送(sockfd,sdbuf,fs_block_sz,0)<0)
{
fprintf(stderr,“错误:发送文件%s失败。(错误号=%d)\n”,fs\u名称,错误号);
打破
}
停止=bpt::微秒时钟::本地时间();
bpt::time_dur=停止-启动;
双秒=dur.total_毫秒()/1000.0;
totalBytes+=fs_block_sz;
传输速率=总字节/秒;//b/s
bzero(sdbuf,长度);
}
您可以使用

#包括
开始、结束的时间;
时间(&开始);
.
.
.
.
.
.
时间(&结束);
双dif=difftime(结束,开始);
printf(“弹性时间为%.2lf秒”,dif);

有关打开和读取文件的信息,请查看
ifstream
类和相关文件。您可能会将文件读入缓冲区,然后从缓冲区读入套接字。您无法真正控制服务器的下载速率,但您可以通过将字节放入socketI的速率来近似它。我想我可以正常打开文件,读取一些字节数,然后使用send()和recv()发送它们。但是我怎样才能接近速度呢?可能使用usleep()函数?为什么要检查fr_block_sz!=512? 这是在处理下载速度吗?+为什么在memset上使用bzero?这方面有什么进步吗?bzero效率更高,因为它只会对内存进行调零,所以它不需要像memset那样做任何额外的检查。但是,如果代码必须是标准的,则应该使用memset。bzero不受欢迎。该检查是因为#定义长度512
#include <time.h>
time_t start,end;
time (&start);
.
.
.
<your code>
.
.
.
time (&end);
double dif = difftime (end,start);
printf ("Elasped time is %.2lf seconds.", dif );