Server.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma comment(lib, "ws2_32.lib")
 
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <WinSock2.h>
 
#define BUFSIZE 512
 
int main(int argc, char *argv[]) {
    WSADATA wsaData;
    SOCKET  ConnectSocket = INVALID_SOCKET;
    SOCKADDR_IN addrinfo;
    int iResult;
 
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
        return 1;
 
    if ((ConnectSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        return 1;
 
    ZeroMemory(&addrinfo, sizeof(addrinfo));
    addrinfo.sin_family = AF_INET;
    addrinfo.sin_port = htons(atoi(argv[2]));
    addrinfo.sin_addr.s_addr = inet_addr(argv[1]);
 
    if (connect(ConnectSocket, (SOCKADDR *)&addrinfo, sizeof(addrinfo)) == SOCKET_ERROR)
        return 1;
 
    FILE *fp = fopen(argv[3], "rb");
    if (fp == NULL) {
        printf("File I/O error\n");
        return 1;
    }
 
    // Send filename
    char filename[256];
    ZeroMemory(filename, 256);
    sprintf(filename, argv[3]);
    if (send(ConnectSocket, filename, 256, 0) == SOCKET_ERROR)
        return 1;
 
    // Get file size
    fseek(fp, 0, SEEK_END);
    int filesize = ftell(fp);
 
    // Send file size
    if (send(ConnectSocket, (char *)&filesize, sizeof(filesize), 0) == SOCKET_ERROR)
        return 1;
 
    char buf[BUFSIZE];
    int read;
    int total = 0;
 
    // Send file contents
    rewind(fp);
    while (1) {
        read = fread(buf, 1, BUFSIZE, fp);
        if (read > 0) {
            iResult = send(ConnectSocket, buf, read, 0);
            if (iResult == SOCKET_ERROR)
                break;
            total += read;
        } else if (read == 0 && total == filesize) {
            printf("File transmission complete: %d bytes\n", filesize);
            break;
        } else {
            printf("File I/O error\n");
            break;
        }
    }
    fclose(fp);
 
    closesocket(ConnectSocket);
    WSACleanup();
    return 0;
}


Client.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma comment(lib, "ws2_32.lib")
 
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <WinSock2.h>
 
#define BUFSIZE 512
 
int recvn(SOCKET socket, char *buf, int len, int flags) {
    int     iResult;
    char    *ptr = buf;
    int     left = len;
 
    while (left > 0) {
        iResult = recv(socket, ptr, left, flags);
        if (iResult == SOCKET_ERROR)
            return SOCKET_ERROR;
        else if (iResult == 0)
            break;
 
        left -= iResult;
        ptr += iResult;
    }
 
    return (len - left);
}
 
int main(int argc, char *argv[]) {
    WSADATA wsaData;
    SOCKET  ListenSocket = INVALID_SOCKET;
    SOCKADDR_IN addrinfo;
    int iResult;
 
    if (WSAStartup(MAKEWORD(2, 2),   &wsaData) != 0)
        return 1;
 
    if ((ListenSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        return 1;
     
    ZeroMemory(&addrinfo, sizeof(addrinfo));
    addrinfo.sin_family = AF_INET;
    addrinfo.sin_port = htons(atoi(argv[2]));
    addrinfo.sin_addr.s_addr = htonl(INADDR_ANY);
 
    if (bind(ListenSocket, (SOCKADDR *)&addrinfo, sizeof(addrinfo)) == SOCKET_ERROR)
        return 1;
 
    if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
        return 1;
    printf("Listen!\n");
 
    SOCKET  SenderSocket;
    SOCKADDR_IN senderinfo;
    int     addrlen;
    char    buf[BUFSIZE];
 
    while (1) {
        addrlen = sizeof(senderinfo);
        SenderSocket = accept(ListenSocket, (SOCKADDR *)&senderinfo, &addrlen);
        if (SenderSocket == INVALID_SOCKET)
            continue;
 
        printf("File Sender: %s(%d)\n", inet_ntoa(senderinfo.sin_addr), ntohs(senderinfo.sin_port));
 
        // Receive filename from server
        char filename[256];
        ZeroMemory(filename, sizeof(filename));
        iResult = recvn(SenderSocket, filename, 256, 0);
        if (iResult == SOCKET_ERROR) {
            printf("Receive filename failed\n");
            closesocket(SenderSocket);
            continue;
        }
        printf("\tFilename: %s\n", filename);
 
        // Receive file size from server
        int filesize;
        iResult = recvn(SenderSocket, (char *)&filesize, sizeof(filesize), 0);
        if (iResult == SOCKET_ERROR) {
            closesocket(SenderSocket);
            continue;
        }
        printf("\tFile size: %d\n", filesize);
 
        // File open
        FILE *fp = fopen(filename, "wb");
        if (fp == NULL) {
            closesocket(SenderSocket);
            continue;
        }
 
        // Receive file contents
        int total = 0;
        while (1) {
            ZeroMemory(buf, sizeof(buf));
            iResult = recvn(SenderSocket, buf, BUFSIZE, 0);
            if (iResult == SOCKET_ERROR) {
                break;
            } else if (iResult == 0) {
                break;
            } else {
                fwrite(buf, 1, iResult, fp);
                if (ferror(fp)) {
                    printf("File I/O error\n");
                    break;
                }
                total += iResult;
            }
        }
        fclose(fp);
 
        // File size check
        if (total == filesize)
            printf("File download success\n");
        else
            printf("File download failed\n");
 
        closesocket(SenderSocket);
    }
 
    closesocket(ListenSocket);
    WSACleanup();
    return 0;
}