Client
#include<iostream>
#include <stdio.h>
#include<WinSock2.h>
#include<string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS 1
#define SERVER_PORT 8080
#pragma comment (lib,"ws2_32.lib")
#pragma warning (disable:4996)
void l_s(SOCKET sock)
{
char cmd[2] = "L";
send(sock, cmd, strlen(cmd), 0);
char resp[1024];
SOCKET iResult = recv(sock, resp, sizeof(resp), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
if (resp[0] != 'O') {
printf("列出文件目录失败:%s\n", resp);
return;
}
char* p = resp + 1;
while (*p != '\0') {
printf("%s\n", p);
p += strlen(p) + 1;
}
}
void i_nfo(SOCKET sock, char* path)
{
char cmd[3] = "I";
strcat(cmd, path);
send(sock, cmd, strlen(cmd), 0);
char resp[1024];
SOCKET iResult = recv(sock, resp, sizeof(resp), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
if (resp[0] != 'O') {
printf("获取文件详细信息失败:%s\n", resp);
return;
}
char* p = resp + 1;
while (*p != '\0') {
printf("%s\n", p);
p += strlen(p) + 1;
}
}
void f_ind(SOCKET sock, char* path)
{
char cmd[4] = "F";
strcat(cmd, path);
send(sock, cmd, strlen(cmd), 0);
char resp[1024];
SOCKET iResult = recv(sock, resp, sizeof(resp), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
if (resp[0] != 'O') {
printf("文件搜索失败:%s\n", resp);
return;
}
char* p = resp + 1;
while (*p != '\0') {
printf("%s\n", p);
p += strlen(p) + 1;
}
}
void g_et(SOCKET sock, char* path) {
char cmd[4] = "G";
strcat(cmd, path);
send(sock, cmd, strlen(cmd), 0);
char resp[1024];
SOCKET iResult = recv(sock, resp, sizeof(resp), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
if (resp[0] != 'O') {
printf("文件回传失败:%s\n", resp);
return;
}
char buf[1024];
int len;
while ((len = recv(sock, buf, sizeof(buf), 0)) > 0) {
FILE* fp = fopen(path, "wb");
fwrite(buf, len, 1, fp);
fclose(fp);
}
free(buf);
}
void p_ut(SOCKET sock, char* path) {
send(sock, path, strlen(path), 0);
FILE* fp = fopen(path, "rb");
if (fp == NULL) {
printf("打开文件失败:%s\n", path);
return;
}
char buf[1024];
int len;
while ((len = fread(buf, 1, 1024, fp)) > 0) {
send(sock, buf, len, 0);
}
fclose(fp);
}
void f_ormat(SOCKET sock, char* path)
{
char cmd[5] = "M";
strcat(cmd, path);
send(sock, cmd, strlen(cmd), 0);
char resp[1024];
SOCKET iResult = recv(sock, resp, sizeof(resp), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return;
}
if (resp[0] != 'O') {
printf("文件格式识别失败:%s\n", resp);
return;
}
printf("文件格式:%s\n", resp + 1);
}
int main()
{
WSADATA wsaData;
SOCKET iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup() 失败:%d\n", (int)iResult);
return 1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket() 失败:%d\n", WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
iResult = connect(sock, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (iResult != 0) {
printf("connect() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
char cmd[1024];
while (1) {
printf("请输入指令:");
scanf("%s", cmd);
SOCKET iResult = send(sock, cmd, strlen(cmd), 0);
if (iResult == SOCKET_ERROR) {
printf("send() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
char resp[1024];
SOCKET i_Result = recv(sock, resp, sizeof(resp), 0);
if (i_Result == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
printf("响应:%s\n", resp);
switch (resp[0]) {
case 'L':
l_s(sock);
break;
case 'I':
i_nfo(sock, resp + 1);
break;
case 'F':
f_ind(sock, resp + 1);
break;
case 'G':
g_et(sock, resp + 1);
break;
case 'P':
p_ut(sock, resp + 1);
break;
case 'M':
f_ormat(sock, resp + 1);
break;
default:
printf("未知指令:%s\n", cmd);
break;
}
}
closesocket(sock);
int WSACleanup();
return 0;
}
server-side
#include<iostream>
#include <stdio.h>
#include<WinSock2.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<fileapi.h>
#define SERVER_PORT 8080
#pragma comment (lib,"ws2_32.lib")
#pragma warning (disable:4996)
void Sl_s(SOCKET sock) {
char resp[2] = "O";
send(sock, resp, strlen(resp), 0);
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW(L"C:\\*", &findData);
if (hFind == INVALID_HANDLE_VALUE) {
resp[0] = 'E';
send(sock, resp, strlen(resp), 0);
return;
}
do {
wchar_t fileName[MAX_PATH];
wcscpy_s(fileName, findData.cFileName);
int len = wcslen(fileName) * sizeof(wchar_t);
send(sock, (const char*)fileName, len, 0);
} while (FindNextFileW(hFind, &findData));
FindClose(hFind);
}
void Si_nfo(SOCKET sock, char* path) {
char resp[2] = "O";
send(sock, resp, strlen(resp), 0);
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW((LPCWSTR)path, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
resp[0] = 'E';
send(sock, resp, strlen(resp), 0);
return;
}
do {
wchar_t fileName[MAX_PATH];
wcscpy_s(fileName, findData.cFileName);
char attr[10];
sprintf(attr, "%d", findData.dwFileAttributes);
char size[10];
sprintf(size, "%d", findData.nFileSizeLow);
char hidden[10];
if (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
strcpy(hidden, "是");
}
else {
strcpy(hidden, "否");
}
char info[1024];
sprintf(info, "%s\t%s\t%s\t%s\n", fileName, attr, size, hidden);
send(sock, info, strlen(info), 0);
} while (FindNextFileW(hFind, &findData));
FindClose(hFind);
}
void Sf_ind(SOCKET sock, char* path) {
char resp[2] = "O";
send(sock, resp, strlen(resp), 0);
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW((LPCWSTR)path, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
resp[0] = 'E';
send(sock, resp, strlen(resp), 0);
return;
}
do {
wchar_t fileName[MAX_PATH];
wcscpy_s(fileName, findData.cFileName);
int len = wcslen(fileName) * sizeof(wchar_t);
send(sock, (const char*)fileName, len, 0);
} while (FindNextFileW(hFind, &findData));
FindClose(hFind);
}
void Sg_et(SOCKET sock, char* path) {
char resp[2] = "O";
send(sock, resp, strlen(resp), 0);
FILE* fp = fopen(path, "rb");
if (fp == NULL) {
resp[0] = 'E';
send(sock, resp, strlen(resp), 0);
return;
}
char buf[1024];
int len;
while ((len = fread(buf, 1, 1024, fp)) > 0) {
send(sock, buf, len, 0);
}
fclose(fp);
}
void Sp_ut(SOCKET sock, char* path) {
char fileName[1024];
int iResult = recv(sock, fileName, sizeof(fileName), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
return;
}
FILE* fp = fopen(fileName, "wb");
if (fp == NULL) {
printf("打开文件失败:%s\n", fileName);
return;
}
char buf[1024];
int len;
while ((len = recv(sock, buf, sizeof(buf), 0)) > 0) {
fwrite(buf, 1, len, fp);
}
fclose(fp);
SetFileAttributes((LPCWSTR)fileName, FILE_ATTRIBUTE_HIDDEN);
}
void Sf_ormat(SOCKET sock, char* path) {
char resp[2] = "O";
send(sock, resp, strlen(resp), 0);
char buf[1024];
int len = recv(sock, buf, sizeof(buf), 0);
char format[10];
if (len >= 2 && memcmp(buf, "MZ", 2) == 0) {
strcpy(format, "EXE");
}
else if (len >= 2 && memcmp(buf, "DLL", 3) == 0) {
strcpy(format, "DLL");
}
else if (len >= 2 && memcmp(buf, "ELF", 3) == 0) {
strcpy(format, "ELF");
}
else if (len >= 8 && memcmp(buf, "PK\x03\x04", 4) == 0) {
strcpy(format, "ZIP");
}
else if (len >= 2 && memcmp(buf, "\xFF\xD8", 2) == 0) {
strcpy(format, "JPG");
}
else if (len >= 2 && memcmp(buf, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) {
strcpy(format, "PNG");
}
else if (len >= 2 && memcmp(buf, "BM", 2) == 0) {
strcpy(format, "bmp");
}
else if (len >= 4 && memcmp(buf, "Rar!\x1A\x07", 4) == 0) {
strcpy(format, "RAR");
}
else {
strcpy(format, "未知");
}
send(sock, format, strlen(format), 0);
}
int main() {
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup() 失败:%d\n", iResult);
return 1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket() 失败:%d\n", WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.s_addr = INADDR_ANY;
iResult = bind(sock, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (iResult != 0) {
printf("bind() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
iResult = listen(sock, SOMAXCONN);
if (iResult != 0) {
printf("listen() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
while (1) {
SOCKET clientSock = accept(sock, NULL, NULL);
if (clientSock == INVALID_SOCKET) {
printf("accept() 失败:%d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
char cmd[1024];
iResult = recv(clientSock, cmd, sizeof(cmd), 0);
if (iResult == SOCKET_ERROR) {
printf("recv() 失败:%d\n", WSAGetLastError());
closesocket(clientSock);
closesocket(sock);
WSACleanup();
return 1;
}
switch (cmd[0]) {
case 'L':
Sl_s(clientSock);
break;
case 'I':
Si_nfo(clientSock, cmd + 1);
break;
case 'F':
Sf_ind(clientSock, cmd + 1);
break;
case 'G':
Sg_et(clientSock, cmd + 1);
break;
case 'P':
Sp_ut(clientSock, cmd + 1);
break;
case 'M':
Sf_ormat(clientSock, cmd + 1);
break;
default:
printf("未知指令:%s\n", cmd);
break;
}
}
closesocket(sock);
int WSACleanup();
return 0;
}