通过TCP发送文件| C,Windows

时间:2014-12-27 19:16:12

标签: c file tcp client server

您好我试图将文件从服务器发送到客户端 我确实发了一条消息,确实有效 但对于文件它只是不起作用.. 也许它的语法问题 你知道为什么吗?

客户:

#include<stdio.h>
#include<winsock2.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main()
{
    WSADATA wsa;// Struct that winsock2 has
    SOCKET s; // the socket
        size_t nRecv,nWrite;
FILE *fp;


    struct sockaddr_in server; // built-in struct
        /*struct sockaddre_in
    {
        short  sin_family; // Address Faimly (IPV4 / IPV6)
        unsigned short sin_port; // which port the data will transfer e.g htons(80)
        struct in_addr sin_addr;
        char sin_zero[8];
    }server;*/
    char *message , server_reply[2000]; // the data we send and the server reply
    int recv_size; // the size of the recived data

    // ------------------------------------------Checking the DLL verision --------------------------------------- //


    // - First we have to initialising the winsock dll to see if there are any errors while loading it //

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)  // if there are any error WSAStartup will return non-zero value .
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Step 1 : Initialised.\n");

        // ---------------------------------------------Creating the Socket ----------------------------------------- //

    // - AF_INET - Address Family (IP version , This case is IPV4)
    //- SOCK_STREAM - this is the type , what type of communction would be this case is TCP Which could STREAM
    // - 0 is the TCP protocol.

    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET) // if the returned value of socket() INVALID_SOCKET
    {
        printf("Could not create socket : %d" , WSAGetLastError());
        return 1;
    }

    printf("Step 2 : Socket created.\n");

         // --------------------------------------- Connecting to the SERVER ----------------------------------------//

    // inet_addr() converting a IP to a long format .

    server.sin_addr.s_addr = inet_addr("10.0.0.4"); // <-- here we have to insert the ip of the server
    server.sin_family = AF_INET; // IPV4 in this case .
    server.sin_port = htons( 9999 ); // the wanted port (HTML = 80)

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)//casing server value to sockaddr struct
    {
        puts("connect error");
        return 1;
    }

    puts("Step 3 : Connected");

//-----------------------------------------------------Recieve from the Server ------------------------------//

fp = fopen("output.txt", "wb");
    if (fp == NULL)
    {
        printf("File not found!\n");
        return 1;
    }
    else
    {
        printf("created file output.txt\n");
    }  


while (recv_size = recv(s, server_reply, 256, 0))
{
    if (recv_size == SOCKET_ERROR) {
        puts("recv failed");
        return 1;
    }
    else
    {
         nWrite = fwrite(server_reply, sizeof(char), recv_size, fp);
    }

if(nWrite<=0)
{
printf("error while reading the file");
return 1;
}
else{
puts("Step 4 :Reply received\n");
}

}



    return 0;
}

服务器:

#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main()
{

        File *fp;
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char message[200],TheData[256];
        size_t nRead,nSent

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("10.0.0.4");
    server.sin_port = htons( 9999 );

    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
        return 1;
    }

    puts("Bind done");

    //Listen to incoming connections
    listen(s , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");

    c = sizeof(struct sockaddr_in);

   while ((new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET) {
    printf ("Server: ");

    pf = fopen("input.txt", "rb");

    while (!feof(pf))
    {
        nRead = fread(TheData, sizeof(char), 256, pf);
        if (nRead <= 0)
{
            printf("ERROR reading file");
                return 1;
}
        while (nRead > 0)
        {
            nSent = send(new_socket, TheData, nRead, 0);
            if (nSent < 0)
            {

                    printf("ERROR sending from socket = %d\n", WSAGetLastError());
                    return 1;

            }
            if (nSent == 0)
{
                printf("DISCONNECTED writing to socket");
                return 1;
}

}


    closesocket(s);
    WSACleanup();

    return 0;
}

0 个答案:

没有答案