(1)大文件的传输。对于比较大的文件,应该进行分包操作,以防止占用过多的内存,导致文件发送
失败,实验中每次最多传输1024个字符;
(2)用户根据参数输入选择传输的文件和传输位置;
(3)发送端和接收端分别显示文件传输相应的信息,包括:对方IP地址,当前已写(读)文件长度等;
(4)对于Ctrl+C命令以及服务器提前关闭的特殊情况给出响应(如:显示信息)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll
#define BUF_SIZE 1024
//using namespace std; //直接用std好像会出现bug
using std::cout;
//封装套接字绑定
int make_socket(const char* address,int port)
{
//创建套接字
SOCKET Sock = socket(AF_INET, SOCK_STREAM, 0);
//绑定套接字
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = AF_INET; //使用IPv4地址
sockAddr.sin_addr.s_addr = inet_addr(address); //回送IP地址
sockAddr.sin_port = htons(1234); //端口
bind(Sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
return Sock;
}
//通过套接字获取IP、Port等地址信息
bool GetAddressBySocket(SOCKET m_socket, SOCKADDR_IN &m_address)
{
memset(&m_address, 0, sizeof(m_address));
int nAddrLen = sizeof(m_address);
//根据套接字获取地址信息
if (::getpeername(m_socket, (SOCKADDR*)&m_address, &nAddrLen) != 0)
{
printf("获取IP地址失败");
return false;
}
//读取IP和Port
return true;
}
int main()
{
//初始化dll
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
int servSock = make_socket("127.0.0.1", 1234);
//服务器socket进入监听状态
listen(servSock, 200);
//接收客户端请求
SOCKADDR clntAddr;
int nSize = sizeof(SOCKADDR);
char buffer[BUF_SIZE] = { 0 }; //缓冲区
while (1)
{
SOCKET clntSock = accept(servSock,&clntAddr, &nSize);
if (clntSock<=0)
{
cout << "连接客户端有误" << std::endl;
continue;
}
else
{
SOCKADDR_IN temp;
GetAddressBySocket(clntSock, temp);
cout << "客户端:" << ::inet_ntoa(temp.sin_addr) << "连接了服务器" << std::endl;
}
std::ofstream out("结果.txt", std::ios::app);
int recv_length = recv(clntSock, buffer, BUF_SIZE, 0);
int all_lenth = 0;
while (recv_length>0)
{
all_lenth += recv_length;
out << buffer;
memset(buffer, 0, BUF_SIZE); //重置缓冲区
cout << "已接受" << all_lenth <<"字节"<< std::endl;
recv_length = recv(clntSock, buffer, BUF_SIZE, 0);
}
out.close();
}
//关闭套接字
closesocket(servSock);
//终止 DLL 的使用
WSACleanup();
return 0;
}
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll
#define BUF_SIZE 1024
using std::cout;
using std::cin;
using std::endl;
//TODO 解决黏包问题
//通过套接字获取IP、Port等地址信息
bool GetAddressBySocket(SOCKET m_socket, SOCKADDR_IN &m_address)
{
memset(&m_address, 0, sizeof(m_address));
int nAddrLen = sizeof(m_address);
//根据套接字获取地址信息
if (::getpeername(m_socket, (SOCKADDR*)&m_address, &nAddrLen) != 0)
{
printf("获取IP地址失败");
return false;
}
//读取IP和Port
return true;
}
//封装套接字绑定
int make_socket(const char* address, int port)
{
//创建套接字
SOCKET Sock = socket(AF_INET, SOCK_STREAM, 0);
//绑定套接字
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = AF_INET; //使用IPv4地址
sockAddr.sin_addr.s_addr = inet_addr(address); //回送IP地址
sockAddr.sin_port = htons(1234); //端口
bind(Sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
return Sock;
}
int main() {
//初始化DLL
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//向服务器发起请求
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //回送地址
sockAddr.sin_port = htons(1234);
char bufSend[BUF_SIZE] = { 0 };
char bufRecv[BUF_SIZE] = { 0 };
while (true)
{
//创建套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
//获取服务器地址端口相关
SOCKADDR_IN address;
GetAddressBySocket(sock, address);
cout << "IP: " << ::inet_ntoa(address.sin_addr) << " PORT: " << ntohs(address.sin_port) << endl;
//获取文件位置
std::ifstream file;
do
{
cout << "请输入文件名" << std::endl;
std::string filename = "";
cin >> filename;
file.open(filename, std::ifstream::binary);
} while (!file.is_open());
//传输文件
//发送完毕标志
bool flag = true;
for (int i = 0; !file.eof();)
{
char buf[1024] = { '\0' };
memset(buf, 0, sizeof(buf));
file.read(buf, sizeof(buf) - 1);
int n = file.gcount();
int send_char_num= send(sock, buf, n, 0);
if (send_char_num==0)
{
cout << "服务器已连接断开" << endl;
flag = false;
break;
}
if (send_char_num<0)
{
cout << "发生错误,我也不知道是啥错误^_^" << endl;
flag = false;
break;
}
i += send_char_num;
cout << "已发送:" << i << "字节" << endl;
}
if (flag)
{
cout << "文件已发送完毕" << endl;
}
closesocket(sock); //关闭套接字
}
WSACleanup(); //终止使用 DLL
return 0;
}