socket 客户端可以向服务器端接收和发送文件,代码怎么写

socket 客户端可以向服务器端接收和发送文件,代码怎么写,第1张

之前写过这样的一个小程序,我发布在自己的博客上:>

Serverc

#include <WinSock2h>
#include <WS2tcpiph>
#include <cstdio>
#include <iostream>
// for file read
#include <fstream>
#include <string>
int main() {
/ load the initializer library /
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
/ list the each fields of wsaData /
// printf("wVersion: %d\n", wsaDatawVersion); // 514
// printf("wHighVersion: %d\n", wsaDatawHighVersion); // 514
// printf("iMaxSockets: %d\n", wsaDataiMaxSockets); // 0
// printf("iMaxUdpDg: %d\n", wsaDataiMaxUdpDg); // 0
/ you may not print the lpVendorInfo /
// printf("szDescription: %s\n", wsaDataszDescription); // WinSock 20
// printf("szSystemStatus: %s\n", wsaDataszSystemStatus); // Running
/ create socket(address family, type, protocol) /
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
/ initialize the addrinfo hints /
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hintsai_family = AF_INET;
hintsai_socktype = SOCK_STREAM;
hintsai_protocol = IPPROTO_TCP;
hintsai_flags = AI_PASSIVE;
struct addrinfo result = nullptr;
/ by hints, and then get the result(a linked list, see fields below) /
getaddrinfo(NULL, "8080", &hints, &result);
/ the member fiekds of result /
// printf("ai_family: %d\n", result->ai_family); // 2
// printf("ai_socktype: %d\n", result->ai_socktype); // 1
// printf("ai_protocol: %d\n", result->ai_protocol); // 6
// printf("ai_flags: %d\n", result->ai_flags); // 0
// printf("ai_canonname: %s\n", result->ai_canonname); // (null)
// printf("ai_a ddrlen: %d\n", result->ai_addrlen); // 16
// printf("ai_addr->sa_family: %d\n", result->ai_addr->sa_family); // 2
// printf("ai_addr->sa_data: %s\n", result->ai_addr->sa_data); // 
// printf("ai_next: %x\n", result->ai_next); // 0
/ bind the socket to a address /
/ bind(socket, sockaddr name, name len) /
bind(socket_fd, result->ai_addr, result->ai_addrlen);
freeaddrinfo(result);
/ listen the socket /
/ listen(socket, backlog) /
listen(socket_fd, SOMAXCONN); / 0x7fffffff /
while(true) {
/ accept a connection /
/ accept(socket, addr, addrlen) /
int client_fd = accept(socket_fd, nullptr, 0);
printf("New Connection Eastablished\n");
/ Now we send the source code of this file to client /
std::ifstream fin("c:\\sourcecpp");
/ store it into a buffer /
while (fingood()) {
// I rememeber there is a method called getline
std::string s;
getline(fin, s);
// we need a new line
s = s + '\n';
send(client_fd, sc_str(), ssize(), 0);
}
finclose();
/ close client socket /
closesocket(client_fd);
}
/ may be never invoked /
closesocket(socket_fd);
WSACleanup();
return 0;
}

Clientc

#include <WinSock2h>
#include <cstdio>
#include <iostream>
#include <fstream>
int main() {
/ load the initializer library /
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
/ create socket /
int socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
/ connect /
sockaddr_in addr;
addrsin_family = AF_INET;
addrsin_addrs_addr = inet_addr("127001");
addrsin_port = htons(8080);
/ connect(socket, sockaddr, namelen) /
connect(socket_fd, (sockaddr)&addr, sizeof(addr));
/ buffer memory /
char buf[1024];
/ open a file for write /
std::ofstream fout("sourcecpp", std::ios_base::trunc);
while (true) {
int read = recv(socket_fd, buf, 1024, 0); 
if (read == 0) {
foutclose();
printf("Connection Closed\n");
break;
}
if (read > 1024) {
printf("Buffer Overflow\n");
} else {
buf[read - 1] = 0;
/ now we can print the bytes from server /
fout << buf << std::endl;
}
}
/ close socket /
closesocket(socket_fd);
WSACleanup();
return 0;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13508433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-21
下一篇 2023-08-21

发表评论

登录后才能评论

评论列表(0条)

保存