C ++ - 通过套接字定期发送文件

时间:2015-06-15 18:56:24

标签: c++ sockets

我是这个stackoverflow的新来者 我成功发送了一个file.txt并收到了它。我想定期通过套接字发送文件并定期接收它。这是一个服务器客户端程序。客户端应该发送数据,服务器应该以间隔时间接收它。

任何人都知道如何做到这一点?我用这种方式:

  

need to call a function at periodic time intervals in c++

问题是文件未发送。

这是我的客户代码:

void Inwinsock(){

    WSAStartup(MAKEWORD(2,2), &winsock);
    if (LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2 ) {

           WSACleanup();
    }
}




void ClientSock() {

    clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ZeroMemory(&addr, sizeof(addr));
    addr.sin_family = AF_INET; 
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(6091);

}




int _tmain(int argc, _TCHAR* argv[])
{

    Inwinsock();
    ClientSock();


start:

    if (connect(clientSock, (sockaddr*)&addr, sizeof(addr)) < 0 ) {

        Sleep(5000);
        goto start; 
    }

    printf("Socket Connected ......... \n");




    FILE *File;
    char *Buffer;
    unsigned long Size;

    File = fopen("B:\Filesend.txt","rb");
    if(!File){

        printf("", WSAGetLastError());
        goto END;

    } 

    printf("File open ok ! \n");


        fseek(File,0,SEEK_END);
        Size = ftell(File);
        fseek(File,0,SEEK_SET);
        printf("file size succeed...\n");


        Buffer = (char*) malloc (Size+1); 
        fread(Buffer,Size,1,File);
        char cisi[10];
        sprintf(cisi, "%i", Size);
        fclose(File);


        printf("sending data....\n");
        send(clientSock,cisi,10,0);   //file size sent
        send(clientSock,Buffer,Size,0); // File Binary sent
        free(Buffer);
        printf("sending finished....\n");

END:
    closesocket(clientSock);
    WSACleanup();
    getchar();
    return 0;
    system("PAUSE");

}
  

这是我的服务器代码:

void iniSocket() {

    WSAStartup(MAKEWORD(2,2), &winsock);
    if (LOBYTE(winsock.wVersion) != 2 || HIBYTE(winsock.wVersion) != 2) {

        WSACleanup();

    }

}




void opSock(){

    servSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ZeroMemory(&addr , sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6091);
    bind(servSocket, (sockaddr*)&addr, sizeof(addr));
}


void sockList(){

            if (listen(servSocket, 5) == SOCKET_ERROR ) {

                printf("listen error %d\n", WSAGetLastError());

            }else
            {
                printf("listen succeed....\n");

            }

}




void receive() {



                            if(recv(ClientAcc,Filesize,10,0)){

                                Size = atoi((const char*)Filesize);
                                printf("File size : %d\n",Size);

                            }



                            Buffer = (char*)malloc(Size + 1);
                            int file_dit, total_file = 0 ;

                            while(total_file < Size) 
                            {
                                ZeroMemory(Buffer, Size);
                                if((file_dit = recv(ClientAcc,Buffer,Size,0)) < 0)
                                {
                                    goto END;
                                }
                                else
                                {

                                    total_file += file_dit;

                                    File = fopen("fileReceived.txt", "wb");
                                    fwrite((const char*)Buffer,1,file_dit,File);
                                    fclose(File);
                                    Sleep(1000);

                                }


                                    END:

                                        printf("File received \n");
                                        free(Buffer);
                                        closesocket(ClientAcc);
                                        WSACleanup();
                                        getchar();

                            }



              }




int _tmain(int argc, _TCHAR* argv[])
{

    while(1)
    {

        iniSocket();
        opSock();
        sockList();


        if (ClientAcc = accept(servSocket, (sockaddr*)&incommingAddress, &addresslen))
        {

                            char *ClientIP = inet_ntoa(incommingAddress.sin_addr);
                            int ClientPort = ntohs (incommingAddress.sin_port);
                            printf("Client connected ....\n" );
                            printf("IP : %s:%d\n ", ClientIP, ClientPort);

                            receive();
        }

    }

        return 0;
        system("PAUSE");
}

1 个答案:

答案 0 :(得分:0)

对于周期性部分,您可以在start:和END:标签之间取一切,并将其放入一个新函数,例如sendMyFile();

然后在客户端的_tmain()中,在start:和END:标签之间,

while (1)
{
    sendMyFile();
    Sleep( 30000 );    //  The interval is 30,000.
}

肯定有更好,更复杂的方法定期做某事,但这是一种好的,简单的教育方法。