server.cpp
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <WinSock2.h>
#include<string.h>
#pragma comment(lib,"wsock32.lib")
using namespace std;
#define BUFF_SIZE (1024 * 256)
#define FILE_NAME_LENGTH 1024
int s;
int ns;
int getFileSize(char *filePath) {
FILE *f;
f = fopen(filePath, "rb");
if (NULL == f) {
printf("getFileSize fopen error\n");
return -1;
}
if (0 != fseek(f, 0, SEEK_END)) {
printf("getFileSize fseek error\n");
return -1;
}
int fileSize = ftell(f);
if (fileSize < 0) {
printf("ftell error\n");
}
printf("fileSize:%lld\n", fileSize);
fclose(f);
return fileSize;
}
char *getFileName(char *filePath) {
bool bFound = false;
char *buff = new char[1024];
memset(buff, 0, 1024);
while (!bFound) {
int lastIndex = 0;
for (int i = 0; i < strlen(filePath); ++i) {
if (filePath[i] == '\\' || filePath[i] == '/') {
lastIndex = i;
}
}
for (int i = lastIndex + 1; i < strlen(filePath); ++i) {
buff[i - lastIndex - 1] = filePath[i];
}
bFound = true;
}
return buff;
}
int main(int argc, char **argv)
{
unsigned short port;
char buff[BUFF_SIZE];
struct sockaddr_in client;
struct sockaddr_in server;
int namelen;
char *filePath = new char[FILE_NAME_LENGTH];
port = 23;
int fileSize;
char *fileName;
WSADATA wsadata;
WSAStartup(0x202, &wsadata);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket error\n");
exit(2);
}
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("bind error\n");
exit(3);
}
if (listen(s, 1) != 0)
{
printf("listen error\n");
exit(4);
}
namelen = sizeof(client);
if ((ns = accept(s, (struct sockaddr *)&client, &namelen)) == -1)
{
printf("accept error\n");
}
int fileRecv = 0;
FILE *f = NULL;
int totalFileSize = 0;
while (true) {
int iRecv = 0;
memset(buff, 0, BUFF_SIZE);
iRecv = recv(ns, buff, BUFF_SIZE, 0);
char recvstr[BUFF_SIZE];
strcpy(recvstr, buff);
if (iRecv < 0) {
printf("recv message error\n");
}
else
{
if (memcmp(buff, "filesize=", 9) == 0)
{
char tempbf[BUFF_SIZE];
strcpy(tempbf, &buff[9]);
totalFileSize = atoll(tempbf);
printf("%s,%d\n", buff, totalFileSize);
memset(buff, 0, BUFF_SIZE);
}
if (memcmp(buff, "fileName=", 9) == 0)
{
printf("buff=%s\n", buff);
char tempbf[BUFF_SIZE];
strcpy(tempbf, &buff[9]);
printf("tempbf=%s\n", tempbf);
strcpy(filePath, "D:\\client\\");
strcat(filePath, tempbf);
printf("filePath=%s\n", filePath);
f = fopen(filePath, "wb");
if (f == NULL) {
printf("file:%s doesn't exist and failed to create\n", filePath);
}
memset(tempbf, 0, BUFF_SIZE);
memset(buff, 0, BUFF_SIZE);
send(ns, "startsend", 9, 0);
}
if (memcmp(buff, "clientSend", 9) == 0)
{
while (true)
{
int iRecv = 0;
if (memcmp(buff, "clientSend", 9) != 0&&memcmp(buff, "SendFinish", 9) != 0)
{
memset(buff, 0, BUFF_SIZE);
iRecv = recv(ns, buff, BUFF_SIZE, 0);
}
char recvstr[BUFF_SIZE];
strcpy(recvstr, buff);
if (iRecv < 0) {
printf("recv message error\n");
}
else
{
if (fileRecv <= totalFileSize&&buff[0]!='\0'&&memcmp(buff, "SendFinish", 9) != 0) {
if (memcmp(buff, "clientSend", 9) != 0)
{
fileRecv += iRecv;
fwrite(buff, 1, iRecv, f);
printf("%s\n", buff);
}
memset(buff, 0, BUFF_SIZE);
send(ns, "recvEnd", 7, 0);
}
else
{
fclose(f);
printf("传输完成?\n");
break;
}
}
}
break;
}
}
}
system("pause");
}
client.cpp
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <WinSock2.h>
#include <time.h>
#pragma comment(lib,"wsock32.lib")
using namespace std;
#define BUFF_SIZE (1024 * 256)
#define FILE_NAME_LENGTH 1024
int s;
int getFileSize(char *filePath) {
FILE *f;
f = fopen(filePath, "rb");
if (NULL == f) {
printf("getFileSize fopen error\n");
return -1;
}
if (0 != fseek(f, 0, SEEK_END)) {
printf("getFileSize fseek error\n");
return -1;
}
int fileSize = ftell(f);
if (fileSize < 0) {
printf("ftell error\n");
}
printf("fileSize:%lld\n", fileSize);
fclose(f);
return fileSize;
}
char *getFileName(char *filePath) {
bool bFound = false;
char *buff = new char[1024];
memset(buff, 0, 1024);
while (!bFound) {
int lastIndex = 0;
for (int i = 0; i < strlen(filePath); ++i) {
if (filePath[i] == '\\' || filePath[i] == '/') {
lastIndex = i;
}
}
for (int i = lastIndex + 1; i < strlen(filePath); ++i) {
buff[i - lastIndex - 1] = filePath[i];
}
bFound = true;
}
return buff;
}
int main(int argc, char** argv)
{
WSADATA wsadata;
WSAStartup(0x202, &wsadata);
printf("start...\n");
unsigned short port;
char buf[BUFF_SIZE];
struct hostent *hostnm;
struct sockaddr_in server;
port = 23;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr("127.0.0.1") ;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Socket error\n");
}
printf("ready to connet to server ...\n");
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect error\n");
}
char buff[BUFF_SIZE];
char *filePath = new char[FILE_NAME_LENGTH];
filePath = "C:\\Users\\18131\\Desktop\\期货窗口截图\\易联众PACS 3.6影像浏览工作站使用说明书.pdf";
int fileSize = getFileSize(filePath);
printf("fileSize:%lld\n", fileSize);
char *fileName = getFileName(filePath);
printf("fileName:%s\n", fileName);
FILE *f;
f = fopen(filePath, "rb");
if (f == NULL) {
printf("file:%s doesn't exist\n", filePath);
exit(6);
}
int sendSize = 0;
_itoa(fileSize, buff, 10);
char tempbuff[BUFF_SIZE];
strcpy(tempbuff, buff);
sprintf(buff, "filesize=%s", tempbuff);
if (send(s, buff, sizeof(buff), 0) < 0) {
printf("send fileSize to client error\n");
}
else
{
memset(buff, 0, BUFF_SIZE);
printf("send fileSize to client sucess\n");
}
strcpy(tempbuff, fileName);
sprintf(fileName, "fileName=%s", tempbuff);
if (send(s, fileName, strlen(fileName), 0) < 0) {
printf("send fileName to client error\n");
}
else
{
memset(fileName, 0, strlen(fileName));
printf("send fileName to client sucess\n");
}
while (true)
{
int iRecv = 0;
memset(buff, 0, BUFF_SIZE);
iRecv = recv(s, buff, BUFF_SIZE, 0);
char recvEnd[BUFF_SIZE];
memset(recvEnd, 0, BUFF_SIZE);
if (memcmp(buff, "startsend", 9) == 0)
{
send(s, "clientSend", 10, 0);
while (sendSize < fileSize) {
if (memcmp(recvEnd, "recvEnd", 7) == 0)
{
memset(buff, 0, BUFF_SIZE);
size_t iread = fread(buff, 1, BUFF_SIZE, f);
printf("iread:%d\n", iread);
if (iread < 0) {
printf("fread error\n");
fclose(f);
break;
}
int iSend = send(s, buff, iread, 0);
if (iSend < 0) {
printf("send error\n");
fclose(f);
break;
}
else
{
memset(buff, 0, BUFF_SIZE);
}
sendSize += iSend;
printf("fileSize:%lld iSend:%d sendSize:%lld\n", fileSize, iSend, sendSize);
fseek(f, sendSize, SEEK_SET);
memset(recvEnd, 0, BUFF_SIZE);
}
recv(s, recvEnd, BUFF_SIZE, 0);
}
send(s, "SendFinish", 10, 0);
fclose(f);
break;
}
}
printf("Client Ended Successfully\n");
system("pause");
}
|