client
#include <iostream>
#include <fstream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSAData wsd;
SOCKET soSend;
struct sockaddr_in siRemote;
char sendBuf[65536]; // 发送缓冲区
int bytesSent;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) {
std::cerr << "WSAStartup failed with error: " << WSAGetLastError() << std::endl;
return 1;
}
soSend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (soSend == INVALID_SOCKET) {
std::cerr << "Socket creation failed with error: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
memset(&siRemote, 0, sizeof(siRemote));
siRemote.sin_family = AF_INET;
siRemote.sin_port = htons(5150);
inet_pton(AF_INET, "138.10.8.1", &siRemote.sin_addr.s_addr);
std::ifstream file("C:\\Users\\jiang\\Desktop\\input.txt", std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open input file." << std::endl;
closesocket(soSend);
WSACleanup();
return 1;
}
while (file.good()) {
file.read(sendBuf, sizeof(sendBuf));
int bytesRead = file.gcount();
if (bytesRead > 0) {
bytesSent = sendto(soSend, sendBuf, bytesRead, 0, (struct sockaddr*)&siRemote, sizeof(siRemote));
if (bytesSent == SOCKET_ERROR) {
std::cerr << "sendto failed with error: " << WSAGetLastError() << std::endl;
break;
}
}
}
file.close();
closesocket(soSend);
WSACleanup();
std::cout << "Sent " << bytesSent << " bytes." << std::endl;
return 0;
}
server
#include <iostream>
#include <fstream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSAData wsd;
SOCKET soRecv;
struct sockaddr_in siLocal, siRemote;
char recvBuf[65536]; // 接收缓冲区
int bytesReceived;
int fromlen = sizeof(siRemote);
int totalBytesReceived = 0; // 用于跟踪接收的总字节数
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) {
std::cerr << "WSAStartup failed with error: " << WSAGetLastError() << std::endl;
return 1;
}
soRecv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (soRecv == INVALID_SOCKET) {
std::cerr << "Socket creation failed with error: " << WSAGetLastError() << std::endl;
WSACleanup();
return 1;
}
memset(&siLocal, 0, sizeof(siLocal));
siLocal.sin_family = AF_INET;
siLocal.sin_port = htons(5150);
siLocal.sin_addr.s_addr = INADDR_ANY;
if (bind(soRecv, (struct sockaddr*)&siLocal, sizeof(siLocal)) == SOCKET_ERROR) {
std::cerr << "Bind failed with error: " << WSAGetLastError() << std::endl;
closesocket(soRecv);
WSACleanup();
return 1;
}
std::ofstream file("C:\\Users\\jiang\\Desktop\\output.txt", std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open output file." << std::endl;
closesocket(soRecv);
WSACleanup();
return 1;
}
while (totalBytesReceived < 10) {
bytesReceived = recvfrom(soRecv, recvBuf, sizeof(recvBuf), 0, (struct sockaddr*)&siRemote, &fromlen);
if (bytesReceived == SOCKET_ERROR) {
std::cerr << "recvfrom failed with error: " << WSAGetLastError() << std::endl;
break;
}
file.write(recvBuf, bytesReceived);
totalBytesReceived += bytesReceived;
}
std::cout << "Received " << bytesReceived << " bytes." << std::endl;
file.close();
closesocket(soRecv);
WSACleanup();
return 0;
}