参考:http://www.jb51.net/article/53732.htm
本博客主要实现了简单win7系统下,两台电脑之间(客户机与服务器)的文件传输,主要是基于Windows网络编程中winsock来实现的,当然这个只是简单的实现,你可以根据你的需求在你的项目里面进行更改。这里主要实现的通过客户机(Client) 通过监听服务器(Server)的需求进而向服务器传输指定文件。
操作步骤:
(1) 首先打开你的cmd控制台,查看你的端口号,输入命令:netstat -ano,如下图所示,TCP后面红色矩形框就是你部分的端口号。在client.cpp和server.cpp这两个文件中将“#define PORT 1029“改为你的可用端口号,比如“#define PORT 8128“,其中8128就是你某个可用端口号。
(2) 如果你在同一台电脑上面进行模拟,是不用更改IP的,当然如果是两台电脑,这里就需要更改你的client.cpp和server.cpp这两个文件中#define SERVER_IP“127.0.0.1”,更改为你的Server服务器IP地址,查看你IP地址的方法为:打开你的打开你的cmd控制台,输入命令:ipconfig
(3) 然后新建两个工程即可,分别添加下面两个源文件,先运行客户端代码,然后运行服务器代码,输入想要接收客户端的文件名,就可以在你的服务器工程下接收到相应的文件。
服务器代码:
server.cpp
/*************************************************************************
> File Name: Win_Server.c
> Author: SongLee
************************************************************************/
#include
#include
#include
#include
//#define PORT 8082
#define PORT 1029 //需要更改你可用的端口
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
#pragma comment(lib, "WS2_32")
int main()
{
// 声明并初始化一个服务端(本地)的地址结构
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// 初始化socket dll
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if(WSAStartup(socketVersion, &wsaData) != 0)
{
printf("Init socket dll error!");
exit(1);
}
// 创建socket
SOCKET m_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == m_Socket)
{
printf("Create Socket Error!");
exit(1);
}
//绑定socket和服务端(本地)地址
if (SOCKET_ERROR == bind(m_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
{
printf("Server Bind Failed: %d", WSAGetLastError());
exit(1);
}
//监听
if (SOCKET_ERROR == listen(m_Socket, 10))
{
printf("Server Listen Failed: %d", WSAGetLastError());
exit(1);
}
while(1)
{
printf("Listening To Server...\n"); //监听服务器需求
sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
SOCKET m_New_Socket = accept(m_Socket, (sockaddr *)&client_addr, &client_addr_len);
if (SOCKET_ERROR == m_New_Socket)
{
printf("Server Accept Failed: %d", WSAGetLastError());
break;
}
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
if (recv(m_New_Socket, buffer, BUFFER_SIZE, 0) < 0)
{
printf("Client Receive Data Failed!");
break;
}
char file_name[FILE_NAME_MAX_SIZE+1];
memset(file_name, 0, FILE_NAME_MAX_SIZE+1);
strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE ? FILE_NAME_MAX_SIZE:strlen(buffer));
printf("%s\n", file_name);
FILE * fp = fopen(file_name, "rb"); //windows下是"rb",表示打开一个只读的二进制文件
if (NULL == fp)
{
printf("File: %s Not Found\n", file_name);
}
else
{
memset(buffer, 0, BUFFER_SIZE);
int length = 0;
while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
{
if (send(m_New_Socket, buffer, length, 0) < 0)
{
printf("Send File: %s Failed\n", file_name);
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
fclose(fp);
printf("File: %s Transfer Successful!\n", file_name);
}
closesocket(m_New_Socket);
}
closesocket(m_Socket);
//释放winsock库
WSACleanup();
return 0;
}
客户机代码:
client.cpp
/*************************************************************************
> File Name: Win_Client.c
> Author: SongLee
************************************************************************/
#include
#include
#include
#include
//#define PORT 8082
#define PORT 1029 //需要更改你可用的端口
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
#pragma comment(lib, "WS2_32")
int main()
{
// 初始化socket dll
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if(WSAStartup(socketVersion, &wsaData) != 0)
{
printf("Init socket dll error!");
exit(1);
}
while(1){
//创建socket
SOCKET c_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == c_Socket)
{
printf("Create Socket Error!");
system("pause");
exit(1);
}
//指定服务端的地址
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
if (SOCKET_ERROR == connect(c_Socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
{
printf("Can Not Connect To Client IP!\n");
system("pause");
exit(1);
}
//输入文件名
char file_name[FILE_NAME_MAX_SIZE+1];
memset(file_name, 0, FILE_NAME_MAX_SIZE+1);
printf("Please Input File Name On Client: ");
scanf("%s", &file_name);
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE);
strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE ? BUFFER_SIZE:strlen(file_name));
//向服务器发送文件名
if(send(c_Socket, buffer, BUFFER_SIZE, 0) < 0)
{
printf("Send File Name Failed\n");
system("pause");
exit(1);
}
//打开文件,准备写入
FILE * fp = fopen(file_name, "wb"); //windows下是"wb",表示打开一个只写的二进制文件
if(NULL == fp)
{
printf("File: %s Can Not Open To Write\n", file_name);
system("pause");
exit(1);
}
else
{
memset(buffer, 0, BUFFER_SIZE);
int length = 0;
while ((length = recv(c_Socket, buffer, BUFFER_SIZE, 0)) > 0)
{
if (fwrite(buffer, sizeof(char), length, fp) < length)
{
printf("File: %s Write Failed\n", file_name);
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
printf("Receive File: %s From Client Successful!\n", file_name);
}
fclose(fp);
closesocket(c_Socket);
}
//释放winsock库
WSACleanup();
system("pause");
return 0;
}