socket 客户端可以向服务器端接收和发送文件,代码怎么写
展开全部
之前写过这样的一个小程序,我发布在自己的博客上:http://www.zhouhaibing.com/blog/archive/8
Server.c
#include <WinSock2.h>
#include <WS2tcpip.h>
#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", wsaData.wVersion); // 514
// printf("wHighVersion: %d\n", wsaData.wHighVersion); // 514
// printf("iMaxSockets: %d\n", wsaData.iMaxSockets); // 0
// printf("iMaxUdpDg: %d\n", wsaData.iMaxUdpDg); // 0
/* you may not print the lpVendorInfo */
// printf("szDescription: %s\n", wsaData.szDescription); // WinSock 2.0
// printf("szSystemStatus: %s\n", wsaData.szSystemStatus); // 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));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_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:\\source.cpp");
/* store it into a buffer. */
while (fin.good()) {
// 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, s.c_str(), s.size(), 0);
}
fin.close();
/* close client socket */
closesocket(client_fd);
}
/* may be never invoked */
closesocket(socket_fd);
WSACleanup();
return 0;
}
Client.c
#include <WinSock2.h>
#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;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_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("source.cpp", std::ios_base::trunc);
while (true) {
int read = recv(socket_fd, buf, 1024, 0);
if (read == 0) {
fout.close();
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询