Server.cpp
#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
#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;
}