-1

I'm a new comer in this stackoverflow.
I successfully sent a file.txt and received it . And I want to send the file through a socket periodically and receive it periodically, too. This is a server client program. Client should send the data and server should receive it with an interval time.

Anyone know how to do this? I used this way:

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

Problem is the file is not sent.

Here is my client code :

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");

}

and here are my server code :

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");
}
Community
  • 1
  • 1
codeJr
  • 11
  • 5
  • `servSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);` `bind(servSocket, (sockaddr*)&addr, sizeof(addr));` You probably should add some code handling error situations that might occur, and inspect the error codes accordingly. – πάντα ῥεῖ Jun 15 '15 at 19:07
  • The client is written to send only once, not repeatedly with interval. What output do you see from the client? Specifically, does it ever print that it connected successfully? – donjuedo Jun 15 '15 at 19:18
  • no ..look like it doesn't. can u help me how to send it repeatedly ? – codeJr Jun 15 '15 at 19:22
  • I'd recommend printing the value returned from connect(), at least for debugging. My guess is it is an address problem, so while you're adding output for detected errors, you could add output of the address the code *thinks* it tried. – donjuedo Jun 15 '15 at 19:25
  • "I used this way"... um, nope. Don't see that you are doing *anything* from that link. – crashmstr Jun 15 '15 at 19:43

1 Answers1

0

For the periodic part, you could take everything between the start: and END: labels, and put it in a new function, say, sendMyFile();

Then in the client's _tmain(), between the start: and END: labels,

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

There are certainly better, more sophisticated ways to do something periodically, but this is a good, simple, educational approach.

donjuedo
  • 2,475
  • 18
  • 28
  • i did what you said. but the file content is strange when it arrives to the destination. – codeJr Jun 15 '15 at 21:17
  • So it is connecting successfully and printing "Socket Connected ......... \n". Is the server receiving strange file content *periodically*? – donjuedo Jun 15 '15 at 21:33
  • yes the socket connected . the server successfully prints "file received ". but the file content is strange – codeJr Jun 15 '15 at 21:43
  • What is in "B:\Filesend.txt", and what is received? What is strange about it? – donjuedo Jun 15 '15 at 21:45
  • Filesend.txt contains "this is a test text this is a test text this is a test text this is a test text this is a test text this is a test text this is a test text this is a test text this is a test text this is a test text " and the the received file contains "ㄲ0쳌쳌쳌췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍 " – codeJr Jun 15 '15 at 21:47
  • Despite the disappointing result, you do have a lot working to get this much. I will go out on a limb and say your send buffer is freed too soon, and somehow (I can't explain), the send() call is getting garbage from the freed memory. It's a long shot, but easy to test. Try allocating Buffer once outside of your periodic loop, and freeing once after the END: label, after a 5 second delay, just to make the test thorough. – donjuedo Jun 15 '15 at 21:58
  • hi.. the solution about the interval that you gave me above , was it only for the client ? how about the server ? is it needed to give the interval too for receiving the data sent by the client ? – codeJr Jun 16 '15 at 03:37
  • @codeJr: Yes, the interval is for the client only. Your existing server code already has a while loop in _tmain() to receive as many files as are sent. – donjuedo Jun 16 '15 at 10:15
  • when the file sent and received in server , the file is still original file for example : Filesend.txt contains 12345 for the first 10 seconds. before the next 10 seconds , i add 123456789 to Filesend.txt so the FileReceived.txt should be 123456789. but it doesn't change anything. i mean, i want the file send the change every 10 seconds if there is a change. – codeJr Jun 16 '15 at 10:33
  • It sounds like a new StackOverflow question. – donjuedo Jun 16 '15 at 11:06
  • okay.. thank you for your answer. i will post the new question – codeJr Jun 16 '15 at 11:45
  • @codeJr, was my answer to this question sufficient to accept? – donjuedo Jun 16 '15 at 12:31
  • actually it sends file but look like you said it's going to be new question. i post the question in http://stackoverflow.com/questions/30867235/c-sending-a-modified-file-periodically?noredirect=1#comment49776116_30867235 – codeJr Jun 16 '15 at 12:36