windows 下实现socket编程_传送文件
其实,Windows下的socket编程与Linux下的类似,这里不再赘述!
参考:http://blog.csdn.net/chudongfang2015/article/details/51926999
下面直接上代码:
cilent.cpp:
/*************************************************************************
= File Name: Win_Client.c
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#define PORT 8087 //端口号
#define SERVER_IP "127.0.0.1"//本地环回地址,
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512 //文件名最大值
#pragma comment(lib, "WS2_32")//加入相应的库
int main()
{
//
WSADATA wsaData;
WORD socketVersion = MAKEWORD(2, 0);
if(WSAStartup(socketVersion, &wsaData) != 0)
{
printf("Init socket dll error!");
exit(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)))//连接client
{
printf("Can Not Connect To Server 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 Server: \n");
scanf("%s",file_name);
//把文件名考到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, "rt");
if(NULL == fp)
{
printf("File: %s Can Not Open To Write\n", file_name);
system("pause");
exit(1);
}
else
{
memset(buffer, 0, BUFFER_SIZE);//清空buffer区域,
int length = 0;
//发送文件内容,直到读不到文件
while((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
{
if (send(c_Socket, buffer, length, 0) < 0)//向服务端发送
{
printf("Send File: %s Failed\n", file_name);
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
printf("File: %s Transfer Successful!\n", file_name);
}
fclose(fp); //关闭文件
closesocket(c_Socket);//关闭socket
//释放winsock库
WSACleanup();
system("pause");
return 0;
}
server.cpp:
/*************************************************************************
> File Name: Win_Server.c
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <WinSock2.h>
#define PORT 8087 //端口号
#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 Client...\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("Server 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, "wt");//以写入的方式打开文件
if (NULL == fp)
{
printf("File: %s Not Found\n", file_name);
}
else
{
memset(buffer, 0, BUFFER_SIZE);
int length = 0;
//接受到客户端发来的文件数据
while ((length = recv(m_New_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 Server Successful!\n", file_name);
}
fclose(fp); //关闭文件描述符
closesocket(m_New_Socket);
}
closesocket(m_Socket);
//释放winsock库
WSACleanup();
return 0;
}